Code for multiple textboxes

sammy204

Registered User.
Local time
Today, 06:38
Joined
Jun 14, 2008
Messages
20
Hi, I put this under the wring topic thing before so I will put it here.

I have 10 text boxes and instead of writing the same code for each one 10 times, I wondered if i can run the same code on each one with a for statement. Each textbox has the same code run on it.
The code currently is:

DoCmd.GoToRecord , , acGoTo, 1
If Me.textbox1.Value = "Ordering" Then
Me.label1.BackColor = vbGreen
Else
Me.label1.BackColor = vbBlue
End If

and this is repeated for textbox2 and 3 up to 10. However it just changes the numbers 1 to a 2 or 3 etc. I was thinking of something like:


for number = 1 to 10
DoCmd.GoToRecord , , acGoTo, number
If Me.textbox(number).Value = "Ordering" Then
Me.label(number).BackColor = vbGreen
Else
Me.label(number).BackColor = vbBlue
End If


but this doesent work, I think its me.label(number) part.

Any Ideas?
all the textboxes have the same name of textbox, followed by a number 1 to 10. e.g. textbox1,textbox2,textbox3,textbox4 etc
 
Are these textboxes on the same form? If so where is the commands being run from (is it from onload)?
If these textboxes are on the same form then the best way is to enumerate the controls of the form an look for the textboxes, then find the captions of the text box to find which one you are looking at. Then you can change it's backcolor from the value.
 
Have a look at this snippit to see how to access the controls properties in a form.

' Call SetTextBoxProperties procedure. This is called from a form using the me keyword
SetTextBoxProperties Me

Sub SetTextBoxProperties(frm As Form)
Dim ctl As Control
' Enumerate Controls collection.
For Each ctl In frm.Controls
' Check to see if control is text box.
If ctl.ControlType = acTextBox Then
' Set control properties.
With ctl
.SetFocus
.Enabled = True
.Height = 400
.SpecialEffect = 0
End With
End If
Next ctl
End Sub
 
Last edited:
Hi thanks for the help

I will try that a bit later when I get a chance.

However I want to go to a record, then see if that the data in that record = ordering. The textbox1 is a control for one of the fields in a table so I need to go to each record, see if it is ordering, and if it is colour a different label which corresponds to its record number (the primary ID). Made a mistake on the code I wrote on this forum, the textbox doesnt change number e.g. textbox1,2,3 its the same one each time. What changes is the record I am on.

Thanks I will try what you said a bit later anyway
 
Last edited:

Users who are viewing this thread

Back
Top Bottom