Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2009
    Posts
    89

    Unanswered: Trouble making a form Option Explicit with Cancel = True

    HI. I just found ms access's performance analyser and ran it. IT complained for all my forms that I hadnt set option explicit as an option at the top.

    so I did.

    but now some forms won't compile. This is where I've used "Cancel = True" in a procedure behind a button's; click event.

    for example@
    Code:
    Private Sub CancelAndClose_Click()
       On Error GoTo CancelAndClose_Click_Error
    
        Cancel = True
        DoCmd.Close
    
       On Error GoTo 0
       Exit Sub
    
    CancelAndClose_Click_Error:
    Call ErrorLog(Err.Description, Err.Number, Me.Name)
        
        'MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure CancelAndClose_Click of VBA Document Form_Landlord Details"
    End Sub
    If I put "Cancel As Boolean" in teh procedure's brackets, it says "Procedure declaration does not match description of event or procedure having the same name".

    How can I get round this? or is option explicit nto really nevessary?

  2. #2
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Basically, using Option Explicit is very good practice. It will cause some code to stop compiling and it will also expose bugs, as per here.

    The Cancel = True was doing nothing in your original code. Some event procedures have a ByRef parameter of Cancel. You can therefore cancel the handler by using Cancel = True. However, not all events have this parameter. This is one of them.

    Remove the "Cancel = True" bit - I bet your code works exactly as it did before, yes?
    Testimonial:
    pootle flump
    ur codings are working excelent.

  3. #3
    Join Date
    Jun 2009
    Posts
    89
    THe problem I was having was that users were filling out half the form, and then closing it. THis caused errors if they didn't fill in a required field.

    So I craeted a "cancel and close" button. This undoes all the changes on the form, adn then closes it, so access won't try and insert a new record with missing information.

    I can replace it with docmd.Undo, and that works, but is it exactly the same as cancel = true?
    Last edited by rudeboymcc; 10-15-09 at 13:05.

  4. #4
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    No it isn't! Cancel = True means that the event is canceled! Some events cannot be canceled, OnClick being one of them! How could you cancel a click? And why would you want to, in this case? You don't want to Cancel dumping the data and closing the form, do you? That's what the event is intended to do!

    DoCmd.Undo is the way to go here!
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  5. #5
    Join Date
    Jun 2009
    Posts
    89
    ah! you've just explained a lot of my "bugs" :-(

    Ok here;s another question. Say I have edited a record, and have changed it so that it is now invalid (I.e. a required field is blank). I click close without saving it first, and when closing hte beforeupdate event wll be called as the form is dirty.

    In the beforeupdate() event, i check the fields, and if one is null, I call Cancel = True (to stop the record from updating).

    would this also cancel the close event? or just the update event?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •