(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?
asswaspAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dannywarehamCommented:
What is "frmCMReport.txtCMStartDate"?
If this code is being ran from the form, you can just use:
me.txtCMStartDate
Also, lose the .text - it's not needed

Private Sub cmdRunReport_Click()
    Dim retVal As Integer
    Dim myStr As String
    myStr = "start: "
    myStr = myStr & me.txtCMStartDate   ' error line
    myStr = myStr & ", end: "
    myStr = myStr & me.txtCMEndDate
    myStr = myStr & "."
    retVal = MsgBox(myStr, vbOKOnly)
End Sub


If it still gives you a prob, change the focus in the code:

Private Sub cmdRunReport_Click()
    Dim retVal As Integer
    Dim myStr As String
    myStr = "start: "
me.txtCMStartDate.setfocus
    myStr = myStr & me.txtCMStartDate   ' error line
    myStr = myStr & ", end: "
me.txtCMendDate.setfocus
    myStr = myStr & me.txtCMEndDate
    myStr = myStr & "."
    retVal = MsgBox(myStr, vbOKOnly)
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
asswaspAuthor Commented:
Your first version works perfectly. Thank you!
0
dannywarehamCommented:
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.

:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.