Hi,
I'm trying to reference to a control on a form or subform, that has a part of it's name variable, ie it is named txtBox1, txtBox2, or on a subform control subForm1, subForm2, subForm3...
I have trouble injecting the variable part of the address into the string.
For example:
If I use Debug.Print Forms!Form1.txtBox_1.Value, the code resolves properly, but if I use Debug.Print Forms!Form1.[& counter1 &].Value I get error message 2465 "Microsoft Access can't find the field '|1' referred to in your expression."
I've tried placing "" everywhere and even building a string (it looks proper, as in Debug.Print Forms!Form1.txtBox_1.Value) and passing it to DoCmd., but it never resolves.
I've also tried passing entire Access.Controls object references to the subroutine, but can't manage to extract form/control reference back to the source unless the address is hard-coded.
Anyone able to advise on how to build form/control references programatically in VBA?
I'm trying to reference to a control on a form or subform, that has a part of it's name variable, ie it is named txtBox1, txtBox2, or on a subform control subForm1, subForm2, subForm3...
I have trouble injecting the variable part of the address into the string.
For example:
Code:
Sub DoSomething()
Dim i As Integer
Dim counter1 As String
For i = 1 To 5
counter1 = "txtBox_" & CStr(i)
Debug.Print Forms!Form1.[& counter1 &].Value
Next
End Sub
If I use Debug.Print Forms!Form1.txtBox_1.Value, the code resolves properly, but if I use Debug.Print Forms!Form1.[& counter1 &].Value I get error message 2465 "Microsoft Access can't find the field '|1' referred to in your expression."
I've tried placing "" everywhere and even building a string (it looks proper, as in Debug.Print Forms!Form1.txtBox_1.Value) and passing it to DoCmd., but it never resolves.
I've also tried passing entire Access.Controls object references to the subroutine, but can't manage to extract form/control reference back to the source unless the address is hard-coded.
Anyone able to advise on how to build form/control references programatically in VBA?