954,559 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

validaition check... urgent...

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!...

smile4evr
Junior Poster in Training
53 posts since Aug 2008
Reputation Points: 10
Solved Threads: 2
 

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
Teme64
Veteran Poster
1,031 posts since Aug 2008
Reputation Points: 218
Solved Threads: 203
 

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
jireh
Posting Whiz
316 posts since Jul 2007
Reputation Points: 11
Solved Threads: 49
 

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.

K.Vanlalliana
Light Poster
44 posts since Jul 2008
Reputation Points: 10
Solved Threads: 4
 

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!

smile4evr
Junior Poster in Training
53 posts since Aug 2008
Reputation Points: 10
Solved Threads: 2
 

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.).

Teme64
Veteran Poster
1,031 posts since Aug 2008
Reputation Points: 218
Solved Threads: 203
 

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 asisFilledUp()

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.

jireh
Posting Whiz
316 posts since Jul 2007
Reputation Points: 11
Solved Threads: 49
 

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

Teme64
Veteran Poster
1,031 posts since Aug 2008
Reputation Points: 218
Solved Threads: 203
 

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

jireh
Posting Whiz
316 posts since Jul 2007
Reputation Points: 11
Solved Threads: 49
 
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
Bhatti302
Newbie Poster
8 posts since May 2008
Reputation Points: 10
Solved Threads: 1
 

So, smile4evr, does your problem solved?

jireh
Posting Whiz
316 posts since Jul 2007
Reputation Points: 11
Solved Threads: 49
 

thanks a lot! the problem has been solved!!

very sorry for late response!!

smile4evr
Junior Poster in Training
53 posts since Aug 2008
Reputation Points: 10
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You