Search code examples
vbams-accessms-access-2013linked-tables

Adding a Macro to AfterUpdate Event on Linked Table using VBA


I'm wondering is there any way to add a macro to the AfterUpdate Table Event in VBA?

    Dim Db As Database, tDef As TableDef
    Set Db = CurrentDb
    Set tDef = Db.CreateTableDef("ARCH_CALL")      
    tDef.Connect = BuildLinkString(UserName)
    tDef.SourceTableName = CreateDataSourceString(BuildDataSourceString, "LinkedTableName")
    Db.TableDefs.Append tDef

I re-create the linked table after each login, so I need to re-associate the AfterUpdate Table Event as well.


Solution

  • DoCmd.RunMacro "MacroName" 
    

    should do the trick. Just tack it on at the end of the AfterUpdate event.

    EDIT:

    Why not just change the link instead of creating a new table? Then you don't have to reassign a macro using VBA.

    Set db = CurrentDB
    Set tdf = db.TableDefs("ARCH_CALL")
    tdf.Connect = "ODBC;DRIVER=SQL Server;SERVER=10.0.0.1;" _
      & "UID=testdb;APP=Microsoft Office 2003;WSID=abc;TABLE=dbo.ARCH_CALL"
    tdf.RefreshLink