Results 1 to 13 of 13

Thread: MsgBox - Unload Form Predicament

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2003
    Posts
    140

    MsgBox - Unload Form Predicament

    Okay, I have the following code in my Form_Load
    Code:
    If datanavigator.Recordset.RecordCount = 0 Then
    MsgBox "There are No Invoices to be Viewed/Printed!", vbCritical, "No Records"
    If vbOK = True Then
    Unload Me
    End If
    End If
    So it basically checks if the ADO (datanavigator) is empty and if it is, it brings up the message box, and when the person clicks Ok, it unloads the form, but here is the problem.

    Using the code above, when the person clicks the ok, the message box stays there regardless of how many times u press ok and the app crashes.

    How can I upload the form?

  2. #2
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    it should work if you remove the inner if condition - just let them click OK and then unload the form.
    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

  3. #3
    Fanatic Member demotivater's Avatar
    Join Date
    Jun 2002
    Location
    is everything
    Posts
    627
    Try this instead

    VB Code:
    1. Call MsgBox("There are no invoices...", vbExclamation + vbDefaultButton1, "No records")
    2. Unload Me

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Dec 2003
    Posts
    140
    tried both ways - still dies!

  5. #5
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246
    Have you tried using just End?
    VB Code:
    1. If datanavigator.Recordset.RecordCount = 0 Then
    2. MsgBox "There are No Invoices to be Viewed/Printed!", vbCritical, "No Records"
    3. End
    4. End If

    Phreak

    Visual Studio 6, Visual Studio.NET 2005, MASM

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Dec 2003
    Posts
    140
    well at least with the "end" the app doesn't crash, but the whole app gets closed down, which is not what i want - btw the form in question is part of a mdiform, so i don't want the whole thing to get closed down!

  7. #7
    Fanatic Member TheVader's Avatar
    Join Date
    Oct 2002
    Location
    Rotterdam, the Netherlands
    Posts
    871
    It looks like the problem has more to do with closing the form that with handling the messagebox call. To see if this is right, place an empty MessageBox call in the Form_Unload event (MsgBox ""). If your app crashes after clicking the OK button of both boxes the problem lies with the unloading.

    In that case, make sure you close everything: set all objects to nothing etc.
    Author for Visual Basic Web Magazine

    My articles on the Web Browser Control:
    Using the Web Browser Control & Using the DHTML Document Object Model

    The examples referenced in the articles can be found here:

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Dec 2003
    Posts
    140
    okay, i tried the thing in form_unload and it closed fine - no problems there!

  9. #9
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    Hmmm.... and I thought you can't unload a form in its load event. What I usually do is set a flag in the load event then unload the form in the activate event if the flag is true.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Dec 2003
    Posts
    140
    how do i do that?

  11. #11
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    VB Code:
    1. Private booForceUnload As Boolean  'module level
    2.  
    3. Private Sub Form_Load()
    4.  
    5. booForceUnlaod = False
    6. If datanavigator.Recordset.RecordCount = 0 Then
    7.    If vbOK = MsgBox ("There are No Invoices to be Viewed/Printed!", vbCritical + vbOkOnly, "No Records") Then
    8.       booForceUnload = True
    9.    End If
    10. End If
    11. '
    12. '
    13. End Sub
    14.  
    15. Private Sub Form_Activate()
    16.    If booForceUnload = True Then
    17.       Unload Me
    18.       Exit Sub
    19.    End If
    20. '
    21. '
    22. '
    23. End Sub
    24.  
    25. Private Sub Form_Unload()
    26.    booForceUnload = False  'reset. just in case for succeeding form load
    27. End Sub
    Last edited by leinad31; Apr 12th, 2004 at 12:16 PM.

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104
    You can unload in the load event, it will just raise an error. One possible solution is to catch that error and exit the sub.

  13. #13
    New Member
    Join Date
    Apr 2004
    Location
    Toronto Canada
    Posts
    14
    You could also check that condition before you allow the form to load. That would also look cleaner to the end user.

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