|
-
Jun 18th, 2003, 09:56 AM
#1
Thread Starter
Frenzied Member
Raising Errors in a class
Hi,
In my form I have a textbox validate routine that calls the individual sets of a class module, which check the data to ensure it is within the field size limits of the database. If the data is too long, I want to raise an error and pass this back to the textbox Validate routine so I can trap the error there. Currently, the program crashes in the Class in the Set properties when the data is too big. How do I pass this error back to the form??? I've included all the code below.
VB Code:
'Here is the text box validate routine on my form
'Customer is the name of the class object I am using
Private Sub txtField_Validate(Index As Integer, Cancel As Boolean)
On Error GoTo ValidationError
Select Case Index
Case FirstName
Customer.FirstName = txtField(FirstName).Text
Case MI
Customer.MI = txtField(MI).Text
Case LastName
Customer.LastName = txtField(LastName).Text
Case Spouse
Customer.Spouse = txtField(Spouse).Text
Case Address
Customer.Address = txtField(Address).Text
Case City
Customer.City = txtField(City).Text
Case StateProv
Customer.StateProv = txtField(StateProv).Text
Case ZipPostal
Customer.ZipPostal = txtField(ZipPostal).Text
Case PhoneNumber1
Customer.PhoneNumber1 = txtField(PhoneNumber1).Text
Case PhoneNumber2
Customer.PhoneNumber2 = txtField(PhoneNumber2).Text
Case Comments
Customer.Comments = txtField(Comments).Text
Case Else
End Select
Exit Sub
ValidationError:
MsgBox Err.Number & " " & Err.Description
txtField(Index).SetFocus
End Sub
'Here is an example of a set property in my Customer class
Public Property Let FirstName(vData As String)
If Len(vData) > 20 Then
Call Err.Raise(vbObjectError + 1, "First name exceeds the maximum length for this field")
Else
mFirstName = vData
End If
End Property
-
Jun 18th, 2003, 10:11 AM
#2
Lively Member
Don't know why you're using the Call function. Using Err.Raise should be fine like this
Code:
Err.Raise vbObjectError + 1, "First name exceeds the maximum length for this field"
-
Jun 18th, 2003, 10:16 AM
#3
if you trap the error in the class using err.raise.. then you can just do something like this
VB Code:
ValidationError:
Select Case Err.Number
Case 1 + vbObjectError 'this was the error num u made in the class
MsgBox Err.Description
Case Else
'ETC
End Select
txtField(Index).SetFocus
End Sub
-
Jun 18th, 2003, 10:18 AM
#4
Originally posted by simondeutsch
Don't know why you're using the Call function. Using Err.Raise should be fine like this
Code:
Err.Raise vbObjectError + 1, "First name exceeds the maximum length for this field"
call can be used or omitted.. it doens't matter.. the only difference it makes is ( ) are required around function/sub params when you use call.. and they can't be used when you don't use call.. unless its returning a value
-
Jun 18th, 2003, 10:55 AM
#5
Thread Starter
Frenzied Member
Isn't it 'better coding practice' if the class knows nothing about the interface? I'm trying to write a n-tier demo which I can then use to copy into any new programs I write. I was under the impression that the Business Tier classes should know nothing about the Interface, and the Database Tier classes should know nothing about the Business Tier classes.
Correct me if I'm wrong.
-
Jun 18th, 2003, 11:00 AM
#6
well i suppose if you just want to display the error you could just show your error description in the error handling.. like
VB Code:
ValidationError:
MsgBox Err.Description
txtField(Index).SetFocus
End Sub
in this case it would work for you... because you just want to display the error description... in other cases you would need interaction.. because you would need to check specific error numbers and do specific things accordingly.. in that case you would need to look at error numbers
-
Jun 18th, 2003, 12:24 PM
#7
Thread Starter
Frenzied Member
Originally posted by kleinma
if you trap the error in the class using err.raise.. then you can just do something like this
VB Code:
ValidationError:
Select Case Err.Number
Case 1 + vbObjectError 'this was the error num u made in the class
MsgBox Err.Description
Case Else
'ETC
End Select
txtField(Index).SetFocus
End Sub
Maybe I'm just confused, or maybe you aren't understanding my problem. I want to be able to trap the error and set the focus to the appropriate text box (from the gui/form!!!) when an error is raised in the class. If this is or is not possible please let me know (I'm sure I've seen this before).
I don't want the class to know which text box to setfocus to, because I want this to be reused with minimal effort in other projects.
Am I making any sense?
-
Jun 18th, 2003, 12:25 PM
#8
Thread Starter
Frenzied Member
Nevermind, I looked at your code again and it makes sense now. Thanks again.
-
Jun 18th, 2003, 12:33 PM
#9
Originally posted by ae_jester
Nevermind, I looked at your code again and it makes sense now. Thanks again.
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
|