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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validating data in a Text Box 3

Status
Not open for further replies.

hawaiian1975

Programmer
Nov 7, 2003
26
US
I am trying to validate data in a text box. Basically the data in the text box will be nothing but numbers. Letters and anything else will be invalid data. Here is the code that I have so far:


Private Sub mnuDataValidate_Click()
Const conBtns As Integer = vbOK + vbExclamation + vbDefaultButton1 + vbApplicationModal
Dim intRetVal As Integer
Select Case txtWheel(0).Text
Case "A" To "Z"
intRetVal = MsgBox("Invalid Data", conBtns, "Data Entry Form")
txtWheel(0).Text = ""
Case "a" To "z"
intRetVal = MsgBox("Invalid Data", conBtns, "Data Entry Form")
txtWheel(0).Text = ""
Case "~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "+", "=", "|", "\"
intRetVal = MsgBox("Invalid Data", conBtns, "Data Entry Form")
txtWheel(0).Text = ""
End Select
Select Case txtWheel(1).Text
Case "A" To "Z"
intRetVal = MsgBox("Invalid Data", conBtns, "Data Entry Form")
txtWheel(1).Text = ""
Case "a" To "z"
intRetVal = MsgBox("Invalid Data", conBtns, "Data Entry Form")
txtWheel(1).Text = ""
Case "~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "+", "=", "|", "\"
intRetVal = MsgBox("Invalid Data", conBtns, "Data Entry Form")
txtWheel(1).Text = ""
End Select
End Sub


If I was to type 123a4 the data won't be validated, but if I was to type Aaaa, the data will be validated. How do I write this so that anything other than numbers is invalid?
 
Try this...

Private Sub Text1_Change()
If Not ValidateNumeric(Text1.Text) Then
Text1.Text = ""
End If
End Sub

Private Function ValidateNumeric(strText As String) _
As Boolean
ValidateNumeric = CBool(strText = "" _
Or strText = "-" _
Or strText = "-." _
Or strText = "." _
Or IsNumeric(strText))
End Function
 
Hi,

Judging from your code you are using a menu click to validate your textboxes so try this:

Private Sub mnuDataValidate_Click()
Const conBtns = vbOK + vbExclamation + vbDefaultButton1 + vbApplicationModal

Dim Ndx As Integer
Dim Cancel As Boolean

For Ndx = txtWheel.LBound To txtWheel.UBound
txtWheel_Validate Ndx, Cancel
If Cancel Then
MsgBox "Invalid Data", conBtns, "Data Entry Form"
txtWheel(Ndx).SetFocus
Exit For
End If
Next Ndx
End Sub

Private Sub txtWheel_Validate(Index As Integer, Cancel As Boolean)
If Not IsNumeric(txtWheel(Index).Text) Then
Cancel = True
Else
Cancel = False
End If
End Sub

Have a good one!
BK
 
That was awesome BlackKnight, but I get the following error when I click the validate button after I get to the fourth text box:

Run-time error '5':
Invalid procedure call or argument

Here is the rest of my code:

Option Explicit


Private Sub chkWheel5_6_Click()
Const conBtns = vbOK + vbExclamation + vbDefaultButton1 + vbApplicationModal
Dim intRetVal As Integer
If chkWheel5_6.Value = vbChecked Then
txtWheel(4).Visible = True
txtWheel(5).Visible = True
lblWheel5.Visible = True
lblWheel6.Visible = True
Else
txtWheel(4).Visible = False
txtWheel(5).Visible = False
lblWheel5.Visible = False
lblWheel6.Visible = False
End If


If chkWheel5_6.Value = vbUnchecked And chkWheel7_8 = vbChecked Then
intRetVal = MsgBox("Invalid Operation, Uncheck Wheel 7 and 8", conBtns, "Data Entry")
chkWheel7_8.Value = vbUnchecked
chkWheel7_8.Enabled = False
txtWheel(6).Visible = False
txtWheel(7).Visible = False
txtWheel(6).Text = ""
txtWheel(7).Text = ""
End If
End Sub

Private Sub chkWheel7_8_Click()
If chkWheel7_8.Value = vbChecked Then
txtWheel(6).Visible = True
txtWheel(7).Visible = True
lblWheel7.Visible = True
lblWheel8.Visible = True
Else
txtWheel(6).Visible = False
txtWheel(7).Visible = False
lblWheel7.Visible = False
lblWheel8.Visible = False
End If
End Sub

Private Sub Form_Load()
chkWheel7_8.Enabled = False
txtWheel(4).Visible = False
txtWheel(5).Visible = False
txtWheel(6).Visible = False
txtWheel(7).Visible = False
lblWheel5.Visible = False
lblWheel6.Visible = False
lblWheel7.Visible = False
lblWheel8.Visible = False

'center the form
frmDataEntry.Top = (Screen.Height - frmDataEntry.Height) / 2
frmDataEntry.Left = (Screen.Height - frmDataEntry.Width) / 2
End Sub

Private Sub Form_Unload(Cancel As Integer)
Const conBtns As Integer = vbYesNo + vbExclamation + vbDefaultButton1 + vbApplicationModal
Dim intUserResponse As Integer
intUserResponse = MsgBox("Do you want to exit?", conBtns, "Data Entry Form")
If intUserResponse = vbYes Then
End
End If
If intUserResponse = vbNo Then 'user does not want to quit the application
Cancel = 1 'prevent form from being unloaded
End If
End Sub

Private Sub mnuExit_Click()
Unload frmDataEntry
End Sub


Private Sub mnuDataValidate_Click()
Const conBtns = vbOK + vbExclamation + vbDefaultButton1 + vbApplicationModal

Dim Ndx As Integer
Dim Cancel As Boolean

