msgbox combo box value

meyou

Registered User.
Local time
Today, 11:37
Joined
Feb 1, 2010
Messages
17
Hey everyone,

Something really simple here that is giving me some hard time! I guess I just don't understand the way it works!

I have 2 combo box on my form

-cmbExp1
-cmbExp2

Now, I need to print with a msgbox the selected value of cmbExp1 and cmbExp2. It works if writing the code like this: msgbox(cmbExp1.value). But what I want is to use a loop because in reality the number of combo boxes will change.

So, here's what I have :

temp = 2
For i = 1 To temp


MsgBox ("cmbExp" & i & ".Value")


Next

The result is : cmbExp1.value! I don't understand the logic behind that!

Sincerely,
 
Msgbox Controls("cmbExp" & i).value

Try that.

You need to qualify the name of your combo box. What you've done in the message box is call a string.
 
Ok,

I tried and it works for the first loop and in the second pass I received that error msg :

Run-time error '94':
Invalid use of Null.

Any ideas?
 
Msgbox Nz(Controls("cmbExp" & i).value, "")

That error is thrown if you are trying to display a Null value from via a message box. Try the above.

By the way, the use of a loop for just two controls is redundant. Just call your combo boxes one after the other.
 
Forget it, user problem!

Of course a value in cmbExp2 was not selected!

So it works, and to summarize, when you are saying "You need to qualify the name of your combo box", if I understand, you qualify the name by calling Controls()?

Sincerely,
 
Correct. If you were concatenating a couple of strings to form the name of an object or control you should use the appropriate collection. Controls() is the collection for all controls like buttons, labels, text boxes, combo boxes etc, the Forms() collection is that for a form (as the name implies). Concatenating without those will be regarded as string by the compiler.
 

Users who are viewing this thread

Back
Top Bottom