Results 1 to 5 of 5

Thread: [RESOLVED] Help textbox input error

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2011
    Posts
    17

    Resolved [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.

  2. #2
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    445

    Re: Help textbox input error

    Try setting the textbox DataFormat to Number this may solve your problem.

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Help textbox input error

    For the other part of your question
    Quote Originally Posted by langamer101 View Post
    I want to know how to create a msgbox if I input or type a letter in the texbtox
    try
    vb Code:
    1. 'Place in keypress event of text box to prevent
    2. 'typing in non numeric text
    3. Private Sub Text1_KeyPress(KeyAscii As Integer)
    4. If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46 And KeyAscii <> 13 Then
    5.     MsgBox "Only numbers are allowed for this entry"
    6.     KeyAscii = 0
    7.     Exit Sub
    8. End If
    9. End Sub
    10.  
    11. 'also, in the change event put this code
    12. 'to prevent pasting in non numeric text
    13. Private Sub Text1_Change()
    14. If Not IsNumeric(Text1.Text) Then
    15.    "Only numbers are allowed for this entry"
    16.    Text1.Text = vbNullString
    17.    Exit Sub
    18. End If
    19. End Sub

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2011
    Posts
    17

    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
  •  



Click Here to Expand Forum to Full Width