For Ndx = txtWheel.LBound To txtWheel.UBound
txtWheel_Validate Ndx, Cancel
If Cancel Then
MsgBox "Invalid Data", conBtns, "Data Entry Form"
txtWheel(Ndx).SetFocus
Exit For
End If
Next Ndx
End Sub

Private Sub txtWheel_Validate(Index As Integer, Cancel As Boolean)
If Not IsNumeric(txtWheel(Index).Text) Then
Cancel = True
Else
Cancel = False
End If

End Sub

Private Sub txtWheel_KeyPress(Index As Integer, KeyAscii As Integer)
If txtWheel(4).Text = "" And txtWheel(5).Text = "" Then
chkWheel7_8.Enabled = False
Else
chkWheel7_8.Enabled = True
End If
End Sub
 
Private function txtWheel_Validate(Index As Integer)
If Not IsNumeric(txtWheel(Index).Text) Then
txtWheel_Validate= True
Else
txtWheel_Validate= False
End If

End function
....
..
.
if not txtwheel_validate then
the value is numeric
end if


--
Luis MX
 
Did you try my example? It allows for all numeric type of input and blows Blacknights example away, but use his example if you wish, doesnt bother me if you use sub standard code
 
Thankyou luisfelipe007 Can you see a customer manually checking data input validation, or wanting too?
Using the Text_Change() function allows us to do an automatic check each time something is entered into the textbox(s), the customer is unaware of any data input validation unless there is non numeric data entered into the textbox(s) in which case a msgbox notifies the user of the problem.
 
[blue]Private Sub [/blue]txtwheel_Change(Index [blue]As Integer[/blue])
[blue]If Not [/blue]ValidateNumeric(txtwheel(Index).Text) [blue]Then
[/blue]MsgBox "Invalid Data", vbExclamation, "Data Entry Form"
txtwheel(Index).SetFocus
[blue]End If
End Sub

Private Function [/blue]ValidateNumeric(strText [blue]As String[/blue]) _
[blue]As Boolean
[/blue]ValidateNumeric = [blue]CBool[/blue](strText = "" _
[blue]Or [/blue]strText = "-" _
[blue]Or [/blue]strText = "-." _
[blue]Or [/blue]strText = "." _
[blue]Or [/blue]IsNumeric(strText))
[blue]End Function

[/blue][green]'-----------------------------------------

[/green]A couple of tricks...

[blue]Private Sub [/blue]Form_Load()

[blue]Dim [/blue]i [blue]As Integer
For [/blue]i = 4 [blue]to [/blue]7
txtWheel(i).Visible = [blue]False
Next [/blue]i

lblWheel5.Visible = [blue]False [/blue][green]'<Put these into arrays
[/green]lblWheel6.Visible = [blue]False [/blue][green]'You can loop them also
[/green]lblWheel7.Visible = [blue]False
[/blue]lblWheel8.Visible = [blue]False

[/blue]chkWheel7_8.Enabled = [blue]False

[/blue][green]'center the form '<Set the Form StartUp
'<Property to CentreScreen and you can remove the rest of the code
[/green]frmDataEntry.Top = (Screen.Height - frmDataEntry.Height) / 2
frmDataEntry.Left = (Screen.Height - frmDataEntry.Width) / 2
[blue]End Sub

[/blue]
 
Thanks hawaiian. This solves your other question now? (Because you dont have to validate the text with the mnuDataNextForm_Click

So for this event we can simplify the whole routine...

blue]Private Sub [/blue]mnuDataNextForm_Click()
frmCompute.Show
frmDataEntry.Hide
[blue]End Sub
[/blue]
 
Hi,

LPlates:

I agree that the menu validation is not needed since the validate event is what does the work I was merely following the given specs. <g>

Without the mnuclick my code is far better than yours since you have reinvented the wheel for nothing when VB functions do it, as well as you don't handle the fact that it is a control array. The code I provided does and indicates there is an error, and sets focus to the problem control.

Blanking the textbox isn't always a good thing since it may only be a mere character that is incorrect why would the user want to re-type it?

Have a good one!
BK
 
I don't think you understand the point of these forums. It is so that someone with an issue can post a sensible question, and get examples or ideas on how to solve them from like minded forum users. If you want the whole thing to be a populartiy contest, and get the most votes and stars, may I suggest this forum is not for you.

BB
 
My example allows for all types of numeric input, such as the user types in -234.32, the first character typed would be the minus or '-' sign, if you tried to validate this, you would recieve a message that the text is non numeric.
It is flexible, as for minor coding such as clearing the text etc, put a comment tag in front of the line of code or delete it.

BlackKnights example (disregarding the purpose of the code) was, in my opinion a poor example, here's a cleanup of his code...

txtWheel_Validate <-- this naming convention causes an error

Private Sub mnuDataValidate_Click()
Dim Ndx As Integer

For Ndx = txtwheel.LBound To txtwheel.UBound
If Not txtWheelValidate(Ndx) Then
MsgBox &quot;Invalid Data&quot;, vbOK + vbExclamation + _
vbDefaultButton1 + vbApplicationModal, &quot;Data Entry Form&quot;
txtwheel(Ndx).SetFocus
Exit For
End If
Next Ndx
End Sub

Private Function txtWheelValidate(Index As Integer)
txtWheelValidate = IsNumeric(txtwheel(Index).Text)
End Function

BiggerBrother
Whats it all about?
Help, advice, debate, constructive criticism and cleaner coding practices! 3 Cheers.
 
Hi,

LPlates:

Actually, no error occurs from calling an event sub (validate). I see you are trying to reinvent the validate event as well. <BG>

BB: Yes, I agree but LPlates started. ROFL!

Have a good one!
BK
 
I think both LPlates and BlackKnights solutions were very helpful. I give both of you guys a thumbs up. Thanx
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top