2017-04-20 82 views
0

访问VBA对象模型,我看不到任何选项来启用Microsoft Access中的VBA对象模型,因为这打印屏幕上:连接到从Excel

enter image description here

从Excel中我可以在同见选项对话窗口:

enter image description here

我知道下面的工作正常访问VBA对象模型工作

 
Public strAllCode As String 

Public Sub StringAllLines() 

    Dim accObj As AccessObject 'Each module/form/report. 
    Dim bWasOpen As Boolean  'Flag to leave form/report open if it was open. 
    Dim strDoc As String  'Name of each form/report 

    'Stand-alone modules. 
    For Each accObj In CurrentProject.AllModules 
     Call GetModuleLines(accObj.Name, True) 
    Next 

    'Modules behind forms. 
    For Each accObj In CurrentProject.AllForms 
     strDoc = accObj.Name 
    bWasOpen = accObj.IsLoaded 

    If Not bWasOpen Then 
     DoCmd.OpenForm strDoc, acDesign, WindowMode:=acHidden 
    End If 

    If Forms(strDoc).HasModule Then 
     Call GetModuleLines("Form_" & accObj.Name, False) 
    End If 

    If Not bWasOpen Then 
     DoCmd.Close acForm, strDoc, acSaveNo 
    End If 

    Next 

    'Modules behind reports. 
    For Each accObj In CurrentProject.AllReports 
    strDoc = accObj.Name 
    bWasOpen = accObj.IsLoaded 

    If Not bWasOpen Then 
    'In Access 2000, remove the ", WindowMode:=acHidden" from the next line. 
     DoCmd.OpenReport strDoc, acDesign, WindowMode:=acHidden 
    End If 

    If Reports(strDoc).HasModule Then 
     Call GetModuleLines("Report_" & accObj.Name, False) 
    End If 

    If Not bWasOpen Then 
     DoCmd.Close acReport, strDoc, acSaveNo 
    End If 

    Next 

    Dim intFile As Integer 

    '*** Set to next free open number *** 
    intFile = FreeFile() 
    Open "c:\temp\AllCode.txt" For Output As #intFile 
    Print #intFile, strAllCode 
    Close #intFile 

End Sub 

Private Function GetModuleLines(strModule As String, bIsStandAlone As Boolean) 

    Dim bWasOpen As Boolean  'Flag applies to standalone modules only. 
    Dim lngLineNo As Long 

    If bIsStandAlone Then 
     bWasOpen = CurrentProject.AllModules(strModule).IsLoaded 
    End If 

    If Not bWasOpen Then 
     DoCmd.OpenModule strModule 
    End If 

    strAllCode = strAllCode & "**********" & vbCrLf 
    strAllCode = strAllCode & strModule & vbCrLf 
    strAllCode = strAllCode & "**********" & vbCrLf 

    For lngLineNo = 1 To Modules(strModule).CountOfLines 
    strAllCode = strAllCode & Right("  " & lngLineNo, 5) & ": " & Modules(strModule).Lines(lngLineNo, 1) & vbCrLf 
    Next 

    strAllCode = strAllCode & vbCrLf & vbCrLf & vbCrLf 

    If Not bWasOpen Then 
     On Error Resume Next 
     DoCmd.Close acModule, strModule, acSaveNo 
    End If 

    Debug.Print strModule & " complete" 

    DoEvents 

End Function 

以上只是一个代码示例,允许将所有模块和对象名称从Access中导出到文本文件。

我想从Excel中执行它,换句话说,Excel中的vba宏打开Access数据库并将每个对象的名称和类型(表格,表格,查询,报告,vba模块...)文本文件,然后关闭数据库。这可以做到吗?

reference的代码发布

回答

0

你可以只写你的代码,你知道接入内工作,并运行从Excel宏?

不是很动态的,但将成为你的目的

Set oAcc = CreateObject("Access.Application") 

