Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validating Multiple Textbox??? 1

Status
Not open for further replies.

RookieDBO

Technical User
Sep 20, 2001
83
0
0
CA
I need help on validating multiple textbox. My application involves 3 textbox where the user would input numeric data and a calculate button. I want to display an "error" msgbox if the user leaves the one or more of the textbox unfilled when clicking on the "calculate" button. The user must enter "zero" in those textbox in order to proceed with calculate. How do I go about on doing that and where do I place it???

Private Sub CommandButton4_Click()
'Calculate all three textboxes

TextBox8.Text = (Val(TextBox7) + Val(TextBox5) + Val(TextBox6)) * 1.15

End Sub

Thanks,
RookieDBO

 
It seems to me the easiest way to do this is take advantage of the validate event for each of text boxes. This event fires as you are "leaving" a textbox. There is a boolean variable -cancel- which you can set to true to prevent the focus from shifting to the next item. For example IsNumeric(txtbox1.text). If it is then leave cancel alone and the focus will move to the next item in the tab list. Make sure you have the textboxes in sequential tab order. In the validate procedure you can set the text to "0" if it is left empty.

I would also suggest that you avoid the use of the VAL subroutine for converting text. Use CDbl or Csng.

Hope this helps. Dan Grogan
dan@siqual.com

"Absit prudentia nil rei publicae profitur."
Without common sense you ain't gonna have nothing.
 
Better to NOT Enable Calculate until Textboxes are filled with VALID DATA. Imaqine a REACTOR START buttin being enabled BEFORE the Cooling System is ON.
GIVE THOSE CONTROLS DECENT NAMES.
DON'T bug your users with message boxes because you screwed up and LET THEM enter invalid data. It is YOUR FAULT when any user does anything to screw up your program.
I meant to shout but now I mean to help.
I use a centralized procedure when one control affects another.
Code:
Private Sub AdjustControls
    ' Call from ALL Change Events
    Dim blnEnabled As Boolean
    Dim StrW as string
    strW = Textcalc1.text
    blEnabled = Len(strW) > 0 and ISnumeric(strW)

    strW = Textcalc2.text
    blEnabled = blnEnabled and Len(strW) > 0 and ISnumeric(strW)

    strW = Textcalc3.text
    blEnabled = blnEnabled and Len(strW) > 0 and ISnumeric(strW)

    cmdCalc.enabled = blnEnabled
End Sub
Private Sub Form_Load()
    AdjustControls
End Sub
Private txtcal1_Change()
    AdjustControls
End Sub 
Private txtcal2_Change()
    AdjustControls
End Sub 
Private txtcal3_Change()
    AdjustControls
End Sub 
Private txtcalc1_KeyPress(KeyAscii As Integer)
    'KeyPress_Decimal KeyAsscii ' Decimal places?
    KeyPress_Number KeyAsscii 
End Sub 
Private txtcalc2_KeyPress(KeyAscii As Integer)
    'KeyPress_Decimal KeyAsscii ' Decimal places?
    KeyPress_Number KeyAsscii 
End Sub 
Private txtcalc3_KeyPress(KeyAscii As Integer)
    'KeyPress_Decimal KeyAsscii ' Decimal places?
    KeyPress_Number KeyAsscii 
End Sub 

Public Sub KeyPress_Ucase(KeyAscii As Integer)
    If KeyAscii = 8 Then Exit Sub
    KeyAscii = Asc(UCase$(Chr$(KeyAscii)))
End Sub
Public Sub KeyPress_Number(KeyAscii As Integer)
    If KeyAscii = 8 Then Exit Sub
    On Error Resume Next
        If Not Chr$(KeyAscii) Like "[0-9]" Then
            KeyAscii = 0
        End If
    On Error GoTo 0
End Sub
Public Sub KeyPress_AlphaNumeric(KeyAscii As Integer)
    If KeyAscii = 8 Then Exit Sub   ' BackSpace
    If Not (Chr$(KeyAscii) Like "[A-Z0-9]") Then KeyAscii = 0
