I am trying to capture the value entered into the text box of a form instance which was created using the following module code:
Public myForm As Form_Form1
'Dim myForm As Form_Form1 ' tried this
Sub test()
'Dim myForm As New Form_Form1 ' tried this
Set myForm = New Form_Form1
With myForm
.Visible = True
' .SetFocus ' tried this
' .Modal = True ' tried this
If .IsCancelled Then
Exit Sub
End If
Debug.Print .RptDt
End With
The form is very basic with an OK and Cancel button and a single text box named Text7. The form code-behind is:
Private cancelling As Boolean
Public Property Get RptDt() As String
RptDt = Text7.Text
End Property
Public Property Get IsCancelled() As Boolean
IsCancelled = cancelling
End Property
Private Sub Command2_Click()
'DoCmd.Close acForm, Me.Name
Me.Visible = False
'Me.Visible
End Sub
Private Sub Command4_Click()
cancelling = True
'DoCmd.Close acForm, Me.Name
'MsgBox Me.Name
'MsgBox Me.OpenArgs
'Me.Hide
Me.Visible False
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = VbQueryClose.vbFormControlMenu Then
cancelling = True
Me.Visible = False
End If
End Sub
When I run the code as is I get:
"Run-time error '2185' You can't reference a property or method for a control unless the control has the focus"
I have also tried:
Public Property Get RptDt() As String
RptDt = Text7.Value
End Property
I then get Run-time error '94' Invalid use of null. The code above was modified from a comparable Excel VBA code which uses the Userform Show method (only works in Excel) in the Sub Test() instead of .Visible = True
.
DoEvents
under.Visible = True
– Unhandled Exception