丢失Db连接?!?!?! [英] Loosing Db connection?!?!?!

查看:70
本文介绍了丢失Db连接?!?!?!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我遇到以下问题;


我在找到这些公共变量后遇到问题正在设置,我不知道为什么。我使用下面的代码设置打开一个数据库连接到
。变量是公共变量,之前我没有任何问题,但我现在似乎遇到了麻烦。我可以在它上次工作的时间和现在之间修改它(当我正在清理

一些代码时),但我不记得碰任何东西了。


首先我运行此代码;

Public wksp As DAO.Workspace

Public db As DAO.Database


函数connectMyDb(可选strlocation As String)As Boolean

On Error GoTo connectMyDb_Err


如果strlocation =""然后strlocation = getAttachment()''如果没有指定

位置,那么从现有附件中获取附件


DBEngine.SystemDB =" system。 mdw"


设置wksp = CreateWorkspace(" ModifyDb"," Admin","",dbUseJet)


设置db = wksp.OpenDatabase(strlocation,False,False," ;; pwd ="&

GetPassword())

connectMyDb = True


connectMyDb_Exit:

退出函数


connectMyDb_Err:

如果Err.Number = 3031那么

''如果抛出此错误,用户已在他或她的b
自己的数据库上输入密码

SetCustomPassword

恢复

ElseIf Err.Number = 3051然后

''MsgBox"数据文件是只读的。无法继续,vbCritical,阅读

仅限数据文件

恢复connectMyDb_Exit

否则

MsgBox Err .Number& : &安培; Err.Description,vbCritical,错误

连接>

恢复connectMyDb_Exit

结束功能


我调用此代码后,我做了一个

Debug.Print db.Name


并抛出错误

" 3420对象无效或不再设置" ;.


我一直在撞墙,想知道为什么会这样。任何

想法?


谢谢!

解决方案

Jozef写道:< blockquote class =post_quotes>你好,

我遇到了以下问题;

我在浏览这些公共变量之后遇到了麻烦设置,我不知道为什么。我使用下面的代码设置打开与数据库的连接。变量是公共变量,我以前没有任何问题,但我现在似乎遇到了麻烦。我可以在它上次工作和现在之间修改它(当我清理一些代码时)但是我不记得碰到任何东西了。

首先我运行此代码;

公共wksp作为DAO.Workspace
公共数据库作为DAO.Database

函数connectMyDb(可选strlocation As String)作为Boolean
On Error GoTo connectMyDb_Err

如果strlocation =""然后strlocation = getAttachment()''如果没有指定
位置,则从现有附件获取附件

DBEngine.SystemDB =" system.mdw"

设置db = wksp.OpenDatabase(strlocation,False,False," ;; pwd = &
GetPassword())

connectMyDb = True

connectMyDb_Exit:
退出函数

connectMyDb_Err:
如果Err.Number = 3031则
''如果抛出此错误,则用户已在其自己的数据库中输入密码
SetCustomPassword
Resume
ElseIf Err.Number = 3051然后
''MsgBox'数据文件是只读的。无法继续,vbCritical,仅读取数据文件
恢复connectMyDb_Exit
其他
MsgBox Err.Number& : &安培; Err.Description,vbCritical,错误
连接
恢复connectMyDb_Exit
结束功能

我调用此代码后,我做了一个
调试.Print db.Name

并抛出错误
3420对象无效或不再设置。

我一直在打击我的头隔离墙想知道为什么会这样。任何
想法?

谢谢!




此代码不应该运行,因为它缺少End If。

如果确实运行它应该引发错误;这可以解释为什么

公共变量被释放。当出现错误

时会发布公共变量。


Jozef:


现在是时候有人说了这个:


您的代码令人费解,异常且令人费解。它的风格并不像经验丰富的开发人员编写的更好的代码那么多。或许,这是有效的b $ b;也许它适合你的独特需求,但是...


我的邪恶双胞胎,凯尔,会说代码是垃圾。他还会说,

