"Tony M." <an*************@navy.mil> wrote in
news:11**********************@j55g2000cwa.googlegr oups.com:
I'm trying to execute an append query. I have a case that works,
and one that returns an object not set error.
THIS WORKS:
Private Sub cmdArchiveRecs_Click()
BeginTrans
CurrentDb.Execute "qapp_RecsNotYetArchived", dbFailOnError
CommitTrans
End Sub
THIS DOESN'T WORK. It returns OBJECT NOT SET on dbLocal.Execute():
Dim dbLocal As DAO.Database
Private Sub Form_Load()
Set dbLocal = CurrentDb
End Sub
Private Sub Form_Unload(Cancel As Integer)
dbLocal.Close
Set dbLocal = Nothing
End Sub
Private Sub cmdArchiveRecs_Click()
BeginTrans
dbLocal.Execute "qapp_RecsNotYetArchived", dbFailOnError
CommitTrans
End Sub
Why isn't "dbLocal" set for the life of the form? Of what value is
setting a reference vice always using CurrentDb?
A truly persistent cached reference is much more useful. I've often
posted my code for a public function called dbLocal() that can be
used just like a public DAO.Database variable, and the code for that
is posted below after my sig. A discussion of some of the issues of
how it works is found in the thread beginning with this post:
http://groups.google.com/group/comp..../msg/9520d63d8
9decbb3
Paste the code into a public module and then you don't have to
declare a database variable anywhere in your app, or maintain a
public variable -- just use dbLocal anywhere you'd have used a
database variable initialized with CurrentDB() and forget about it.
The one thing you might want to do (though it's not strictly
necessary) is to de-initialize the variable when closing your app.
This is done by calling dbLocal with the optional argument set to
False.
--
David W. Fenton
http://www.dfenton.com/
usenet at dfenton dot com
http://www.dfenton.com/DFA/
Public Function dbLocal(Optional ysnInitialize As Boolean = True) _
As DAO.Database
' 2003/02/08 DWF added comments to explain it to myself!
' 2005/03/18 DWF changed to use Static variable instead
' uses GoTos instead of If/Then because:
' error of dbCurrent not being Nothing but dbCurrent being closed
' would (3420) would then be jumping back into the middle of an
' If/Then statement
On Error GoTo errHandler
Static dbCurrent As DAO.Database
Dim strTest As String
If Not ysnInitialize Then GoTo closeDB
retryDB:
If dbCurrent Is Nothing Then
Set dbCurrent = CurrentDb()
End If
' now that we know the db variable is not Nothing, test
' if it's Open
strTest = dbCurrent.Name
exitRoutine:
Set dbLocal = dbCurrent
Exit Function
closeDB:
If Not (dbCurrent Is Nothing) Then
'dbCurrent.close
Set dbCurrent = Nothing
End If
GoTo exitRoutine
errHandler:
Select Case Err.Number
Case 3420 ' Object invalid or no longer set.
Set dbCurrent = Nothing
If ysnInitialize Then
Resume retryDB
Else
Resume closeDB
End If
Case Else
MsgBox Err.Number & ": " & Err.Description, vbExclamation, _
"Error in Public Function dbLocal()"
Resume exitRoutine
End Select
End Function