Dynamic object variables

1jet

Registered User.
Local time
Tomorrow, 03:30
Joined
Sep 15, 2008
Messages
117
Hi all,

I have some code similar to what I've written below...

Code:
Function vbaExample()

Dim counter As Integer
Dim frm as Form
[COLOR=Red]Dim box as Control[/COLOR]

Set frm = Forms!frmExample
counter = 0

Do
    counter = counter + 1
[COLOR=Red]    box = "frm.txtBox" & counter
    MsgBox box.Value[/COLOR]
Loop Until counter = 5

End Function
Basically I have 5 textboxes named txtBox1 - 5. I'm trying to get their VALUES not their names.
And it'd be nice to find whether my approach is even possible.
The lines in red are of concern to me.

Thanks!
 
What are you are ultimately trying to do?
 
look up 'For Each...Next Statement' in vba help and check out the example.
 
hi wazz...the problem isnt the loop
the loop works fine

ken ultimately im asking if using strings to set names to object variables is possible
let me clarify

projectcbo = "Forms!frmEmployeeTimesheet!cboSelectProject" & project_count
this works because projectcbo and project_count were declared as a string and integer respectively

Set projectcbo = "Forms!frmEmployeeTimesheet!cboSelectProject" & project_count
(note: Dim projectcbo As Control already declared) this time projectcbo is an object, but i'd like to use projectcbo.Value it in a loop with the project_count integer concatenated

im starting to think this isnt possible with object variables
 
Last edited:
hi wazz...the problem isnt the loop
the loop works fine

ken ultimately im asking if using strings to set names to object variables is possible
let me clarify

projectcbo = "Forms!frmEmployeeTimesheet!cboSelectProject" & project_count
this works because projectcbo and project_count were declared as a string and integer respectively

Set projectcbo = "Forms!frmEmployeeTimesheet!cboSelectProject" & project_count
(note: Dim projectcbo As Control already declared) this time projectcbo is an object, but i'd like to use projectcbo.Value it in a loop with the project_count integer concatenated

im starting to think this isnt possible with object variables

It is possible, but CURRENTLY you are trying to assign the TEXT not the actual control to the variable. You would need to change it to this:

Code:
Set projectcbo = Forms!frmEmployeeTimesheet!cboSelectProject & project_count
WITHOUT the quotes. Or you can use this method

Code:
Dim strProjCBO As String

strProjCBO = "cboSelectProject" & project_count

Set projectcbo = Forms("frmEmployeeTimesheet").Controls(strProjCBO)
 
I've tried the first approach, and failed.
Even tried declaring projectcbo as string although I'd normally use control.

Nevermind, I prefer your 2nd method.

You really are an MVP.
Thanks this will help me a great deal.
 

Users who are viewing this thread

Back
Top Bottom