|
-
Jun 16th, 2000, 05:24 AM
#1
I'm trudging along, learning VB as I go, and am designing a sample program as I read through a VB manual.
My program is basically a "calculator" for computing response rates, gross sales, overhead costs, etc., for Direct Marketing people.
I have placed an ErrorCheck within a Module to be called from the form.
Code:
'Error-Checking
If ErrorCheck() = 1 Then
Exit Sub
End If
In the module I have written:
Code:
Public Function ErrorCheck() As Integer
Dim IntPress As Single
'Error-checking for Gross Sales.
If Val(frmQuick.txtPrice.Text) <= 0 Then
IntPress = MsgBox("Enter a value for the price", vbExclamation, "Gross Sales")
frmQuick.txtPrice.SetFocus
ErrorCheck = 1
Exit Function
End If
If Val(frmQuick.txtSold.Text) <= 0 Then
IntPress = MsgBox("Enter number of units sold", vbExclamation, "Gross Sales")
frmQuick.txtSold.SetFocus
ErrorCheck = 1
Exit Function
End If
'No error occurred if execution gets here
ErrorCheck = 0
End Function
When the field is blank, the MsgBox and set focus works fine. When the field begins with an alpha character (in case of an inadvertent entry error), then a number, it works fine.
But when I enter a numeral followed by an alpha character I recieve a Runtime Error '13', Type Mismatch. 
Where have I missed this??? Obviously some code checking for Alpha Characters???
Thanks for any assistance!
-
Jun 16th, 2000, 05:32 AM
#2
Hyperactive Member
Look at the function IsNumeric()
-
Jun 16th, 2000, 06:31 AM
#3
First let me say that for someone reading from a manual for the first time (I assume), your doing very well.
Here are some things you might want to consider:
Most programmers would assume that IntPress was an Integer since it starts with "Int", so you should either change it to an integer or rename it sglPress (note the the prefix is usually written with all lower case).
You actually don't need IntPress at all. MsgBox and other functions can be called either the way you have which expects a returned value, or like MsgBox "Enter a value for the price", vbExclamation, "Gross Sales" which doesn't. Since you are not using the returned value, you can use the second form.
Validating fields one by one like you have done certainly works, but let's say you were a user of a program that validated 10 fields one by one and you made 10 mistakes. Wouldn't you get annoyed receiving 10 seperate error messages? Here's one of several approahes you can take instead:
Code:
Dim strError As String
If Val(frmQuick.txtPrice.Text) <= 0 Or _
Not IsNumeric(frmQuick.txtPrice.Text) Then
strError = "Price is not valid. Enter a value for the price"
frmQuick.txtPrice.ForeColor = vbRed
Else
frmQuick.txtPrice.ForeColor = vbBlack
End If
If Val(frmQuick.txtSold.Text) <= 0 Or _
Not IsNumeric(frmQuick.txtSold.Text) Then
strError = strError & vbCrLf & vbCrLf _
& "Units sold is not valid. Enter number of units sold"
frmQuick.txtSold.ForeColor = vbRed
Else
frmQuick.txtSold.ForeColor = vbBlack
End If
If strError <> "" Then
MsgBox strError, vbExclamation, "Errors Found"
Exit Function
End If
-
Jun 16th, 2000, 07:06 AM
#4
-
Jun 16th, 2000, 09:20 AM
#5
I'm not 100% sure I understand what "Being able to validate the fields inside a certain frame on the same main form, from which the cmdButton is clicked, is where I want to go with this program" means, but if you want each frame's calculations done seperately then create 3 ErrorCheck routines, each a with unique name. For example Frame1's error routine might be called ValidateFrame1, etc. Then call the appropriate routine from the command button in the frame. If however you want a one-button validation of all the fields then you should have only one command button and that should either call 1 routine that validates all the fields, or you could have it call the 3 seperate routines I suggested above.
-
Jun 16th, 2000, 09:51 AM
#6
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
|