T
teddysnips
Weird.
I have taken over responsibility for a legacy application, Access 2k3,
split FE/BE.
The client has reported a problem and I'm investigating. I didn't
write the application.
The AutoExec macro calls a function "InitApplication" which, in turn,
calls a function to set the value of a global string variable
"MyStrVar" (yes, I know, globals....)
The main form has a "Quit" button, which examines the value of this
global variable and performs an operation depending on the value.
However, a breakpoint at this point in the code shows that the
variable is empty (hence the problem).
After a good deal of trial and error, I have found out what is
happening, but not why! The following step-by-step guide shows what
I've found (I've added either MsgBox or breakpoints to allow me to
figure out what's going on).
1. Autoexec for the first time - "MyStrVar" is emptly
2. Call function to load variable - "MyStrVar" correctly loaded
3. Call function to quit application - "MyStrVar" empty - PROBLEM
4. Application quits.
Repeat the above but with slight variation
1. Autoexec for the first time - "MyStrVar" is emptly
2. Call function to load variable - "MyStrVar" correctly loaded
3. Call function to quit application - "MyStrVar" empty - PROBLEM
4. Press RESET button in code window and close form by flipping to
design mode - "MyStrVar" still empty
5. Run Autoexec for the second time - "MyStrVar" is empty
6. Call function to load variable - "MyStrVar" correctly loaded
7. Call function to quit application - "MyStrVar" now correctly
loaded - WHY?
At no point in the program is there an assignment to "MyStrVar" except
in the function to load the value. The operations that I'm performing
manually are simply to run the AutoExec macro, and pressing the "Quit"
button on the form. So my questions are:
a) Why is it "losing" the value between setting it and calling the
function to quit?
b) Why does this behaviour change if I press RESET and run it again?
In case it might help here is the relevant code:
' Function to load the variable
Public Sub GetAllQueries()
Dim rcd As DAO.Recordset
Set rcd = CurrentDb.OpenRecordset("SELECT tblDatabaseQueries.QryID
FROM tblDatabaseQueries;")
If rcd.RecordCount > 0 Then
With rcd
.MoveFirst
Do
MyStrVar = MyStrVar & "*" & !QryID & "*"
.MoveNext
Loop While Not (rcd.EOF)
End With
End If
End Sub
'Function to quit
Private Sub CloseDatabase_Click()
Dim Ref As Reference
Dim bar As CommandBar
Dim Qry As QueryDef
For Each Ref In References
If Ref.Name = "Word" Then References.Remove Ref
Next Ref
For Each bar In Application.CommandBars
bar.Enabled = True
Next bar
Call subfrmManageDatabaseQueries_Enter
For Each Qry In CurrentDb.QueryDefs
If InStr(MyStrVar , "*" & Qry.Name & "*") > 0 Then
CurrentDb.QueryDefs.Delete (Qry.Name)
End If
Next Qry
DoCmd.DeleteObject acTable, "tblEditedQueries"
DoCmd.Quit
End Sub
I have taken over responsibility for a legacy application, Access 2k3,
split FE/BE.
The client has reported a problem and I'm investigating. I didn't
write the application.
The AutoExec macro calls a function "InitApplication" which, in turn,
calls a function to set the value of a global string variable
"MyStrVar" (yes, I know, globals....)
The main form has a "Quit" button, which examines the value of this
global variable and performs an operation depending on the value.
However, a breakpoint at this point in the code shows that the
variable is empty (hence the problem).
After a good deal of trial and error, I have found out what is
happening, but not why! The following step-by-step guide shows what
I've found (I've added either MsgBox or breakpoints to allow me to
figure out what's going on).
1. Autoexec for the first time - "MyStrVar" is emptly
2. Call function to load variable - "MyStrVar" correctly loaded
3. Call function to quit application - "MyStrVar" empty - PROBLEM
4. Application quits.
Repeat the above but with slight variation
1. Autoexec for the first time - "MyStrVar" is emptly
2. Call function to load variable - "MyStrVar" correctly loaded
3. Call function to quit application - "MyStrVar" empty - PROBLEM
4. Press RESET button in code window and close form by flipping to
design mode - "MyStrVar" still empty
5. Run Autoexec for the second time - "MyStrVar" is empty
6. Call function to load variable - "MyStrVar" correctly loaded
7. Call function to quit application - "MyStrVar" now correctly
loaded - WHY?
At no point in the program is there an assignment to "MyStrVar" except
in the function to load the value. The operations that I'm performing
manually are simply to run the AutoExec macro, and pressing the "Quit"
button on the form. So my questions are:
a) Why is it "losing" the value between setting it and calling the
function to quit?
b) Why does this behaviour change if I press RESET and run it again?
In case it might help here is the relevant code:
' Function to load the variable
Public Sub GetAllQueries()
Dim rcd As DAO.Recordset
Set rcd = CurrentDb.OpenRecordset("SELECT tblDatabaseQueries.QryID
FROM tblDatabaseQueries;")
If rcd.RecordCount > 0 Then
With rcd
.MoveFirst
Do
MyStrVar = MyStrVar & "*" & !QryID & "*"
.MoveNext
Loop While Not (rcd.EOF)
End With
End If
End Sub
'Function to quit
Private Sub CloseDatabase_Click()
Dim Ref As Reference
Dim bar As CommandBar
Dim Qry As QueryDef
For Each Ref In References
If Ref.Name = "Word" Then References.Remove Ref
Next Ref
For Each bar In Application.CommandBars
bar.Enabled = True
Next bar
Call subfrmManageDatabaseQueries_Enter
For Each Qry In CurrentDb.QueryDefs
If InStr(MyStrVar , "*" & Qry.Name & "*") > 0 Then
CurrentDb.QueryDefs.Delete (Qry.Name)
End If
Next Qry
DoCmd.DeleteObject acTable, "tblEditedQueries"
DoCmd.Quit
End Sub