Click to See Complete Forum and Search --> : Textbox


chakri_m16
May 8th, 2001, 11:08 PM
suppose i have three textboxes in a form and when i write this code and leave two textboxes empty it is displaying the message twice i.e. if in the form five textboxes r left empty then the message("fields should not be left empty") is displayed five times.please help me out

Public Sub CheckEmpty(frm As Form, ctl As Control)
For Each ctl In frm
If TypeOf ctl Is TextBox Then
If ctl.Text = "" Then
MsgBox " Feilds should not be empty "
ctl.SetFocus
End If
End If
Next ctl
End Sub

chakri_t@rediffmail.com

Cakkie
May 9th, 2001, 02:23 AM
You can jump out of any loop using the exit whatever statement.

public Sub CheckEmpty(frm as Form, ctl as Control)
' start looping
for Each ctl In frm
If TypeOf ctl is TextBox then
' check if textbox is filled in
If ctl.Text = "" then
' it isn't, display message
MsgBox " Fields should not be empty "
' give the control the focus
ctl.SetFocus
' stop checking for more
' Exit for is used, because we are in a for loop
Exit for
End If
End If
next ctl
End Sub





Tom Cannaerts
slisse@planetinternet.be

Programming today is a race between software engineers striving to build bigger and better idot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook