View Full Version : Dynamic object variables


1jet
09-26-2008, 04:22 AM
Hi all,

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



Function vbaExample()

Dim counter As Integer
Dim frm as Form
Dim box as Control

Set frm = Forms!frmExample
counter = 0

Do
counter = counter + 1
box = "frm.txtBox" & counter
MsgBox box.Value
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!

KenHigg
09-26-2008, 04:39 AM
What are you are ultimately trying to do?

wazz
09-26-2008, 05:13 AM
look up 'For Each...Next Statement' in vba help and check out the example.

1jet
09-26-2008, 05:24 AM
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

boblarson
09-26-2008, 06:03 AM
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:


Set projectcbo = Forms!frmEmployeeTimesheet!cboSelectProject & project_count

WITHOUT the quotes. Or you can use this method


Dim strProjCBO As String

strProjCBO = "cboSelectProject" & project_count

Set projectcbo = Forms("frmEmployeeTimesheet").Controls(strProjCBO)

1jet
09-26-2008, 11:12 AM
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.