Link to home
Start Free TrialLog in
Avatar of asswasp
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.Text
    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.txtCMStartDate.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?
ASKER CERTIFIED SOLUTION
Avatar of dannywareham
dannywareham
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of asswasp
asswasp

ASKER

Your first version works perfectly. Thank you!
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.

:-)