This section provides tutorial example on how to add a Visual Basic code in Microsoft Access database to be executed as a macro on database tables.
As a comparison, this tutorial shows you how to write Visual Basic code and run it as a macro with Microsoft Access.
Microsoft Access is a Microsoft application that can be used to store and manage data in database tables.
Microsoft Access also supports a macro module that allows you to write macro code
with Visual Basic (VB) language.
If you have Microsoft Access installed on your Windows system, you can follow the steps below
to create a simple application in Visual Basic language within Microsoft Access.
1. Run Microsoft Access, and create a blank Access Database called vb_tutorial.mdb.
2. Click Insert > Module from the menu. The Microsoft Visual Basic window shows up.
3. Enter the following code into the empty code module:
Sub Main()
MsgBox ("Hello world! - Visual Basic in Access")
End Sub
4. Click File > Save from the menu. Enter "Hello" as the module name and save it.
5. Click Run > Run Sub/UserForm from the menu. The macro selection dialog box shows up.
6. Select "Main" macro, and click "Run". A dialog box shows up with the following message:
Hello world! - Visual Basic in Access
Congratulations. You have successfully written a Visual Basic macro in Microsoft Access!
What happened here was:
We have added a VB macro called "Hello" to our Access database, vb_tutorial.mdb.
We have added a VB procedure called "Main" in the VB macro. Access calls this procedure as a macro.
The "Main" procedure calls the "MsgBox" function, which is a VB built-in function
that displays Windows dialog box with the specified text message.
We ran the "Main" procedure and got exactly what we expected.