Rod
Rod

Reputation: 151

How to dynamically assign a variable to sequentually named textboxes in VBA for Access

I have tbxInvites0 ... tbxInvites6. I would like to create a for loop which will increment the tbxInvites# and assign tbxInvites# = Invites. This is the idea but obviously won't work:

For DBNum = 0 to 6
frm.tbxInvites & DBNum = Invites
Next DBNum

Upvotes: 2

Views: 770

Answers (1)

HansUp
HansUp

Reputation: 97101

frm.tbxInvites & DBNum will give you a string, not a text box object. However, you can use a string to reference a control as member of your form's Controls collection.

For DBNum = 0 to 6
    'frm.tbxInvites & DBNum = Invites
    Me.Controls("tbxInvites" & DBNum).Value = Invites
Next DBNum

Upvotes: 1

Related Questions