Results 1 to 2 of 2
  1. #1
    pdanes is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    Sep 2019
    Posts
    208

    Reference error from custom event

    I'm trying to use custom events more, and running into some rather odd problems.

    Main form has a subform with some associated records. In the main form's OnCurrent event, I call a public routine in the main form's code that does some display housekeeping around the subform's contents. All has worked well for years.

    From the main form, a dialog form can be called that affects the set of associated records. When I designed this, I did not know anything about custom events, so I simply requeried the subform when the dialog form closed. Later, I added some flags to requery only when the dialog form had actually made some changes. Again, all worked well.



    Now, I replaced the flags and requery on exit with calls to a custom event handler. That event handler is called directly from the dialog form, at the moment when the changes are executed in the dialog form. The call works, but the display housekeeping routine bombs with error 2467, that the requested object does not exist. It does, obviously – the main form is open and the subform is perfectly intact. Some sleuthing uncovered what I consider strange behavior: I need a reference to the main form in my code, despite this code residing entirely within the main form.

    That is, I refer to a control on the main form with an expression like cmdUdelatMatrixZeZaznamu for a command button and sfMatrixContainer.Form.Recordset.RecordCount to check the number of records in the subform. That works perfectly when the code is executed from the main form's OnCurrent event, but that same code throws an error when activated by the raised custom event from the dialog form. Some experimenting determined that I have to use a previously set reference to the main form the make this code work from the event call.

    Instead of
    Code:
    cmdUdelatMatrixZeZaznamu
    sfMatrixContainer.Form.Recordset.RecordCount
    I must use
    Code:
    ZNForm.cmdUdelatMatrixZeZaznamu
    ZNForm.sfMatrixContainer.Form.Recordset.RecordCount
    Self-references
    Code:
    Me.cmdUdelatMatrixZeZaznamu
    Me.sfMatrixContainer.Form.Recordset.RecordCount
    also do not work – they throw the same error.

    Is this normal for custom events? When I call that same routine directly from the dialog form, this does not happen – the references to controls in the main form and subform work normally. However, this requires the dialog form to know how to communicate back with the main form, which is somewhat against what is considered good practice these days. The custom event lets the dialog form simply announce to the world what has happened, and whoever wants can do whatever they want about it. The called routine acts as if it was not in the main form, and does not know about its contents. Requiring such a routine to use external references to its own contents seems rather back-asswards to me.

    I suppose one thing I could do would be to move the routine to a separate module, out of the main form, and then external references to the main form would not look so strange, but I don't understand why the external reference is necessary for code inside the main form, and ONLY when called from an event handler, but not when called directly.

    Can anyone shed some light on this?

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,858
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Custom Progress Bar On Load Event and On Timer
    By Desstro in forum Programming
    Replies: 16
    Last Post: 03-25-2018, 06:10 PM
  2. Replies: 17
    Last Post: 05-17-2017, 11:59 AM
  3. Replies: 7
    Last Post: 03-11-2017, 11:00 AM
  4. Replies: 5
    Last Post: 05-15-2015, 10:49 AM
  5. Replies: 7
    Last Post: 05-08-2014, 10:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums