sion_corn
sion_corn

Reputation: 3141

pass TextBox value to variable within SUB

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

Upvotes: 0

Views: 4194

Answers (1)

Blackhawk
Blackhawk

Reputation: 6120

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.

Upvotes: 2

Related Questions