在 VBA 中,不能使用 Access.Application 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1049451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 10:32:02  来源:igfitidea点击:

In VBA, cannot use Access.Application object

ms-accessvbaregistry

提问by PowerUser

This does NOT work:

这不起作用:

Sub X()
    Dim A As Access.Application
    Set A = CreateObject("Access.Application")
    'Do Stuff
End Sub

However, this DOES work:

但是,这确实有效:

Sub X()
    Dim A As Object
    Set A = CreateObject("Access.Application")
    'Do Stuff
End Sub

I know they do virtually the same thing, but can anyone tell me how to make an access.application object? I should add that I have Crystal Reports 11 and on my last upgrade, it may have 'unregistered' some VBA DLLs.

我知道他们几乎做同样的事情,但谁能告诉我如何制作 access.application 对象?我应该补充一点,我有 Crystal Reports 11,在我上次升级时,它可能“未注册”了一些 VBA DLL。

(Update 2009-06-29)

(更新 2009-06-29)

In response to the first 2 questions, I am using MS Access VBA to control some other Access & Excel files. Since this will only ever run on my local machine, I can guarantee that Access will always be installed. I have also referenced the "Microsoft Access 11.0 Object Library" (MSACC.OLB).

针对前两个问题,我使用 MS Access VBA 来控制其他一些 Access & Excel 文件。由于这只会在我的本地机器上运行,我可以保证始终安装 Access。我还参考了“Microsoft Access 11.0 对象库”(MSACC.OLB)。

I know there's ways around this, i.e. use early binding when coding, and switch to late binding when running it, I just don't understand why the early binding method doesn't work at all on my machine (Of course, the code works fine on another machine with Access).

我知道有一些方法可以解决这个问题,即在编码时使用早期绑定,并在运行时切换到后期绑定,我只是不明白为什么早期绑定方法在我的机器上根本不起作用(当然,代码有效在另一台带有 Access 的机器上很好)。

采纳答案by Oorang

Hello,
The code that you say is not working is legal syntax. What error are you getting? When does it occur? Do you know the line of code it happens at?

您好,
您说的代码不起作用是合法的语法。你遇到了什么错误?它什么时候发生?你知道它发生在哪一行代码吗?

Just as a side note, this is legal syntax as well:

顺便提一下,这也是合法的语法:

    Dim accApp As Access.Application
    Set accApp = New Access.Application

But to be clear, the CreateObject Syntax is legal and not the source of the problem.

但需要明确的是,CreateObject 语法是合法的,而不是问题的根源。

回答by DJ.

If you are writing this in Access there is no need to do that as the Application object is already there for you. If you are writing this in Excel or Word then you need to add a reference to the Access Library. Go to Tools/References and look for Microsoft Access XX Object Library

如果您在 Access 中编写此内容,则无需这样做,因为 Application 对象已经为您准备好了。如果您在 Excel 或 Word 中编写此内容,则需要添加对 Access Library 的引用。转到工具/参考并查找 Microsoft Access XX 对象库

回答by Brian Battles

Try Detect And Repair from the Help menu in MS Access. Worked perfect for me.

从 MS Access 的“帮助”菜单尝试“检测和修复”。非常适合我。