Tony Toews <tt****@telusplanet.net> wrote in
news:ph********************************@4ax.com:
"WindAndWaves" <ac****@ngaru.com> wrote:
Every now and then I get an error: "Object Not set", which then
freezes everything. Not even CTRL + ALT + DELETE can really help.
No idea as to why it freezes. I was getting this message when I
had declared a global database variable but for some reason
somewhere it was getting lost. So I ended up resetting that
variable every time the user hit the main menu.
When I'm programming this would get lost when I was debugging code
and hitting the End code button. However if you're running on a
runtime then this isn't the case.
Don't use a global database variable. Instead, use a global function
that returns an object of type DAO.Database, since you can make that
function internally self-healing by re-initializing your actual
global variable, if necessary. My code for this follows my sig.
--
David W. Fenton
http://www.bway.net/~dfenton
dfenton at bway dot net
http://www.bway.net/~dfassoc
[at module level, same one as included the function:]
Private dbCurrent As DAO.Database
Public Function dbLocal(Optional ysnInitialize As Boolean = True) _
As DAO.Database
' 2003/02/08 DWF added comments to explain it to myself!
' 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
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 dbLocal()"
Resume exitRoutine
End Select
End Function