hey all!
please help me to check for validaiton such that... i hv few textboxes and combo boxes and if data is not entered in them then the submit button should be locked... and also tell me where should i write the code as in .... in form_load or submit_click... im confused... the code i wrote was...

If cname.Text = "" Or empname2.Text = "" Or reason2.Text = "" Or days2.Text = "" Or approval.Text = "Yes" Then
MsgBox "Please fill all details"
SUBMIT2(1).Enabled = False
Else: SUBMIT2(1).Enabled = True
End If

thanks a tonne... plz do help!
good day!... enjoy programming!...

Recommended Answers

All 11 Replies

Submit or Ok-button is the proper place to validate input. Since you have many things to validate, I would use a separate function:

Private Function ValidateForm() As Boolean
  ' Validate form fields
  If cname.Text = "" Or empname2.Text = "" Or reason2.Text = "" Or days2.Text = "" Or   approval.Text = "Yes" Then
    MsgBox "Please fill all details"
    ValidateForm = False
    Exit Function
  End If
  ValidateForm = True

End Function

and in the Submit or Ok-button validate:

Private Sub Command1_Click()
 ' Ok-button, validate before exit
   If ValidateForm Then
      ' Fields are ok, return from the form
    Me.Hide
  End If
End Sub

You can also make the function as common for all...

Public Function isAllFilledUp(frm As Form) As Boolean
    Dim ctrl As Control
    
    isAllFilledUp = True
    For Each ctrl in frm
         If TypeOf ctrl Is TextBox Then
              If ctrl = vbNullString Then
                      isAllFilledUp = False
                      Exit Function
              End If
         End If
    Next
End Function

There are many ways to validate whether it is a text box or a button or submit button, the way you write the code and the place you put your code can be vary, depending upon your decision, one simple way is to write the validation code and make that code as public so that you can call it from any where and any time you need. Let me give you one example:

Public Sub ControlMyButton()

if MyText1.Text="" OR MyCombo.Text="" then
Submitbutton.enable=False
else
Submitbutton.enable=True
end if

End Sub


The above code can be easily called (Instead of writing in event of the form) like this:
Say in the KeyUp Event of the text Box:

Call ControlMyButton


Even, In the MyText1_Change, and in the form Load event you can called 'Call ControlMyButton'

So, Only when the textbox is filled, the button will be enabled.

I think this will give you an Idea. Try in different way. Its not difficult.

thanks a lot for the responses... but its not working... i tried each of them invidually... but it is not working...
sorry... pls do keep helping!
thnx!

No. None of them is a complete solution and nobody can give one without seeing the whole code.

There may be a just a little spot everyone's missed. Can you give more details?

But few things to try out:
Text may contain spaces, so trim them off like Trim(cname.Text) = "" Then split your validation code to validate every control separately:

If Trim(cname.Text) = "" Then
  MsgBox "Please fill all details (cname)"
End If
If Trim(empname2.Text) = "" Then
  MsgBox "Please fill all details (empname)"
End If
... etc.

and tell us at which point validation fails and what was the input. You may do validation in any of the suggested places (Ok_Click, Form_Unload etc.).

Ok, I will just correct your code and make a little changes to make it more useful...

your code...

If cname.Text = "" Or empname2.Text = "" Or reason2.Text = "" Or days2.Text = "" Or approval.Text = "Yes" Then
MsgBox "Please fill all details"
SUBMIT2(1).Enabled = False
Else: SUBMIT2(1).Enabled = True
End If

put your code in a function let's name it as isFilledUp()

Private Function isFilledUp() As Boolean
     isFilledUp = True

     If Trim(cname.Text) = "" Then
           Msgbox "Please enter Customer Name."
           cname.SetFocus
           isFilledUp = False
           Exit Function
     End If

     If Trim(empname2.Text) = "" Then 
           Msgbox "Please enter Employee Name."
           empname2.SetFocus
           isFilledUp = False
           Exit Function
     End If

     If Trim(reason2.Text) = "" Then
           Msgbox "Please enter reason."
           reason2.SetFocus
           isFilledUp = False
           Exit Function
     End If

     If Trim(days2.Text) = "" Then
           Msgbox "Please enter days."
           days2.SetFocus
           isFilledUp = False
           Exit Function
     End If

     If Trim(approval.Text = "Yes" Then
           Msgbox " "   'put your comment here.
           isFilledUp = False
           Exit Function
     End If
End Function

Suppose you have a save button...

Private Sub btnSave_Click()
     If Not isFilledUp Then
         SUBMIT2(1).Enabled = False
         'add your code here if not all were filled up
     Else 
          SUBMIT2(1).Enabled = True
          'add your code here if all are filled up
     End If
End Sub

Huh! so much homework... Hope this would help you.

smile4evr said there are also combo boxes. Could the validation fail with combos?

WT?!!! just replace the checking in textboxes to combo boxes... You guys still in school? think this is a homework... anyways...

Dear I think I Solve Your Problime.
When You Open Your Curent Form VB use

Private sub Form1_load()

endsub

this Code is used for Form Load. When You Form Load You enter the following Code

Private sub form1_load()
text1.text=""
text2.text=""
text3.text=""
text4.text=""
Command1.Enable=false
Msgbox "Enter All Deatiles && fill All Fileds",VBInformation,"Add Detailes"
end Sub


I Hope This Code Work Accourding To Your Problime

So, smile4evr, does your problem solved?

thanks a lot! the problem has been solved!!

very sorry for late response!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.