Further to Sergey's answer, event-driven Data Macros in Access can only call VBA functions if
- the table is being updated from Microsoft Access itself (MSACCESS.EXE), and
- the VBA code is available to the Access database file where the table update is invoked.
So, for example, if a Before Change data macro calls the following function to retrieve the current user's name (so it can insert the name into a field in that table, e.g. [CreatedBy] or [UpdatedBy])
Public Function GetUserName()
Dim wshNet As Object ' WshNetwork
Set wshNet = CreateObject("WScript.Network") ' New WshNetwork
GetUserName = wshNet.UserName
Set wshNet = Nothing
End Function
then the following conditions apply:
Updating the table from a non-Access application
A non-Access application (e.g., a .NET application using System.Data.OleDb or System.Data.Odbc) simply cannot update the table. It will throw an error saying
The function 'GetUserName' is not valid for expressions used in data macros.
Updating a linked table from an Access front-end
An Access front-end can update the table provided that the VBA code for the function is available to the front-end file (.accdb, .accde, etc.). The front-end file cannot directly run VBA code that is stored in the back-end file (where the table with the data macro is located). We need to either
- copy the VBA module(s) from the back-end file to the front-end file, or
- use a VBA reference in the front-end file to include the code from the back-end:
