|
-
Aug 12th, 2000, 08:59 PM
#1
What am I missing??? When I run the code without the If Then statements to check for valid field entries... The equation is fine.
When I drop in the If Then statements, the code will return the message box. Once the Okay button is selected instead of setting focus on the offending text box (If an entry wasn't made and the box is empty) I get a 'Runtime Error 13'. With the curPrice = txtPrice.Text highlighted once I try to Debug it.
I used the MSDN Library to look it up and it tells me something about variables not being equal.
Here is the code... Could someone shed light where I have run off the trail???
Code:
Private Sub cmdCompute_Click()
'Use calculation to determine
'total sales.
'
'curPrice is the price of product.
'intSold is number of units sold.
'curTotal is the gross sales amount.
Dim curPrice As Currency, curTotal As Currency
Dim intSold As Integer, intPress As Integer
'check for valid entry in Price field
If Val(txtPrice.Text) <= 0 Then
IntPress = MsgBox("Invalid Price. Please enter Price.", vbExclamation, "Price Request")
txtPrice.SetFocus
End If
'Check for valid entry in Sold field
If Val(txtSold.Text) <= 0 Then
IntPress = MsgBox("Please enter number of units sold.", vbExclamation, "Units Sold")
txtSold.SetFocus
End If
curPrice = txtPrice.Text
intSold = txtSold.Text
txtTotal.Text = Format(curPrice * intSold, "Currency") 'Calculate gross sales.
End Sub
Thanks...
-
Aug 12th, 2000, 11:23 PM
#2
Member
Try this
Code:
Private Sub cmdCompute_Click()
'Use calculation to determine
'total sales.
'
'curPrice is the price of product.
'intSold is number of units sold.
'curTotal is the gross sales amount.
Dim curPrice As Currency, curTotal As Currency
Dim intSold As Integer, intPress As Integer
'check for valid entry in Price field
If txtPrice.Text = "" Then
IntPress = MsgBox("Invalid Price. Please enter Price.", vbExclamation, "Price Request")
txtPrice.SetFocus
End If
'Check for valid entry in Sold field
If txtSold.Text = "" Then
IntPress = MsgBox("Please enter number of units sold.", vbExclamation, "Units Sold")
txtSold.SetFocus
End If
curPrice = txtPrice.Text
intSold = txtSold.Text
txtTotal.Text = Format(curPrice * intSold, "Currency") 'Calculate gross sales.
End Sub
[Edited by absalom on 08-13-2000 at 09:46 AM]
---~^ Absalom ^~---
There is nobody in the world who knows everything there is no one his/her workforce who knows everything what really makes the person smart is that he/she is not affraid to ask for help.
-
Aug 13th, 2000, 11:20 AM
#3
Absalom ~
I still receive a 'Runtime Error 13'. For some reason the Variables curPrice = txtPrice.Text and intSold = txtSold.Text are not compatible.
When I click on the Debug it will highlight curPrice = txtPrice.Text. Or the other depending upon which textbox I leave open to test.
Both codes shown will give the same end-result. But I am going to want to also check them with the IsNumeric eventually. Right now I am self-studying VB.
Thanks for your help though! :-)
Back to the books I guess...
-
Aug 13th, 2000, 11:47 AM
#4
Member
txtPrice.text is a string .
You will need to use the value of this string.
curPrice = val(txtPrice.Text)
-
Aug 13th, 2000, 02:33 PM
#5
Member
---~^ Absalom ^~---
There is nobody in the world who knows everything there is no one his/her workforce who knows everything what really makes the person smart is that he/she is not affraid to ask for help.
-
Aug 13th, 2000, 03:35 PM
#6
Addicted Member
Take care of your value conversions early and use the variables in your value tests (if statements).
Also, you don't need the intPress variabble since you never do anything with it. Exit the sub if one value is incorrect to avoid doing calulations with invalid data. Merely setting the focus to another control will not stop the code from executing.
Try this...
Code:
Private Sub cmdCompute_Click()
'Use calculation to determine
'total sales.
'
'curPrice is the price of product.
'intSold is number of units sold.
'curTotal is the gross sales amount.
Dim curPrice As Currency
Dim curTotal As Currency
Dim intSold As Integer
'Convert Text to proper value types
curPrice = CCur(Val(txtPrice.Text))
intSold = CInt(Val(txtSold.Text))
'check for valid entry in Price field
If curPrice <= 0 Then
MsgBox "Invalid Price. Please enter Price.", vbExclamation, "Price Request"
txtPrice.SetFocus
Exit Sub
End If
'Check for valid entry in Sold field
If intSold <= 0 Then
MsgBox "Please enter number of units sold.", vbExclamation, "Units Sold"
txtSold.SetFocus
Exit Sub
End If
txtTotal.Text = Format(curPrice * intSold, "Currency") 'Calculate gross sales.
End Sub
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
|