VBA Access - 如何使用当前打开的数据库设置对象

时间:2015-01-28 13:22:45

标签: vba ms-access

伙伴们如何将对象设置为当前打开的数据库 - 下面的代码查找数据库,如果找不到它则会打开它,但是如果它打开则代码会在acObj application.run调用时失效因为acObj未设置为数据库....

Set acObj = CreateObject("Access.Application")

If Len(Dir("C:\Users\" & ComputerName() & "\Documents\Components*.laccdb")) = 0 Then 'if the datebase is not running then open it
    acObj.Application.Visible = True
    acObj.OpenCurrentDatabase "C:\Users\" & ComputerName() & "\Documents\Components\Working_Blank.accdb"
    acObj.Application.UserControl = True
End If

acObj.Application.Run "Build_And_Call_LogProfile", _Name, _date

2 个答案:

答案 0 :(得分:1)

If Len(Dir("C:\Users\" & ComputerName() & "\Documents\Muncher_Components\*.laccdb")) = 0 Then
    'if the datebase is not running then open it

    Set acObj = CreateObject("Access.Application")
    acObj.OpenCurrentDatabase "C:\Users\" & ComputerName() & "\Documents\Muncher_Components\CruncherVersion_Working_Blank.accdb"
    acObj.Application.UserControl = True

    Else
    'if the datbase is then get the currenlty active database
    Set acObj = GetObject(, "Access.Application")

End If


acObj.Application.Run "Build_And_Call_LogProfile"

答案 1 :(得分:0)

事实上,您可以在开放或封闭场景中使用 GetObject()

Set acObj = GetObject("C:\Users\" & ComputerName() & "\Documents\Muncher_Components\CruncherVersion_Working_Blank.accdb")
acObj.Application.UserControl = True
acObj.Application.Run "Build_And_Call_LogProfile"