要在Access打开时运行Access脚本,请确保在标准模块中有一个函数(而不是sub)。
Option Compare Database
'------------------------------------------------------------
' AutoExec
'
'------------------------------------------------------------
Function AutoExec()
On Error GoTo AutoExec_Err
DoCmd.RunCommand acCmdWindowHide
MsgBox "Welcome to the client billing application!", vbOKOnly, "Welcome"
DoCmd.OpenTable "Orders", acViewNormal, acEdit
AutoExec_Exit:
Exit Function
AutoExec_Err:
MsgBox Error$
Resume AutoExec_Exit
End Function
请记住,有几种方法可以控制Excel的访问。这里有一个选择。
Global oApp As Object
Sub OpenAccess()
Dim LPath As String
Dim LCategoryID As Long
'Path to Access database
LPath = "C:\Users\Excel\Desktop\Coding\Microsoft Access\Northwind.mdb"
'Open Access and make visible
Set oApp = CreateObject("Access.Application")
oApp.Visible = True
'Open Access database as defined by LPath variable
oApp.OpenCurrentDatabase LPath
'Open form of interest
oApp.DoCmd.OpenForm "Form1"
End Sub