Results 1 to 20 of 20

Thread: how to stop it letting me put it incorrect characters

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Location
    perth
    Posts
    30

    Thumbs down how to stop it letting me put it incorrect characters

    my challenge is to create a program to calculate the cost for a particular customer according to how many dogs they have to be washed.
    1 dog = $10.00
    2 Dogs = $17.00
    3 or more Dogs = $17.00 (price for 2 dogs) + $5 for each additional dog.

    i therefore have written the following code:
    its not layed out correctly since i havent had time to fix it yet..please try and read through!


    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub cmdamountfordwc_Click()
    4. txtamountfordwc.Text = Format(txtcalculatedprice.Text * Val(0.05), "$0.00")
    5. End Sub
    6. Private Sub txtenterdognumber__KeyPress(KeyAscii As Integer)
    7.     If KeyAscii < 48 Or KeyAscii > 57 Then
    8.         Select Case KeyAscii
    9.             Case 8
    10.             Case Else
    11.                 KeyAscii = 0
    12.             End Select
    13.         End If
    14. End Sub
    15.  
    16. Private Sub cmdCalculateCharge_Click()
    17. If txtenterdognumber.Text >= 1 Then
    18. txtcalculatedprice.Text = Format(Val(10), "$0.00")
    19.  
    20.  
    21. End If
    22. If txtenterdognumber.Text >= 2 Then
    23. txtcalculatedprice.Text = Format(Val(17) + ((txtenterdognumber.Text - Val(2)) * Val(5)), "$0.00")
    24. End If
    25. If txtenterdognumber.Text = 0 Then
    26.     Beep
    27.     MsgBox "    PLEASE ENTER NUMBER OF DOGS    ", vbExclamation
    28.     txtcalculatedprice.Text = "0"
    29.     txtamountfordwc.Text = "0"
    30.    
    31.          
    32.     End If
    33. End Sub
    34.  
    35. Private Sub cmdclear_Click()
    36.     txtamountfordwc.Text = ""
    37.     txtcalculatedprice.Text = ""
    38.     txtenterdognumber.Text = ""
    39. End Sub
    40.  
    41. Private Sub cmdcountall_Click()
    42. Call cmdCalculateCharge_Click
    43. Call cmdamountfordwc_Click
    44. End Sub
    45.  
    46. Private Sub cmdexit_Click()
    47. End
    48. End Sub
    49. Private Sub txtcalculatedprice__KeyPress(KeyAscii As Integer)
    50.     If KeyAscii < 48 Or KeyAscii > 57 Then
    51.         Select Case KeyAscii
    52.             Case 8
    53.             Case Else
    54.                 KeyAscii = 0
    55.             End Select
    56.         End If
    57. End Sub
    58. Private Sub txtamountfordwc__KeyPress(KeyAscii As Integer)
    59.     If KeyAscii < 48 Or KeyAscii > 57 Then
    60.         Select Case KeyAscii
    61.             Case 8
    62.             Case Else
    63.                 KeyAscii = 0
    64.             End Select
    65.         End If
    66. End Sub
    67.  
    68. Private Sub txtenterdognumber_Change()
    69. If KeyAscii < 48 Or KeyAscii > 57 Then
    70.         Select Case KeyAscii
    71.             Case 8
    72.             Case Else
    73.                 KeyAscii = 0
    74.             End Select
    75.         End If
    76. End Sub


    the problem is i have to stop it letting me enter letters into the box, or decimal points and multiple letters and to not let you type them in. as you can see i have tried to but it just doesnt want to work. what have i done wrong?

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: how to stop it letting me put it incorrect characters

    Use the KeyPress event. This will only allow numbers and BACKSPACE.
    Press TAB to exit

    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2.   Select Case KeyAscii
    3.     Case 8, 48 To 57
    4.       Exit Sub
    5.     Case Else
    6.         KeyAscii = 0
    7.   End Select
    End Sub

  3. #3
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Thumbs up Re: how to stop it letting me put it incorrect characters

    Why not try to put your KeyAscii codes to your Textbox_KeyCode event

    instead of Command_Click events?


    Rate Me! Rate Me! Rate Me!

    Time to fly.

    Copyright GraysonSoft Inc. 2007

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Location
    perth
    Posts
    30

    Re: how to stop it letting me put it incorrect characters

    i put that into my code but it still doesnt like it. is that for vb 5? thats what i am using. it says the statement is invalid outside of type block

  5. #5
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Thumbs up Re: how to stop it letting me put it incorrect characters

    Quote Originally Posted by spazzirifick
    i put that into my code but it still doesnt like it. is that for vb 5? thats what i am using. it says the statement is invalid outside of type block
    Well, KeyCode is new to VB6 or what I heard of, so try DGLienna's suggestion.

    I tworks definitely with VB5.

    Rate Me! Rate Me! Rate Me!

    Time to fly.

    Copyright GraysonSoft Inc. 2007

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

    Re: how to stop it letting me put it incorrect characters

    Quote Originally Posted by tommygrayson
    Well, KeyCode is new to VB6 or what I heard of, so try DGLienna's suggestion.

    I tworks definitely with VB5.

    David's code will only work if you type the numbers in. You also have to code the Change event of the textbox to prevent characters other than numbers from being copy 'n pasted in.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Location
    perth
    Posts
    30

    Re: how to stop it letting me put it incorrect characters

    Quote Originally Posted by dglienna
    Use the KeyPress event. This will only allow numbers and BACKSPACE.
    Press TAB to exit

    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2.   Select Case KeyAscii
    3.     Case 8, 48 To 57
    4.       Exit Sub
    5.     Case Else
    6.         KeyAscii = 0
    7.   End Select
    8. End Sub
    I entered this in under the correct text box yet it still lets me enter the letters..why is this?!

  8. #8

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Location
    perth
    Posts
    30

    Re: how to stop it letting me put it incorrect characters

    just want to clarify
    i have put in the text below as well as the one that i showed you, but it still doesnt like it. when i say that i mean i can entrer then umbers in but also i click on my command button and i get a runtime error. i dont know what this means though.
    below is what i entered in and it still doesnt work...*looks confused*
    VB Code:
    1. Private Sub txtcalculatedprice__KeyPress(KeyAscii As Integer)
    2.     If KeyAscii < 48 Or KeyAscii > 57 Then
    3.         Select Case KeyAscii
    4.             Case 8
    5.             Case Else
    6.                 KeyAscii = 0
    7.             End Select
    8.         End If
    9. End Sub

  10. #10

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Location
    perth
    Posts
    30

    Re: how to stop it letting me put it incorrect characters

    umm i changed the code because i know other people with the same assignment who put in that code and it worked *shrugs*

    VB Code:
    1. Private Sub cmdCalculateCharge_Click()
    2.    
    3.     If txtenterdognumber.Text >= 1 Then
    4.     End If
    5.     If txtenterdognumber.Text >= 2 Then
    6.         txtcalculatedprice.Text = Format(Val(17) + ((txtenterdognumber.Text - Val(2)) * Val(5)), "$0.00")
    7.     End If
    8.     If txtenterdognumber.Text = 0 Then
    9.         Beep
    10.         MsgBox "    PLEASE ENTER NUMBER OF DOGS    ", vbExclamation
    11.         txtcalculatedprice.Text = "0"
    12.         txtamountfordwc.Text = "0"
    13.     End If
    14. End Sub

    the line with the problem is
    VB Code:
    1. If txtenterdognumber.Text >= 1 Then

  12. #12

  13. #13
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: how to stop it letting me put it incorrect characters

    This is not your problem but I just want to mention that you are using Val where you don't need to. Val is used to convert text to numbers so in the case of 17 for example, that of course is a number so just use 17 and not Val(17).

  14. #14
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: how to stop it letting me put it incorrect characters

    Here is a better way to do it. (I had to make a guess or two about what you were trying to do).

    VB Code:
    1. Select Case txtenterdognumber.Text
    2.         Case "1"
    3.             ' What do you want to do here. You aren't doing ANYTHING in your original code
    4.         Case "0"
    5.             Beep
    6.             MsgBox "    PLEASE ENTER NUMBER OF DOGS    ", vbExclamation
    7.             txtcalculatedprice.Text = "0"
    8.             txtamountfordwc.Text = "0"
    9.         Case Else
    10.             txtcalculatedprice.Text = Format(15 + (Val(txtenterdognumber.Text) * 5), "$0.00")
    11.  
    12.     End Select

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Location
    perth
    Posts
    30

    Re: how to stop it letting me put it incorrect characters

    no thats not really what i want. let me see if i can make it clearer:

    i have my textbox
    when i type into it i want it to ONLY accept numbers!

    at the moment it lets me type in anything i want...
    i would like it to give an error message if the wrong thing be typed in. do you understand me?? hmm...

  16. #16
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: how to stop it letting me put it incorrect characters

    Did you miss post #2?

    I'm not going to write you app for you, but you need to input numbers only into your textboxes, and then use them for calculations, or enter numbers only, and copy them to variables before you format them, for use in calculations.

    You cannot multiply a number that has been formatted with $ or , in them.
    Last edited by dglienna; Sep 13th, 2005 at 02:48 AM.

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Location
    perth
    Posts
    30

    Re: how to stop it letting me put it incorrect characters

    no, i read that one but it wouldnt work!

  18. #18
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: how to stop it letting me put it incorrect characters

    the problem is i have to stop it letting me enter letters into the box, or decimal points and multiple letters and to not let you type them in. as you can see i have tried to but it just doesnt want to work. what have i done wrong?
    What is the problem?

  19. #19
    Addicted Member Paradox's Avatar
    Join Date
    Sep 2004
    Location
    Nairobi
    Posts
    189

    Re: how to stop it letting me put it incorrect characters

    just to get it right, you said you Copied and pasted the code, for the keypress event to work, you have to select the event, then paste the code inside the event other wise if u paste the code inside the code editor without selecting the event it will never work.

    Try writing the code without copy paste, it works ok
    Peny wise pound Foolish

  20. #20
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: how to stop it letting me put it incorrect characters

    You must be doing something basically wrong and I think the only way we are going to find out what it is is if you put you attach your form so we can look at it.

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