首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将参考库添加到外部MS Access数据库

将参考库添加到外部MS Access数据库
EN

Stack Overflow用户
提问于 2020-05-30 02:02:57
回答 1查看 321关注 0票数 1

我有一个创建新的MS Access数据库的代码。我想将参考库添加到这些新创建的MS数据库中。下面是我编写的代码,但不起作用:

代码语言:javascript
复制
Sub makeDb(fl As String)    
Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")

'check if the file already exists

If fs.FileExists(fl) = False Then

    'create new ms access database

    Dim accessApp As Access.Application
    Set accessApp = New Access.Application
    accessApp.DBEngine.CreateDatabase fl, dbLangGeneral

    'loop through all references in current database and add them to the newly created dbs

    Dim cur_vbProj As VBIDE.VBProject: Set cur_vbProj = Application.VBE.VBProjects(1)
    Dim cur_vbRefs As VBIDE.References: Set cur_vbRefs = cur_vbProj.References
    Dim cur_vbRef As VBIDE.Reference

    For Each cur_vbRef In cur_vbRefs
        Dim cur_guid As String: cur_guid = cur_vbRef.Guid
        Dim cur_major As Long: cur_major = cur_vbRef.Major
        Dim cur_minor As Long: cur_minor = cur_vbRef.Minor

        'here is the code that doesn't work

        Dim vbProj As VBIDE.VBProject: Set vbProj = accessApp.Application.VBE.VBProjects(1)
        Dim vbRefs As VBIDE.References: Set vbRefs = vbProj.References
        vbRefs.AddFromGuid Guid:=cur_guid, Major:=cur_major, Minor:=cur_minor

    Next

    accessApp.Quit
    Set accessApp = Nothing

End If

End Sub

Set vbProj = accessApp.Application.VBE.VBProjects(1)将运行时错误'9‘子脚本抛出范围之外。我应该如何修改代码?是否可以添加对外部数据库的引用?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-30 19:32:29

以下是我的作品:

代码语言:javascript
复制
Sub makeDb(f1 As String)
Dim accApp As Access.Application
Dim cur_vbRefs As References
Dim cur_vbRef As Reference
If Dir(f1) = "" Then
    Access.DBEngine.CreateDatabase f1, dbLangGeneral
    Set accApp = New Access.Application
    accApp.OpenCurrentDatabase f1
    'loop through all references in current database and add them to the newly created dbs
    Set cur_vbRefs = Application.References
    For Each cur_vbRef In cur_vbRefs
        On Error Resume Next
        accApp.References.AddFromGuid cur_vbRef.Guid, cur_vbRef.Major, cur_vbRef.Minor
    Next
End If
End Sub
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62096948

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档