I know how to refer to an item on a form by choosing the form, i.e. Forms!frmMyForm![Control].Value
. But I have code that that finds which form is open, and then executes code from that variable, and it's not working. I think I just need to refer to the control using ActiveForm, rather than an explicit form name, but the things I've tried to accomplish that haven't worked.
Here's my code.
Private Sub OrderButton_Click()
'
DoCmd.SetWarnings False
'
CurRecord = Forms!frm_EC_All![L#].Value
GetID = Forms!frm_MainMenu!AssocIDBox
'
Me.LBox.Value = CurRecord
'
If CurrentProject.AllForms("frm_EC_All").IsLoaded = True Then
RightForm = "frm_EC_All"
ElseIf CurrentProject.AllForms("frm_EC_EC#").IsLoaded = True Then
RightForm = "frm_EC_EC#"
ElseIf CurrentProject.AllForms("frm_EC_Holds").IsLoaded = True Then
RightForm = "frm_EC_Holds"
ElseIf CurrentProject.AllForms("frm_EC_L1").IsLoaded = True Then
RightForm = "frm_EC_L1"
ElseIf CurrentProject.AllForms("frm_EC_L1_L2").IsLoaded = True Then
RightForm = "frm_EC_L1_L2"
ElseIf CurrentProject.AllForms("frm_EC_L2").IsLoaded = True Then
RightForm = "frm_EC_L2"
ElseIf CurrentProject.AllForms("frm_EC_L34").IsLoaded = True Then
RightForm = "frm_EC_L34"
End If
'
Me.Refresh
'
DoCmd.RunSQL "UPDATE tbl_CQueue SET Order1 = -1, Order1Date = Now WHERE [L#] = " & CurRecord
DoCmd.Close acForm, "frm_Requests", acSaveYes
DoCmd.Close acForm, RightForm, acSaveYes
'
DoCmd.OpenForm RightForm, acNormal, , , , acWindowNormal
'
Dim CurrentForm As Form: Set CurrentForm = Screen.ActiveForm
MsgBox "Current form is " & CurrentForm.Name
'
CurrentForm.TimerActivatedLabel.Visible = True
'
GetID = Forms!frm_MainMenu!AssocIDBox
CurRecord = Forms!CurrentForm![L#].Value
'
DoCmd.RunSQL "UPDATE tbl_Data SET tbl_Data.[AssocID] = " & GetID & " , tbl_Data.[tsStartAll] = Now WHERE tbl_Data.[AssocID] Is Null AND tbl_Data.[L#] = " & CurRecord
'
Forms!CurrentForm!IDFieldBox.Value = GetID
'
DoCmd.SetWarnings True
'
End Sub
I get an error on the line CurrentForm.TimerActivatedLabel.Visible = True
. The error says "Database can't find the form 'CurrentForm' referred to in a macro expression or Visual Basic code."
So it sees what form is open in the background and stores that as "RightForm", then closes whichever that form is, opens it again, and then to try to get this to work I set the active form to CurrentForm
, and then it's supposed to set some textboxes and runs an SQL statement, etc. But it needs to know the form's name and it's not correctly grabbing CurrentForm
to use as the form's name.
Any ideas? Thanks.