I have some code that creates a new form, opens it in design view, and programmatically adds controls to this new form. As these controls are added, I use the Module.CreateEventProc Method, and Module.InsertLines, to programmatically add VBA code to the controls that I just created. My code is being added to the my new form correctly, and is surprisingly functioning correctly. However, after I finish creating these forms, VBA will 'Reset' thereby erasing the values of all my variables.
Suspected Operations causing this behavior:
(Set frm = CreateForm) (DoCmd.OpenForm frm.Name, acDesign) (Module.CreateEventProc) (Module.InsertLines)
Code:
Public Function WriteStatusLabelCode(ByRef currentModule As Module, _
ByRef ctrl As Control, ByVal statusIDStr As String)
Dim lngReturn As Long
Dim vbaStr As String
'
' local declarations for the label
'
'
' Create lbl_OnClick() Event
'
lngReturn = currentModule.CreateEventProc("Click", ctrl.Name)
vbaStr = vbTab & "If passStatusIDStr <> """" Then " & _
vbCrLf & vbTab & vbTab & "Me.Controls(currentLabelStr).BackStyle = 0" & _
vbCrLf & vbTab & vbTab & "Me.Controls(currentLabelStr).BackColor = 16777215" & _
vbCrLf & vbTab & "End If" & _
vbCrLf & vbTab & "passStatusIDStr = " & statusIDStr & _
vbCrLf & vbTab & "currentLabelStr = """ & ctrl.Name & """ " & _
vbCrLf & vbTab & "Me." & ctrl.Name & ".BackStyle = 1" & _
vbCrLf & vbTab & "Me." & ctrl.Name & ".BackColor = 10092543"
currentModule.InsertLines lngReturn + 1, vbaStr
lngReturn = currentModule.CreateEventProc("DblClick", ctrl.Name)
vbaStr = vbTab & "If passStatusIDStr <> """" Then " & _
vbCrLf & vbTab & vbTab & "Me.Controls(currentLabelStr).BackStyle = 0" & _
vbCrLf & vbTab & vbTab & "Me.Controls(currentLabelStr).BackColor = 16777215" & _
vbCrLf & vbTab & "End If" & _
vbCrLf & vbTab & "passStatusIDStr = " & statusIDStr & _
vbCrLf & vbTab & "currentLabelStr = """ & ctrl.Name & """ " & _
vbCrLf & vbTab & "Me." & ctrl.Name & ".BackStyle = 1" & _
vbCrLf & vbTab & "Me." & ctrl.Name & ".BackColor = 10092543" & _
vbCrLf & vbTab & "passStatusID = CInt(passStatusIDStr) " & _
vbCrLf & vbTab & "DoCmd.OpenForm ""frmViewStatus"""
currentModule.InsertLines lngReturn + 1, vbaStr
'currentModule.InsertLines lngReturn + 1, vbTab & "passStatusIDStr = " & statusIDStr 'Sets the public variable passStatusIDStr to the label's ID
End Function
So after I finish creating all my forms, I run some code to "re-learn" all my global variables. So Im reasonably happy at this point. Lastly, I run some code that deletes all the forms I just made. At this point VBA again 'Resets'.
Suspected Operations causing this behavior:
( DoCmd.DeleteObject )
Code:
Dim obj As Object, dbs As Object
Set dbs = Application.CurrentProject
' Search for open AccessObject objects in AllForms collection.
For Each obj In dbs.AllForms
'Debug.Print obj.Name
If obj.Name Like "tempForm*" Then
DoCmd.Close acForm, obj.Name
DoCmd.DeleteObject acForm, obj.Name
End If
Next obj
In this code, once the DoCmd.DeleteObject completes, VBA freaks out because it "forgets" that obj is Dim'd As Object, or it freaks out because it says the For Loop isn't initialized...
Question:
Is anyone familiar with these methods are typical methods or reasons that cause VBA to reset? I believe that any bit of code that makes changes to other code will run. However, upon completion the Project sense that the code has somehow changed, therefore it must either reset/recompile or something along those lines... It's also worth noting that during Sub-Routines that programmatically write code, VBA will not let me enter Break-Mode. I read somewhere that since the code is making changes to the code that VBA cannot 'hang' therefore it cannot enter break mode in situations like this.