0

I have a form (named DateForm) that contains a textbox (named txt_AsOfDate), which displays a date value.

I have a separate SUB which has the following code:

Sub Test()


    Dim AsOfDate As String
    AsOfDate = txt_AsOfDate.Text

    MsgBox (AsOfDate)

End Sub

When running it, I get the following error:

Run-time error '424': Object required

What is going on? I tried loading the DateForm at the beginning of the SUB, and also tried assigning the value by further defining the object schema like below, but no luck. What am I doing wrong?

AsOfDate = DateForm.txt_AsOfDate.Value
2
  • There must be something missing here. It works for me. What line does the error get thrown on? Commented Nov 12, 2013 at 21:33
  • @roryap - It is getting thrown on this line: AsOfDate = txt_AsOfDate.Text. I want to mention that the textbox is getting populated by a query running against one of the tables in the Access solution. Commented Nov 12, 2013 at 21:39

1 Answer 1

2

Try replacing txt_AsOfDate.Text with Form_DateForm.txt_AsOfDate.Text. Referring to a control directly in code by its name only works in the form, whereas I'm guessing this code is in a separate module.

In order to catch things like this, add Option Explicit to the top of your code modules. This forces the compiler to notify you if there is a variable being used which wasn't first declared with Dim.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.