Results 1 to 9 of 9

Thread: Raising Errors in a class

  1. #1

    Thread Starter
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545

    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:
    1. 'Here is the text box validate routine on my form
    2. 'Customer is the name of the class object I am using
    3.  
    4. Private Sub txtField_Validate(Index As Integer, Cancel As Boolean)
    5.     On Error GoTo ValidationError
    6.    
    7.     Select Case Index
    8.         Case FirstName
    9.             Customer.FirstName = txtField(FirstName).Text
    10.         Case MI
    11.             Customer.MI = txtField(MI).Text
    12.         Case LastName
    13.             Customer.LastName = txtField(LastName).Text
    14.         Case Spouse
    15.             Customer.Spouse = txtField(Spouse).Text
    16.         Case Address
    17.             Customer.Address = txtField(Address).Text
    18.         Case City
    19.             Customer.City = txtField(City).Text
    20.         Case StateProv
    21.             Customer.StateProv = txtField(StateProv).Text
    22.         Case ZipPostal
    23.             Customer.ZipPostal = txtField(ZipPostal).Text
    24.         Case PhoneNumber1
    25.             Customer.PhoneNumber1 = txtField(PhoneNumber1).Text
    26.         Case PhoneNumber2
    27.             Customer.PhoneNumber2 = txtField(PhoneNumber2).Text
    28.         Case Comments
    29.             Customer.Comments = txtField(Comments).Text
    30.         Case Else
    31.        
    32.     End Select
    33.    
    34.     Exit Sub
    35.    
    36. ValidationError:
    37.    
    38.     MsgBox Err.Number & " " & Err.Description
    39.     txtField(Index).SetFocus
    40. End Sub
    41.  
    42. 'Here is an example of a set property in my Customer class
    43.  
    44. Public Property Let FirstName(vData As String)
    45.     If Len(vData) > 20 Then
    46.         Call Err.Raise(vbObjectError + 1, "First name exceeds the maximum length for this field")
    47.     Else
    48.         mFirstName = vData
    49.     End If
    50. End Property

  2. #2
    Lively Member
    Join Date
    Oct 2001
    Posts
    105
    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"

  3. #3
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    if you trap the error in the class using err.raise.. then you can just do something like this
    VB Code:
    1. ValidationError:
    2.    
    3.     Select Case Err.Number
    4.         Case 1 + vbObjectError 'this was the error num u made in the class
    5.             MsgBox Err.Description
    6.         Case Else
    7.             'ETC
    8.     End Select
    9.     txtField(Index).SetFocus
    10. End Sub

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    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

  5. #5

    Thread Starter
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    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.

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    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:
    1. ValidationError:
    2.     MsgBox Err.Description
    3.     txtField(Index).SetFocus
    4.  
    5. 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

  7. #7

    Thread Starter
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    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:
    1. ValidationError:
    2.    
    3.     Select Case Err.Number
    4.         Case 1 + vbObjectError 'this was the error num u made in the class
    5.             MsgBox Err.Description
    6.         Case Else
    7.             'ETC
    8.     End Select
    9.     txtField(Index).SetFocus
    10. 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?

  8. #8

    Thread Starter
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    Nevermind, I looked at your code again and it makes sense now. Thanks again.

  9. #9
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    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
  •  



Click Here to Expand Forum to Full Width