loop through text boxes

G

Guest

I'm having trouble looping through text boxes (with various names) on a form.
I need to loop through each text box and see if the value = null, else exit
the loop and warn the user. THe only part I'm having trouble with is setting
the text boxes as my object and as a collection.
 
N

Nikos Yannacopoulos

Alodar,

Try something like:

For each ctl in Me.Controls
If ctl.ControlType = acTextBox Then
If Not IsNul(ctl) Then
Msgbox "Control " & ctl.Name & " is not Null!"
Exit For
End If
End If
Next

HTH,
Nikos
 
G

Guest

Sorta. More like though, I have 30 text boxes, the user can enter
information into. I need to loop through each text box, and see if the value
in that text box is null. If it is null, then I need to warn the user that
the textbox value is null (I have them named specially for that). The code
you gave me works, but it doesn't give the name of the text box? I'm sorta
confused on that. So far I have

Dim txts As TextBox

For Each txts In Me.Controls
If txts.Value = Null Then
MsgBox "You must have a value for " & txts & ".", vbCritical +
vbOKOnly, "Missing Info"
boolnotdone = True
End If
Next txts

But of course that doesn't work. I've been declaring all my controls and
variables lately, but for this one I've no idea what to declare the things as.
 
D

David C. Holley

I actually use the TAG property to flag wether or not a field should be
validated.

Tag = required

Sub OnForm_AfterUpdate

flgInvalidField = false

for i = 0 to [Forms]![myFormName].Controls.Count-1
If [Forms]![myFormName].Controls(i).Tag = "required" then
[Forms]![myFormName].Controls(i).backColor = [some-color-value]
flgInvalidField = true
end if
next i

If flgInvalidField = true then
MsgBox("The highlighted fields are required.")
end if

End sub

I wrote the code off the top of my head, so it might need some tweaking,
but that should give a good starting point.

David H
 
N

Nikos Yannacopoulos

Alodar,

See specific answers below. By the way, I took it for granted the code
is in the form's own module; is this the case?

Nikos
Sorta. More like though, I have 30 text boxes, the user can enter
information into. I need to loop through each text box, and see if the value
in that text box is null. If it is null, then I need to warn the user that
the textbox value is null (I have them named specially for that).
Well, in your original mail you said the opposite! Anyway...


The code
you gave me works, but it doesn't give the name of the text box? I'm sorta
confused on that. So far I have

Dim txts As TextBox

For Each txts In Me.Controls
If txts.Value = Null Then
MsgBox "You must have a value for " & txts & ".", vbCritical +
vbOKOnly, "Missing Info"
boolnotdone = True
End If
Next txts

But of course that doesn't work. I've been declaring all my controls and
variables lately, but for this one I've no idea what to declare the things as.
One thing I can see right off is why you don't get the name of the null
control; you need to reference the txts object's Name property there:

MsgBox "You must have a value for " & txts.Name & ".", ...etc

Other than that, I don't see any obvious reason why it shouldn't work.
Do you have any other problem?
 
D

David C. Holley

Add this code to the BeforeUpdate event of the form that needs to be
validated...

Call ValidateFields(Me.Name)

To each field that needs to be validated add the value: required
to the TAG property.

Add this to a new module...

Sub ValidateFields(varFormName As Variant)

Dim frm As Form
Dim flagMissingInformation As Integer
Dim flagFirstControl As Integer
Dim flagFirstControlTabIndex As Integer

On Error GoTo Err_ValidateFields
'Ensure all of the fields look 'normal' before highlighting them
For I = 0 To (frm.Count - 1)
If frm(I).Tag Like "*" & "Required" & "*" Then
frm(I).BackColor = 'normal back color value
frm(I).ForeColor = 'normal forecolor value
End If
Next I

flagMissingInformation = 0
flagFirstControlTabIndex = 999
Set frm = Forms(varFormName)

For I = 0 To (frm.Count - 1)
If frm(I).Tag Like "*" & "Required" & "*" Then
If frm(I) = "" Or IsNull(frm(I)) = True Then
flagMissingInformation = 1
frm(I).BackColor = 16711680
frm(I).ForeColor = 16777215
If flagFirstControlTabIndex > frm(I).TabIndex Then
flagFirstControl = I
flagFirstControlTabIndex = frm(I).TabIndex
End If
End If
End If
Next I

If flagMissingInformation = 1 Then
DoCmd.CancelEvent
frm(flagFirstControl).SetFocus
MsgTxt = "Please correct the highlighted fields or select CANCEL to
close this" & Chr$(10) & Chr$(13)
MsgTxt = MsgTxt & "form and abort adding displayed information."
If MsgBox(MsgTxt, 65) = 2 Then
SendKeys "{ESCAPE}"
DoCmd.CancelEvent
DoCmd.Close A_FORM, varFormName
If varFormName = "Create File From Reservation" Then
[Forms]![Main Form].SetFocus
End If
GoTo Exit_ValidateFields
End If
End If

Exit_ValidateFields:
Exit Sub

Err_ValidateFields:
ErrorMsg = "Validate Fields:" & Chr$(10) & Chr$(13) & Error$
MsgBox ErrorMsg, , "Validate Fields" & varFormName
Resume Exit_ValidateFields

End Sub

The code loops through each control and examines the TAG property. If
the TAG property is set to REQUIRED, the code then examines the value in
the field to determine if its NULL or a zero-length. If a value has not
been supplied, the field is highlighted. The tab index for that field is
then captured. Once the code finishes, the focus is set to the field in
the tab index that's highlighted and cancels the BeforeUpdate Event.

The code will need some modification if you're dealing with checkboxes
or radiobuttons but its shouldn't be that difficult.



David H
 
T

Tim Ferguson

I'm having trouble looping through text boxes (with various names) on
a form.

Since you are likely to have other controls on the form, a little bit of
judicious naming can make them look like a control array:

' assuming the text boxes are called txtRequired01, txtRequired02, etc
for i = 1 to 21
strControlName = format(i, """txtRequired"00")
if isnull(me.controls(strControlName).Value) Then
' it's empty
' warn the user
msgBox "You forgot box number " & i
' and put the focus back there
me.controls(strControlName).SetFocus
' set the flag
fErrorFound = True
' and don't do any more checking
exit for

end if
next i

If fErrorFound = False
' validation okay, carry on...



If the names are not regular, you can still do it like this:

' control the names: you could get these from a table
' if they have to be very dynamic
a_varTextBoxes = Array("txtFName", "txtLName", "etc")

' and iterate them
for i = 0 to UBound(a_varTextboxes)
If IsNull(me.controls(a_varTextBoxes(i)).Value) Then
' etc etc

Hope that helps


Tim F
 

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