asswasp
asked on
(easy) Accessing a form field value in VBA
I have a form (frmCMReport) with two text fields (txtCMStartDate, txtCMEndDate) and a button.
Code when you click the button:
Private Sub cmdRunReport_Click()
Dim retVal As Integer
Dim myStr As String
myStr = "start: "
myStr = myStr & frmCMReport.txtCMStartDate .Text ' error line
myStr = myStr & ", end: "
myStr = myStr & frmCMReport.txtCMEndDate.T ext
myStr = myStr & "."
retVal = MsgBox(myStr, vbOKOnly)
End Sub
(It's just simple code to display the values as I want to know how to access the field values. Doing stuff with them comes later.)
Right now when I run the code I get "Error 424 Object Required". If I change the error line to "myStr = myStr & Form_frmCMReport.txtCMStar tDate.Text " I get "Error 2185 You can't reference a property or method for a control unless the control has the focus".
This should be a simple thing to do, so how do I do it?
Code when you click the button:
Private Sub cmdRunReport_Click()
Dim retVal As Integer
Dim myStr As String
myStr = "start: "
myStr = myStr & frmCMReport.txtCMStartDate
myStr = myStr & ", end: "
myStr = myStr & frmCMReport.txtCMEndDate.T
myStr = myStr & "."
retVal = MsgBox(myStr, vbOKOnly)
End Sub
(It's just simple code to display the values as I want to know how to access the field values. Doing stuff with them comes later.)
Right now when I run the code I get "Error 424 Object Required". If I change the error line to "myStr = myStr & Form_frmCMReport.txtCMStar
This should be a simple thing to do, so how do I do it?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Just a note - if you're referencing an object (text box etc) on the form that your code is running on, you don't need to put the form name on.
If you type ME. when you press . you'll get a list of your objects.
:-)
If you type ME. when you press . you'll get a list of your objects.
:-)
ASKER