Set object value with dynamic value

vmon

Registered User.
Local time
Today, 20:56
Joined
Jan 21, 2003
Messages
49
I want to set a varaible to an object but I want the object name to change. I have 3 fields called: field1, field2, and field3 and want to use a varialbe to reference each adding a counter to the end to identify which object. Here is what I have tried. A and B are just numbers. Any ideas?

Dim frm As Object
Dim frmItem as Object
Dim cnt as Integer

cnt = 1

Set frm = Forms!frmItem!fsubItemDetail
Set frmItem = Forms!frmItem!fsubItemDetail!Field & cnt

frm.frmItem = A + B

cnt = cnt + 1

Loop
 
Code:
Dim frm As Object 
Dim frmItem as Object 
Dim cnt as Integer 


For cnt= 1 to 3
    Set frm = Forms!frmItem!fsubItemDetail 
    Set frmItem = Forms!frmItem.form.fsubItemDetail.Controls("Field" & cnt)
    frm.frmItem = A + B 

Loop


Edit: Added the Paren to end of second line within the For Loop (Copy and Paste -> Programmers best friend and worst enemy :) )
Also realised I forgot to add the "form" between the Main Form and its sub form.
 
Last edited:
Thanks for the reply. I am not having luck with it though. To get the statement to not error out I added a right paren. It looks like this but frmCompItem is getting set to 0 after the statement executes which is the value in the control on the form. I changed the field name to the actual name too.

How do I get it to represent the Object so I can use it later like this.

Set frmItemComp = frm.Controls("Comp" & cnt)

frm.frmItemComp = A + B


Thanks again,
vmon
 
Last edited:

Users who are viewing this thread

Back
Top Bottom