oacc.opencurrentdatabase "C:\yourdb.accdb", ,"password" 
oacc.Run "Your_Macro" 
+0

我想宏从Excel运行并保持Access文件不变,谢谢 – pascalb

1

添加在Excel中VBE的Microsoft Access中XX.X对象参考库将公开的方法,参数和功能/潜艇在Access中,你可以调用从Excel中。

添加引用后,添加Access.Application类型的对象,然后使用Set命令将其设置为等于Access.Application对象的新实例。

然后,您可以将变量绑定到数据集,查询/表定义,并且几乎可以在Access中执行任何操作。这被称为早期绑定。或者,您可以创建一个未绑定的对象变量(Dim foo As Object),然后将其设置为等于对象引用(Set foo = New Access.Application)。

您可以像以前一样创建和操作相同的结构,但会失去Intellisense功能。这被称为后期绑定。

0

好的我很懒,在询问之前做了这个实验,但从上面首次发布的代码做了一些调整,可以完全控制访问vba对象模型。请参阅下面的代码我的代码(第一次尝试),我想出了和它的作品:

 
Public strAllCode As String 
Public appAccess As Object 

Public Sub StringAllLines() 

    'create new access object 
    Set appAccess = CreateObject("Access.Application") 
    'open the acces project 
    Call appAccess.OpenCurrentDatabase(_ 
    "D:\ShF3\vba_list_module_proj\access northw modules _4_ok\Northwind 2007z.accdb") 
    appAccess.Visible = False 

    Dim accObj As Object 'Each module/form/report. 
    Dim bWasOpen As Boolean  'Flag to leave form/report open if it was open. 
    Dim strDoc As String  'Name of each form/report 
    'Stand-alone modules. 
    For Each accObj In appAccess.CurrentProject.AllModules 
     Call GetModuleLines(accObj.Name, True) 
    Next 
    'Modules behind forms. 
    For Each accObj In appAccess.CurrentProject.AllForms 
     strDoc = accObj.Name 
     bWasOpen = accObj.IsLoaded 
     If Not bWasOpen Then 
      'DoCmd.OpenForm strDoc, acDesign, WindowMode:=acHidden 
     End If 

    Next 

    'Modules behind reports. 
    Dim intFile As Integer 
    '*** Set to next free open number *** 
    intFile = FreeFile() 
    Open "c:\temp\AllCode.txt" For Output As #intFile 
    Print #intFile, strAllCode 
    Close #intFile 

End Sub 


Private Function GetModuleLines(strModule As String, bIsStandAlone As Boolean) 

    Dim bWasOpen As Boolean  'Flag applies to standalone modules only. 
    Dim lngLineNo As Long 
    If bIsStandAlone Then 
     bWasOpen = appAccess.CurrentProject.AllModules(strModule).IsLoaded 
    End If 
    If Not bWasOpen Then 
     appAccess.DoCmd.OpenModule strModule 
    End If 
    strAllCode = strAllCode & "**********" & vbCrLf 
    strAllCode = strAllCode & strModule & vbCrLf 
    strAllCode = strAllCode & "**********" & vbCrLf 

    For lngLineNo = 1 To appAccess.Modules(strModule).CountOfLines 
     strAllCode = strAllCode & Right("  " & lngLineNo, 5) & ": " & appAccess.Modules(strModule).Lines(lngLineNo, 1) & vbCrLf 
    Next 

    strAllCode = strAllCode & vbCrLf & vbCrLf & vbCrLf 

    If Not bWasOpen Then 
     On Error Resume Next 
     'appAccess.DoCmd.Close acModule, strModule, acSaveNo 
    End If 

    Debug.Print strModule & " complete" 
    DoEvents 

End Function 

我只需要正确地关闭仍保持开放的Access数据库,并做了一些调整,在文本文件中报告的信息。最后,我不需要将文本文件作为中间步骤,而是将所有内容都放在excel中。 Pascal