Help with an array of control variables.

G

Guest

Here is my code:
'Create variables with short names
Dim a, b As Control
Set a = Me.Controls("Email")
Set b = Me.Controls("Phone")

'Create array of variables
Dim aryControls(2) As Control
Dim Z as Variant

'Populate array
aryControls(1) = a <--- Error here: "Object variable or With block variable
not set" aryControls(2) = b

'Check each element in the array for being null, will eventually contain
many vars
For Each Z In aryControls

If IsNull(aryControls(1).Value) Then
MsgBox "aryControls(1) is Null"

Else
MsgBox "aryControls(1) has a value!"

End If
Next Z

How do I create an array of the control variables and then reference them in
loops?
 
D

Douglas J. Steele

Try:

Set aryControls(1) = a
Set aryControls(2) = b

BTW,

Dim a, b As Control

should be

Dim a As Control, b As Control

(there's no short circuiting in VBA declarations)
 
G

Guest

That is it! Thanks. I needed to use "Set" before aryControls(1) = a. Also I
kept the Dim a, b As Control and it does work.
 
D

Douglas J. Steele

It may work, but it's wrong. <g>

When you don't give a type, Access will assume Variant. A variant can
essentially be anything, so there's extra overhead determining what each
variant is at the time it's used.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top