Using a Variable to refer to a conrrol

R

Ray C

I have four combo Boxes Box_1, Box_2, Box_3 and Box_4. I want to set the
default value to some data as part of the After_Update event.
The cumbersome way to do it would be to have 4 individual sub routines to
handle I want to do a whole host of other things that would be the same in
each routine and a whole raft of code would be repeated for each Box Routine.
I have tried setting up the following bit I can not get it to work, any help
would be appreciated.

Private Sub Box_1_AfterUpdate(NewData as String)
Call BoxDefaultRoutine(NewData, 1)
End Sub

Rem This routine would be repeated for each of the other three boxes where
the number 1 would be changed to number 2, 3, or 4

Private Sub BoxDefaultRoutine(NewData as string, BoxNumber)
Dim BoxName as Control
Rem Other bits of code
BoxName = "Box_" & BoxNumber
me.BoxName.DefaultValue - """" & newDAta & """"
Rem More Code
End Sub

I have tried rapping BoxName in Parenthasis, tried seting it as a String and
a number of other odd variations but to no avail. Sorry if my attempt to
explain this is poor but I hope that you understand what I am trying to do.
Thanks Ray C
 
A

Arvin Meyer [MVP]

You can use the controltype property of a control like:

Dim ctl As Control
If ctl.ControlType = acComboBox Then

or you can use something like an array of the names:

Dim i As Integer
Dim frm As Form
Dim ctl As Control

For i = 1 To 4
For each ctl in frm.Controls

If ctl.Name = "Box_" & i Then
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top