-
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!
-
Look at the function IsNumeric()
-
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
-
Thanks a bunch Marty!!! :)
Yes this is my first program attempted. I've always found it best to jump into something and get your hands dirty. And I can't break nothing anyhow! :D HA!
You must be able to read between my lines of code!!! 'Cause I actually do have ten fields to validate! I spent most of the last hour or two reading up on IsNumeric function, based off of the first reply to my post.
On the main form, I have three frames, each housing a particular equation. Inside each frame I also have cmdButtons to calculate the equations. I found by using the code I placed in the Module, ALL of the fields in each of the three frames were checked and validated when any of the cmdButtons were pressed.
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.
Should I place error checking codes within the each separate cmdButtons??? Or should I leave it in the Module and correct, debug and work through my problem there???
Your supplied code, and insight, has given me a better understanding of error checking already. Thanks for the time and effort on your part. :-)
-
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.
-
Thank you Marty! You've saved me hours of researching for something I wouldn't have found unless I knew what it was I was looking for! :)
BTW... I enjoyed your 'Belt Buckle' signature! :D