Error messages cause program not to work

Posted on 2006-05-06
Last Modified: 2010-04-23

I have used the data connection wizard to connect VB.NET to my Access database - the result is a success. However when I try to add error messages like this:

If editCType.Text = "" Then
            MsgBox("Contact type cannot be blank", MsgBoxStyle.Information, "Error")
End If

the program no longer works because a second error message appears - that I did not create and this causes the program to stop.  

There is already some code in the program which appears to be designed to catch errors but I don't know how to use it correctly - it looks like this:

            'Attempt to load the dataset.
        Catch eLoad As System.Exception
            'Add your error handling code here.
            'Display error message, if any.
End Try

Any suggestions?
Question by:tuck1996
    LVL 52

    Expert Comment

    by:Carl Tawn
    What error are you getting ?
    LVL 34

    Expert Comment

    As carl_tawn says, we really need to know what error message you are getting and, ideally, on which line it is occurring.  But here's a couple of comments.

    Yes, that bit of code you quoted is "designed to catch errors" but it only does so with the code placed between Try and Catch.  So, in the specific code you quoted it means that if, and only if, there is an error when the program tries to execute the line


    that error will be "caught" and the message box will display a message about what it is.  If you want similar things to happen for errors with any other code, you will need to put that code after a Try and then put code to say what you want to happen after  a Catch and then put  an End Try.

    One possible source of an unhandled error is in the last line of the code you show


    It depends what objContactDetailDataSet_PositionChanged refers to, but it looks suspiciously like a sub to handle a binding manager's (probably a CurrencyManager's) PositionChanged Event.  And, if it is a call to such a sub, and the sub is in the standard form, it needs to be called with the arguments (sender As Object, e As EventArgs).

    Finally, where does the code you show at the start come in your program?  I doubt this is the case with you, as your program would just have appeared to freeze rather than reporting an error, but if it were in the code handling the textbox's GotFocus Event, it would be circular.  Every time the last line of your code reset the focus to the textbox it would fire again.



    Author Comment

    The field 'Contact.CType' cannot contain a Null value because the Required property for this field is set to True.  Enter a value in this field.  I did resolve this at one point but then I could not get the form to write back to the database.
    LVL 34

    Accepted Solution

    That is happening because your program is attempting to commit the value in editCType.Text to the datatable (and/or from the datatable to the database) before your code has checked whether it has a valid value in it.  It's difficult to suggest in detail how you should change things to overcome this without knowing much more about your program's structure.  But have a look at this


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    This video discusses moving either the default database or any database to a new volume.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now