Call Event Handler

Posted on 2014-07-16
Last Modified: 2014-07-16
MsAccess 2005. I have a form with many controls that the user has to fill in. As a time saver, i've implimented the ability whereby the Ctrl->G keystroke combination gets the corresponding value from another record which the user has identified as being compatible.

So I have one routine which uses to identify the field. It then gets the value and writes it in. The thing which is missing is that I then need to call the control's AfterUpdate event which makes other things happen. So this is a simplified version of the code I would like to get working.

Public Sub GetValueFromOtherRecord
    Dim Fieldname as string
    Dim EventHandlerName as string


    EventHandlerName = FieldName & "_AfterUpdate
    Call EventHandlerName   'This is the line which needs to be fixed

End Sub

The Call statement doesn't work, presumably because the Event Name needs to be explicitly typed in my code. Is there a way I can dynamically call a procedure name?
Question by:TownTalk
    LVL 56

    Accepted Solution

    If you do:

        Me(FieldName).Text = GetOtherRecordFieldValue()

    rather than:


     You will cause the AfterUpdate event of the control to fire.

     Also, I would not use "FieldName", but rather

     Dim  strControlName as String

      Fields and controls are different things.

    LVL 48

    Expert Comment

    by:Gustav Brock
    Or you could move the code from the AfterUpdate to a separate subfunction, then call this from both the AfterUpdate of the textbox and your GetValueFromOtherRecord.

    LVL 84
    Jim's suggestion would work (although I believe you'd have to SetFocus to the control in order to work with the Text property), but take heed of gustav's comment.

    In general, if you need to run code from more than one location you should move that code to a Sub or Function and call it as needed. All too often I see code like you're describing and it can get to be a mess very quickly (at least in my opinion), and can be quite difficult to debug.

    Author Closing Comment

    Thanks Jim that works great. I had actually found another solution using CallByName, but this requires me to declare all my Event handlers as public. So your solution is better.

    I take your point about fieldname and controlname. In this case I supply the Field/ControlName to the routine which gets the the value. So it looks a bit strange strange in my Sql statement when I say "Select " & ControlName & " From ....". Ill strick with FieldName in this instance.

    Thanks again.


    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
    Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
    Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
    In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

    734 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

    17 Experts available now in Live!

    Get 1:1 Help Now