Taco Genie
Taco Genie

Reputation: 3

Having a variable reference an object in VBA

I'm writing some VBA code to dynamically position some elements on a form. What I would like to do is create a two dimensional array of references to 11 rows of label and text box objects, then step through the array and position each "row" differently based on the properties of the above rows.

Unfortunately, I cannot seem to find a way to store the references! The following code produces the error: "Object doesn't support this property or method"

Dim fields(10,10) 
fields(0,0) = txtFirstName

If I assign an object to a single variable like so

Dim field 
field = txtFirstName
MsgBox field.Top

Then I get no error message from the assignment, but the reference to field.Top gives me the error "Object Required".

Please, what am I doing wrong here. I'm new to stack overflow and VBA coding so forgive me if this is an easy fix, but I've leveraged my google-fu and I'm just not finding a solution. Many thanks to anyone who can help.

Upvotes: 0

Views: 2683

Answers (1)

Tom Cannaerts
Tom Cannaerts

Reputation: 648

You need to add the Set keyword, since you are dealing with object references.

Set fields(0,0) = txtFirstName

The reason you get the error, is because VBA will default to the text property of the textbox, giving you a string instead of the textbox object.

Upvotes: 3

Related Questions