简单的简单代码通常是很好的代码;长期涉及的代码通常很糟糕

代码。

幸运的是他不是写这个或者你可能会被冒犯,但他告诉

我之后他清理了你工作的代码中的所有kludge

罚款。他只是无法理解它的用途。


我建议你告诉我们你想做什么,清楚,并要求

for建议。


-

Lyle Fairfield


我是likin''这个Kyle伙计。


嗨Lyle,


如果我和你和你邪恶的双胞胎一样艺术,我很可能不会在这里

请求帮助。 ;)


结尾如果确实存在于原始代码中,并且很可能在

时被剪掉了。我编辑了一些无关的评论。


我能看到的问题不在于代码,而在于保留公共

变量之后。我已经完成了代码并且没有错误

抛出,但第二个代码已经完成,有些东西丢失了,因为我在原始帖子中说明了
;


我调用此代码后,我做了一个

Debug.Print db.Name


和那个抛出错误

3420对象无效或不再设置。


我正在尝试做的是在中央打开数据库连接

在其他地方使用的位置。因此,当例程超出范围时,

数据库对象仍处于活动状态且连接仍处于打开状态,直到我调用

a第二例例关闭连接并设置数据库变量

什么都没有。


如果您需要任何进一步的信息,请告诉我。如果你有更短的清洁方式来做这个,那么它可能会比击打更少的击键次数。


谢谢!


此代码不应该运行,因为它缺少End If。
如果它确实运行它应该引发错误;这可以解释为什么公共
变量被释放。发生错误时会发布公共变量。

Jozef:

现在是时候有人这样说了:

你的代码很复杂,很不寻常令人费解的。它的风格并不像经验丰富的开发人员编写的更好的代码。或许,这是有效的;也许它适合你的独特需求,但是...

我的邪恶双胞胎,凯尔,会说代码是垃圾。他还会说,简单的简单代码通常是很好的代码;长期涉及的代码往往很糟糕
代码。
幸运的是他没有写这个或者你可能会被冒犯,但他告诉我,他清理掉你的代码中的所有kludge后它工作得很好。
他只是无法理解它的用途。

我建议你告诉我们你想做什么,清楚,并要求提出建议。

- Lyle Fairfield



Hello,

I''m having the following issue;

I''m having trouble referencing these public variables after they are being
set, and I''m not sure why. I use the code below to set open a connection to
a database. The variables are public variables and I hadn''t had any
problems previously, but I seem to be having trouble now. I could have
modified it between the time it last worked and now (while I was cleaning up
some of the code) but I don''t recall touching anything.

First I run this code;
Public wksp As DAO.Workspace
Public db As DAO.Database

Function connectMyDb(Optional strlocation As String) As Boolean
On Error GoTo connectMyDb_Err

If strlocation = "" Then strlocation = getAttachment() '' If there is no
location specified, then get an attachment from existing attachments

DBEngine.SystemDB = "system.mdw"

Set wksp = CreateWorkspace("ModifyDb", "Admin", "", dbUseJet)

Set db = wksp.OpenDatabase(strlocation, False, False, ";pwd=" &
GetPassword())

connectMyDb = True

connectMyDb_Exit:
Exit Function

connectMyDb_Err:
If Err.Number = 3031 Then
''If this error is thrown, the user has entered a password on his or her
own database
SetCustomPassword
Resume
ElseIf Err.Number = 3051 Then
''MsgBox "Data file is read only. Cannot continue", vbCritical, "Read
Only Data file"
Resume connectMyDb_Exit
Else
MsgBox Err.Number & ": " & Err.Description, vbCritical, "Error
Connecting"
Resume connectMyDb_Exit
End Function


Right after I call this code, I do a
Debug.Print db.Name

and that throws the error
"3420 Object invalid or no longer set".

I''ve been beating my head against the wall wondering why this is. Any
ideas?

Thanks!

解决方案

Jozef wrote:

Hello,

I''m having the following issue;

