|
-
Sep 11th, 2005, 10:59 PM
#1
Thread Starter
Junior Member
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:
Option Explicit
Private Sub cmdamountfordwc_Click()
txtamountfordwc.Text = Format(txtcalculatedprice.Text * Val(0.05), "$0.00")
End Sub
Private Sub txtenterdognumber__KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then
Select Case KeyAscii
Case 8
Case Else
KeyAscii = 0
End Select
End If
End Sub
Private Sub cmdCalculateCharge_Click()
If txtenterdognumber.Text >= 1 Then
txtcalculatedprice.Text = Format(Val(10), "$0.00")
End If
If txtenterdognumber.Text >= 2 Then
txtcalculatedprice.Text = Format(Val(17) + ((txtenterdognumber.Text - Val(2)) * Val(5)), "$0.00")
End If
If txtenterdognumber.Text = 0 Then
Beep
MsgBox " PLEASE ENTER NUMBER OF DOGS ", vbExclamation
txtcalculatedprice.Text = "0"
txtamountfordwc.Text = "0"
End If
End Sub
Private Sub cmdclear_Click()
txtamountfordwc.Text = ""
txtcalculatedprice.Text = ""
txtenterdognumber.Text = ""
End Sub
Private Sub cmdcountall_Click()
Call cmdCalculateCharge_Click
Call cmdamountfordwc_Click
End Sub
Private Sub cmdexit_Click()
End
End Sub
Private Sub txtcalculatedprice__KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then
Select Case KeyAscii
Case 8
Case Else
KeyAscii = 0
End Select
End If
End Sub
Private Sub txtamountfordwc__KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then
Select Case KeyAscii
Case 8
Case Else
KeyAscii = 0
End Select
End If
End Sub
Private Sub txtenterdognumber_Change()
If KeyAscii < 48 Or KeyAscii > 57 Then
Select Case KeyAscii
Case 8
Case Else
KeyAscii = 0
End Select
End If
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?
-
Sep 11th, 2005, 11:17 PM
#2
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:
Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 8, 48 To 57
Exit Sub
Case Else
KeyAscii = 0
End Select
End Sub
-
Sep 11th, 2005, 11:18 PM
#3
Hyperactive Member
-
Sep 11th, 2005, 11:21 PM
#4
Thread Starter
Junior Member
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
-
Sep 11th, 2005, 11:25 PM
#5
Hyperactive Member
Rate Me! Rate Me! Rate Me!
Time to fly.
Copyright GraysonSoft Inc. 2007
-
Sep 12th, 2005, 06:39 AM
#6
Re: how to stop it letting me put it incorrect characters
 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.
-
Sep 12th, 2005, 08:24 PM
#7
Thread Starter
Junior Member
Re: how to stop it letting me put it incorrect characters
 Originally Posted by dglienna
Use the KeyPress event. This will only allow numbers and BACKSPACE.
Press TAB to exit
VB Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 8, 48 To 57
Exit Sub
Case Else
KeyAscii = 0
End Select
End Sub
I entered this in under the correct text box yet it still lets me enter the letters..why is this?!
-
Sep 12th, 2005, 08:30 PM
#8
Re: how to stop it letting me put it incorrect characters
You've probably done something wrong then because it should work as posted.
-
Sep 12th, 2005, 08:34 PM
#9
Thread Starter
Junior Member
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:
Private Sub txtcalculatedprice__KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then
Select Case KeyAscii
Case 8
Case Else
KeyAscii = 0
End Select
End If
End Sub
-
Sep 12th, 2005, 08:41 PM
#10
Re: how to stop it letting me put it incorrect characters
Why did you change the code. Your code should work but what is the error message and what line gives it?
-
Sep 12th, 2005, 08:47 PM
#11
Thread Starter
Junior Member
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:
Private Sub cmdCalculateCharge_Click()
If txtenterdognumber.Text >= 1 Then
End If
If txtenterdognumber.Text >= 2 Then
txtcalculatedprice.Text = Format(Val(17) + ((txtenterdognumber.Text - Val(2)) * Val(5)), "$0.00")
End If
If txtenterdognumber.Text = 0 Then
Beep
MsgBox " PLEASE ENTER NUMBER OF DOGS ", vbExclamation
txtcalculatedprice.Text = "0"
txtamountfordwc.Text = "0"
End If
End Sub
the line with the problem is
VB Code:
If txtenterdognumber.Text >= 1 Then
-
Sep 12th, 2005, 08:52 PM
#12
Re: how to stop it letting me put it incorrect characters
That's what you get for copying code from people who don't know what they are doing You still haven't said what the error message is.
-
Sep 12th, 2005, 08:56 PM
#13
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).
-
Sep 12th, 2005, 09:02 PM
#14
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:
Select Case txtenterdognumber.Text
Case "1"
' What do you want to do here. You aren't doing ANYTHING in your original code
Case "0"
Beep
MsgBox " PLEASE ENTER NUMBER OF DOGS ", vbExclamation
txtcalculatedprice.Text = "0"
txtamountfordwc.Text = "0"
Case Else
txtcalculatedprice.Text = Format(15 + (Val(txtenterdognumber.Text) * 5), "$0.00")
End Select
-
Sep 13th, 2005, 02:41 AM
#15
Thread Starter
Junior Member
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...
-
Sep 13th, 2005, 02:43 AM
#16
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.
-
Sep 13th, 2005, 02:47 AM
#17
Thread Starter
Junior Member
Re: how to stop it letting me put it incorrect characters
no, i read that one but it wouldnt work!
-
Sep 13th, 2005, 02:50 AM
#18
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?
-
Sep 13th, 2005, 04:06 AM
#19
Addicted Member
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
-
Sep 13th, 2005, 09:21 AM
#20
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|