How do I 'Set an obj reference using a variable?
I have several unbound comboboxes bound with names cboC1, cboC2, cboC3 ... cboC119, cboC120.
After update any individual combo box will pass its value into its respective bound textboxes C1, C2, C3 .... C119, C120
The event handler would be the same for all 120 comboboxes, so I wanted to create two object variables as follows:
Dim cboC As ComboBox
Dim txtC As TextBox
Then I want to set the value of the objects to the firing combobox and its associated textbox (sort of as follows):
Private Sub cboC1_AfterUpdate()
cboSelectCustomer(cboC1.Name)
End Sub
Private Sub cboSelectCustomer(cbo as combobox)
Set cboC=cbo
Set txtC=Eval("C" & Right(cboC.name,len(cboC.name)-4)
If cboC <> " None" Then txtC = cboC Else txtC = Null
txtC.SetFocus
cboC.Visible = False
End Sub
In the first 'Set' command I am trying to pass in cboC1 as variable cboC. It doesn't work, Object required (Error 424)
( Can someone tell me how to make that work?
In the second 'Set' command I am to strip the '1' off the right end of cboC1 and then combine it with C to try to form the name of the object textbox 'C1', which is the name of the text box that I will pass the value into.
Unfortunately that doesn't work either. Eval results in a string and I have tried a variety of things but can't seem to get it.
(Can someone tell me how to make that work?)
The following does work, but if requires that I set the object variables all individually, so its a lot more redundancy of code:
Private Sub cboC1_AfterUpdate()
Set cboC = cboC1
Set txtC = C1
cboSelectCustomer
End Sub
Private Sub cboSelectCustomer()
If cboC <> " None" Then txtC = cboC Else txtC = Null
txtC.SetFocus
cboC.Visible = False
End Sub
My idea is to get the AfterUpdate events to be as short as possible, 1 line if possible, and have all the code in the cboSelectCustomer sub, then I have one place to manage the code for all 120 comboboxes and all 120 textboxes.
I hope I made the problem clear ... your help is appreciated.
|