I''m having trouble referencing these public variables after they are being
set, and I''m not sure why. I use the code below to set open a connection to
a database. The variables are public variables and I hadn''t had any
problems previously, but I seem to be having trouble now. I could have
modified it between the time it last worked and now (while I was cleaning up
some of the code) but I don''t recall touching anything.

First I run this code;
Public wksp As DAO.Workspace
Public db As DAO.Database

Function connectMyDb(Optional strlocation As String) As Boolean
On Error GoTo connectMyDb_Err

If strlocation = "" Then strlocation = getAttachment() '' If there is no
location specified, then get an attachment from existing attachments

DBEngine.SystemDB = "system.mdw"

Set wksp = CreateWorkspace("ModifyDb", "Admin", "", dbUseJet)

Set db = wksp.OpenDatabase(strlocation, False, False, ";pwd=" &
GetPassword())

connectMyDb = True

connectMyDb_Exit:
Exit Function

connectMyDb_Err:
If Err.Number = 3031 Then
''If this error is thrown, the user has entered a password on his or her
own database
SetCustomPassword
Resume
ElseIf Err.Number = 3051 Then
''MsgBox "Data file is read only. Cannot continue", vbCritical, "Read
Only Data file"
Resume connectMyDb_Exit
Else
MsgBox Err.Number & ": " & Err.Description, vbCritical, "Error
Connecting"
Resume connectMyDb_Exit
End Function

Right after I call this code, I do a
Debug.Print db.Name

and that throws the error
"3420 Object invalid or no longer set".

I''ve been beating my head against the wall wondering why this is. Any
ideas?

Thanks!



This code should not run as it is missing an End If.
If it does run it should raise an error; that would explain why the
public variable is released. Public Variables are released when an error
occurs.

Jozef:

It''s time someone said this:

Your code is convoluted, unusual and puzzling. It is not styled as much
of the better code written by experienced developers is. Perhaps, it''s
effective; perhaps it''s suited to your unique needs but ...

My evil twin, Kyle, would say that the code is crap. He would also say,
Short simple code is usually good code; long involved code is often bad
code.
Fortunately he is not writing this or you might be offended, but he told
me that after he cleaned out all the kludge from your code it worked
fine. He just couldn''t understand what it was for.

I would suggest that you tell us what you want to do, clearly, and ask
for suggestions.

--
Lyle Fairfield


I''m likin'' this Kyle dude.


Hi Lyle,

If I was as artistic as you and your evil twin, I likely wouldn''t be here
requesting assistance. ;)

The end if does exist in the original code, and was likely snipped out when
I edited some of the extraneous comments out.

The problem as I can see is not with the code, it''s in retaining the public
variables after. I have stepped through the code and there are no errors
thrown, but the second the code is finished, something is lost, because as I
stated in the orignal post;

Right after I call this code, I do a
Debug.Print db.Name

and that throws the error
"3420 Object invalid or no longer set".

What I''m attempting to do is open the database connection in a central
location for use elsewhere. So when the routine falls out of scope, the
database object is still live and the connection is still open until I call
a second routine that closes the connection and sets the database variable
to nothing.

If you need any further information please let me know. If you have a
shorter cleaner way to do this, then it would likely cost less keystrokes
than critism.

Thanks!


This code should not run as it is missing an End If.
If it does run it should raise an error; that would explain why the public
variable is released. Public Variables are released when an error occurs.

Jozef:

It''s time someone said this:

Your code is convoluted, unusual and puzzling. It is not styled as much of
the better code written by experienced developers is. Perhaps, it''s
effective; perhaps it''s suited to your unique needs but ...

My evil twin, Kyle, would say that the code is crap. He would also say,
Short simple code is usually good code; long involved code is often bad
code.
Fortunately he is not writing this or you might be offended, but he told
me that after he cleaned out all the kludge from your code it worked fine.
He just couldn''t understand what it was for.

I would suggest that you tell us what you want to do, clearly, and ask for
suggestions.

--
Lyle Fairfield



这篇关于丢失Db连接?!?!?!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