简体   繁体   English

如何在Access 2016中使用VBA检测对象的存在?

[英]How do I detect the presence of objects using VBA in Access 2016?

I have an Access 2016 application that gets distributed to many users who are not sophisticated users. 我有一个Access 2016应用程序,该应用程序可以分发给许多不是老练用户的用户。 They usually have to install the MS Runtime for Access. 他们通常必须安装MS Runtime for Access。 Despite clear directions, too many users still find that the application will not open. 尽管有明确的指示,但仍有太多用户发现该应用程序无法打开。 It appears that early bound objects are not present on the system. 似乎早期绑定的对象不存在于系统上。 With bound objects not present no code ever loads or runs, so it is not even possible to give a good error message. 在没有绑定对象的情况下,不会加载或运行任何代码,因此甚至不可能给出良好的错误消息。

I am now attempting to write a small program in which all the objects needed by the application are late bound, thus being able to say which modules are missing, if any. 我现在正在尝试编写一个小程序,其中应用程序需要的所有对象都在后期绑定,因此能够说出缺少的模块(如果有)。 What I am finding though is that my method for detection is failing even when I KNOW the object is present. 我发现的是即使我知道对象存在,我的检测方法仍然失败。 The code below is an example of one test for a required object. 下面的代码是对所需对象进行一项测试的示例。 This test always fails and I cannot figure out why. 该测试始终失败,我无法弄清原因。 I have about 7 of these. 我大约有7个。 Three seem to work correctly, but the others do not. 三个似乎正常工作,但其他的则不能。 Is there some different way I should be coding the "CreateObject"? 我应该对“ CreateObject”进行编码吗?

Private Sub btnOffice_Click()
    'Office    FileDialog    MSO.DLL       Microsoft Office 16.0 Object Library
    Dim obj As Object

    On Error GoTo xyzzy
    Set obj = CreateObject("Office.FileDialog")
    lblOffice.Caption = "Office module present"
    Exit Sub
xyzzy:
    lblOffice.Caption = officeWarning
    MsgBox Err.Description
End Sub

You're trying to detect broken References. 您正在尝试检测损坏的引用。 Here's a procedure to check for and report broken references: 这是检查和报告损坏引用的过程:

Sub CheckReferences()
    Dim ref As Reference

    For Each ref In References
        If ref.IsBroken Then
            MsgBox "Broken reference detected: " & vbCrLf & ref.Name & vbCrLf & ref.FullPath, vbOKOnly + vbCritical, "Broken Reference"
        End If
    Next ref
End Sub

The problem here is that the file dialog is not available as a separate COM object, and thus you can't use CreateObject() to create such an instance. 这里的问题是文件对话框不能作为单独的COM对象使用,因此您不能使用CreateObject()创建这样的实例。

However, if you plan to distribute your application without an office reference (and I think you safe to do so – even with runtime), then you can change the FileDialog code to late binding: 但是,如果您打算在没有Office参考的情况下分发您的应用程序(并且我认为您可以放心-即使在运行时也可以这样做),则可以将FileDialog代码更改为后期绑定:

Eg this: 例如:

Dim f    As FileDialog
Set f = Application.FileDialog(msoFileDialogFilePicker) 
f.Show 
MsgBox "file choose was " & f.SelectedItems(1)

Becomes this: 变成这个:

Dim f    As Object 
Set f = Application.FileDialog(3) 
f.AllowMultiSelect = True 
f.Show 
MsgBox "file choosen was " & f.SelectedItems(1)

So in your case, the filedialog is not available as a separate COM object, but you can still as above shows adopt late binding anyway. 因此,在您的情况下,无法将filedialog作为单独的COM对象使用,但是您仍然可以如上所示仍然采用后期绑定。 However, in my experience, it IS safe to distribute the runtime with a office reference and thus at least for the office dialog you don't need late binding. 但是,以我的经验,可以将运行时与Office参考一起分发是安全的,因此至少对于Office对话框,您不需要后期绑定。 For reliability, since in the case of FileDialog the late binding code is not a big deal, then I would continue distribution without the office reference for the FileDialog and use the above late binding. 出于可靠性考虑,由于对于FileDialog,后期绑定代码不是什么大问题,因此我将继续分发而无需OfficeDialog的FileDialog,并使用上述后期绑定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Access 2016 VBA中连接到新的数据源? - How do I connect to a new Datasource in Access 2016 VBA? 如何使用 VBA 在 Excel 2016 中获取筛选条件? - How do I get Filter Criteria in Excel 2016 using VBA? Access 2016 VBA:如何在公式中使用控件名称? - Access 2016 VBA: How can I use a control name in a formula? 如何在PowerPoint 2016 VBA中动态访问WebBrowser控件? - How can I access WebBrowser control dynamically in PowerPoint 2016 VBA? 如何使用MS Access 2016在VBA表达式中编写数字字段? - How to write a numeric field in a VBA expression using MS Access 2016? 如何使用VBA将Excel 2016数据导出到另一个电子表格中? - How do I export Excel 2016 data into another spreadsheet using VBA? 如何在Excel 2016中调试普通的vba函数? - How do I debug a trivial vba function in Excel 2016? MS Access 2016,VBA:如何在不按 F5 的情况下删除记录后更新/刷新显示 #Deleted 的文本框控件? - MS Access 2016, VBA: How do I update/refresh Textbox controls that display #Deleted after deleting a record without pressing F5? 使用VBA将Access 2016链接到Excel工作表 - Linking Access 2016 to excel sheet using VBA Access-VBA:如何在TreeView-NodeClick中检测鼠标键 - Access-VBA: How do I detect the mouse-key in a TreeView-NodeClick
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM