Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Identifying control types on a form

Status
Not open for further replies.

ALSav

Technical User
Feb 21, 2001
66
0
0
GB
I have a form linked to a database that displays names and address of businesses. If the business has closed a text box 'txtDate_Closed' contains the date of closure. If the business is closed, I would like all text boxes on the form to have a yellow background. I do not want the background colour of labels to change so I need to be able to identify which controls on my form are textboxes. Here's my attempt (which doesn't work

Private Sub Status()
'If the business is closed, set the colour of the text boxes
'on the form to yellow otherwise set them to white
Dim MyCtrl As Control
Dim lngYellow As Long
Dim lngWhite As Long

lngYellow = RGB(255, 255, 0)
lngWhite = RGB(255, 255, 255)

If AdoBrowse.Recordset!Date_Closed <> &quot;&quot; Then
For Each MyCtrl In Controls

Private Sub PracticeStatus()
'If the practice is closed, set the colour of the text boxes
'on the main form to yellow otherwise set them to white
Dim MyCtrl As Control
Dim lngYellow As Long
Dim lngWhite As Long

lngYellow = RGB(255, 255, 0)
lngWhite = RGB(255, 255, 255)

If AdoBrowse.Recordset!Date_Closed <> &quot;&quot; Then
For Each MyCtrl In Controls
If TypeOf .Controls Is TextBox Then 'Problem here
MyCtrl.BackColor = lngYellow
End If
Next MyCtrl
Else
For Each MyCtrl In Controls
If TypeOf .Controls Is TextBox Then
MyCtrl.BackColor = lngWhite
End If
Next MyCtrl
End If

Any ideas??
 
Try something like this:

Dim llSub1 As Long
For llSub1 = 0 To Me.Controls.Count - 1
If UCase$(TypeName(.Controls(llSub1))) = &quot;TEXTBOX&quot; then
'
'<--- do whatever...
'
End IF
Next

Hope this helps -- WB
 
Sorry...I omitted the &quot;Me&quot; qualifier. The &quot;If&quot; statement should look like this:

If UCase$(TypeName(Me.Controls(llSub1))) = &quot;TEXTBOX&quot; then


Or, you could use With...EndWith.
 
Try this:
For Each MyCtrl In Controls
If TypeOf MyCtrl Is TextBox Then
'Do something
End If
Next MyCtrl
 
this is a good one, its code from a form, you can loop through all the textboxes in the me.controls collection. No need to determine the control type.

Option Explicit
Private txtBox As TextBox

Private Sub Form_Load()
For Each txtBox In Me.Controls

' me refers to the form
txtBox.BackColor = vbYellow

Next
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top