Loop through all controls on a form for validation.

G

Guest

Hi I have a form that is used to collect data in 20 different text boxes.

What I wish to do is to loop through all the objects of textbox type on the
form and test them for lenght and is Null ect before adding a new record. I
think it will be somthing like this, but(and try not to laugh) I can for the
life of me figure out how to declare and populate the collection/object:

dim myObject as object
dim myCollection as collection


For Each myObject in myCollection
If isNull(myObject.Value) Then
Msgbox("Field can not be null, bla bla bla")
myObject.setFocus
end if
Next


Any info would be great.

Thanks in advance.

James D.
 
K

Ken Snell \(MVP\)

Pretty close:

Dim ctl As Control
For Each ctl in Me.Controls
If ctl.ControlType = acTextBox Then
If Len(ctl.Value & "") = 0 Then
Msgbox("Field can not be null, bla bla bla")
ctl.SetFocus
Exit For
End If
End If
Next ctl
 
G

Guest

Works perfectly, thank you very much.

Ken Snell (MVP) said:
Pretty close:

Dim ctl As Control
For Each ctl in Me.Controls
If ctl.ControlType = acTextBox Then
If Len(ctl.Value & "") = 0 Then
Msgbox("Field can not be null, bla bla bla")
ctl.SetFocus
Exit For
End If
End If
Next ctl
 

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