|
-
Dec 14th, 2011, 06:26 AM
#1
Thread Starter
Junior Member
[RESOLVED] Help textbox input error
when I compile and run my code then input or type a letter in txtCS_Change() I get an (run-time error "13": type mismatch)
I want to know how to create a msgbox if I input or type a letter in the texbtox
Code:
Dim X As Double
Dim b As Double
Dim c As Double
Dim d As Double
Dim e As Double
Dim a As Double
Private Sub Command2_Click()
End Sub
Private Sub cmdCOMPUTE_Click()
a = Val(txtCS.Text)
b = Val(txtHLAB.Text)
c = Val(txtLEC.Text)
d = Val(txtME.Text)
e = Val(txtQ.Text)
X = (Val(a) + Val(b) + Val(c) + Val(d) + Val(e)) / 5
txtPG = X
MsgBox " the periodic grade is " & X & "", vbInformation, "RESULT"
End Sub
Private Sub cmdRESET_Click()
txtCS = ""
txtHLAB = ""
txtLEC = ""
txtME = ""
txtPG = ""
txtQ = ""
txtCS.SetFocus
End Sub
Private Sub txtCS_Change()
If txtCS > 100 Then
MsgBox "Grade is greater than 100, Pls re-enter", vbInformation, "ERROR"
End If
End Sub
Private Sub txtHLAB_Change()
If txtHLAB > 100 Then
MsgBox "Grade is greater than 100, Pls re-enter", vbInformation, "ERROR"
End If
End Sub
Private Sub txtLEC_Change()
If txtLEC > 100 Then
MsgBox "Grade is greater than 100, Pls re-enter", vbInformation, "ERROR"
End If
End Sub
Private Sub txtME_Change()
If txtME > 100 Then
MsgBox "Grade is greater than 100, Pls re-enter", vbInformation, "ERROR"
End If
End Sub
Private Sub txtQ_Change()
If txtQ > 100 Then
MsgBox "Grade is greater than 100, Pls re-enter", vbInformation, "ERROR"
End If
End Sub
Last edited by langamer101; Dec 14th, 2011 at 06:46 AM.
-
Dec 14th, 2011, 07:31 AM
#2
Hyperactive Member
Re: Help textbox input error
Try setting the textbox DataFormat to Number this may solve your problem.
-
Dec 14th, 2011, 08:57 AM
#3
Re: Help textbox input error
Example: If txtCS > 100 Then
Above is poor coding habits. txtCS is a string and 100 is a number. You should not compare strings to numbers directly. In this case you should convert your string to a number before comparing it to 100
If Val(txtCS.Text) > 100 Then
The Val() function is handy but not ideal. I think you should be doing validation to ensure only numeric data is entered and there are plenty of examples on this forum. Search for: textbox numbers only
-
Dec 14th, 2011, 09:21 AM
#4
Re: Help textbox input error
For the other part of your question
 Originally Posted by langamer101
I want to know how to create a msgbox if I input or type a letter in the texbtox
try
vb Code:
'Place in keypress event of text box to prevent
'typing in non numeric text
Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46 And KeyAscii <> 13 Then
MsgBox "Only numbers are allowed for this entry"
KeyAscii = 0
Exit Sub
End If
End Sub
'also, in the change event put this code
'to prevent pasting in non numeric text
Private Sub Text1_Change()
If Not IsNumeric(Text1.Text) Then
"Only numbers are allowed for this entry"
Text1.Text = vbNullString
Exit Sub
End If
End Sub
-
Dec 14th, 2011, 08:33 PM
#5
Thread Starter
Junior Member
Re: [RESOLVED] Help textbox input error
thank's everybody, problem solved
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|