End Sub
Public Sub KeyPress_Decimals(strText As String, KeyAscii As Integer)
    Dim strW As String
    On Error Resume Next
        If KeyAscii = 8 Then Exit Sub   ' BackSpace
        strW = Chr$(KeyAscii)
        If Not (strW Like "[0-9,.]") Then
            KeyAscii = 0
            Exit Sub
        End If
        If strW = "." Then
            If InStr(1, strText, ".") Then
                KeyAscii = 0
                Exit Sub
            End If
        End If
    On Error GoTo 0
End Sub
 
Thanks for the replies. I was hoping for basic solution but I'll take all of your advices. Maybe I should have had mentioned that I was a beginner.

Thanks,
Joe
 
Bad day huh, John?

Rookie, nothing wrong with being a beginner. We all were at some point.
 
Nope. Not a bad day. I included helpful advice didn't I. Just like Basic Training (Boot Camp), you have to get your point across hard and fast the first time so there will be no second time. This quote "got me".
"I want to display an "error" msgbox if the user leaves the one or more of the textbox unfilled when clicking on the "calculate" button."
This kind of thinking, punishing the user with a MsgBox for pressing a button that should not be enabled in the first place, has to be "corrected" early. The reply was not meant to be offensive, just forceful. Better me than the "pitchfork and torch carrying" users pouring into his/her cubicle. (We actually paid big $$ for an OS2 desktop application programmed by "H+1 A+1 L+1" that will not let users back up to a previous frame on a form to re-enter data that is needed by the later frame).
 
You should Check each textbox With something like :
If len(text1.text) <> 0 then
' user entered something
else
'display errormessage
endif
But I think you should also include the part :
if isnumeric(text1.text) then
'user entered numbers
else
'dispay error message
endif
 
Can someone help me on getting this to work???
I tried your way thesky. But still no luck in getting it to work. Here's how I did it. Any other simple way to do this???


Private Sub cmdCalculate_Click()

'Calculates all textbox including applicable taxes and
'displays
'results in GrandTotal textbox

If Len(txtPartyName.Text) <> 0 Then
' user entered something
Else
'display errormessage
MsgBox &quot;Error: Please fill in all blank field.&quot;
KeyAscii = 0
End If
Exit Sub

If Len(txtDoubleBus.Text) <> 0 Then
' user entered something
Else
'display errormessage
MsgBox &quot;Error: Please fill in all blank field.&quot;
KeyAscii = 0
End If
Exit Sub

If Len(txtGreyBus.Text) <> 0 Then
' user entered something
Else
'display errormessage
MsgBox &quot;Error: Please fill in all blank field.&quot;
KeyAscii = 0
End If
Exit Sub

If Len(txtHorse.Text) <> 0 Then
' user entered something
Else
'display errormessage
MsgBox &quot;Error: Please fill in all blank field.&quot;
KeyAscii = 0
End If
Exit Sub

txtGrandTotal.Text = (Val(txtSubtotal1) + Val
(txtSubtotal2) + Val(txtSubtotal3)) * 1.15

End Sub



Thanks,
RookieDBO
 
well........

If Len(txtPartyName.Text) = 0 Then
'display errormessage
MsgBox &quot;Error: Please fill in all blank field.&quot;
KeyAscii = 0
ElseIf Len(txtDoubleBus.Text) = 0 Then
'display errormessage
MsgBox &quot;Error: Please fill in all blank field.&quot;
KeyAscii = 0
ElseIf Len(txtGreyBus.Text) = 0 Then
'display errormessage
MsgBox &quot;Error: Please fill in all blank field.&quot;
KeyAscii = 0
ElseIf Len(txtHorse.Text) = 0 Then
'display errormessage
MsgBox &quot;Error: Please fill in all blank field.&quot;
KeyAscii = 0
Else
txtGrandTotal.Text = (Val(txtSubtotal1) + Val
(txtSubtotal2) + Val(txtSubtotal3)) * 1.15
End If

End Sub
Tim

Remember the KISS principle:
Keep It Simple, Stupid!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top