我想在Access 2007中嵌入一段简单的VBA代码。我需要在数百个不同的数据库上执行这段代码,所以我不想手动将代码粘贴到每个数据库中。有可能做到这一点吗?也许可以作为一个附加模块?
谢谢
卡尔
编辑
我想执行以下VBA代码:
DoCmd.DeleteObject acTable, "LastNum"
DoCmd.TransferDatabase acLink, "ODBC Database", "ODBC;DSN=myDB;UID=User1;PWD=123;LANGUAGE=u s_english;" & "DATABASE=LastNumber", acTable, "LastNum", "LastNum"我该如何将其转换为VB插件?
visual studio VB外接程序模板如下所示:
imports Extensibility
imports System.Runtime.InteropServices
<GuidAttribute("B61E2444-F46E-4591-A8BA-3D06A4E5D84C"), ProgIdAttribute("MyAddin1.Connect")> _
Public Class Connect
Implements Extensibility.IDTExtensibility2
Private applicationObject As Object
Private addInInstance As Object
Public Sub OnBeginShutdown(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnBeginShutdown
End Sub
Public Sub OnAddInsUpdate(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnAddInsUpdate
End Sub
Public Sub OnStartupComplete(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnStartupComplete
End Sub
Public Sub OnDisconnection(ByVal RemoveMode As Extensibility.ext_DisconnectMode, ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnDisconnection
End Sub
Public Sub OnConnection(ByVal application As Object, ByVal connectMode As Extensibility.ext_ConnectMode, ByVal addInInst As Object, ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnConnection
applicationObject = application
addInInstance = addInInst
End Sub
End Class编辑第2部分:
好的,所以我想我应该做以下事情:
imports Extensibility
Imports System.Runtime.InteropServices
Imports Microsoft.Office.Core
Imports Access = Microsoft.Office.Interop.Access
<GuidAttribute("B61E2444-F46E-4591-A8BA-3D06A4E5D84C"), ProgIdAttribute("MyAddin1.Connect")> _
Public Class Connect
Implements Extensibility.IDTExtensibility2
Private applicationObject As Access.Application
Private addInInstance As Microsoft.Office.Core.COMAddIn
Public Sub OnBeginShutdown(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnBeginShutdown
End Sub
Public Sub OnAddInsUpdate(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnAddInsUpdate
End Sub
Public Sub OnStartupComplete(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnStartupComplete
End Sub
Public Sub OnDisconnection(ByVal RemoveMode As Extensibility.ext_DisconnectMode, ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnDisconnection
End Sub
Public Sub OnConnection(ByVal application As Object, ByVal connectMode As Extensibility.ext_ConnectMode, ByVal addInInst As Object, ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnConnection
applicationObject = CType(application, Access.Application)
addInInstance = CType(addInInst, Microsoft.Office.Core.COMAddIn)
' This line enables VBA to call back into this object.
addInInstance.Object = Me
End Sub
Public Sub ChangeLink()
applicationObject.DoCmd.DeleteObject(Access.AcObjectType.acTable, "LastPolNum")
applicationObject.DoCmd.TransferDatabase(Access.AcDataTransferType.acLink, "ODBC Database", "ODBC;DSN=ZACANTDB02;UID=EDIPolicyNumber;PWD=museum123;LANGUAGE=u s_english;" & "DATABASE=EDIPolicyNumber", Access.AcObjectType.acTable, "LastPolnum", "LastPolNum")
End Sub
End Class现在我想要的是能够在Access中执行ChangeLink()。我该怎么做?
发布于 2011-03-07 22:14:42
根据代码的用途,有(至少)两种不同的方法可以做到这一点,而不是在VB扩展级别(这是您复制和粘贴代码的地方,至少在VBA中):
与#1相比,#2的优势在于,您可以一次调用您的简单例程,而不是数百次。
编辑:
有两种方法可以调用外接程序,具体取决于调用外接程序功能的人员和方式。
通过UI执行
您仍然需要为它提供某种UI :一个命令栏、一个功能区按钮、一个窗体等等。那是一个单独的讨论
通过代码
然后,您可以直接调用该行代码,就像它是当前项目的一部分一样。如果您希望更明确,也可以通过库名调用它: MyAddInName.ChangeLink
发布于 2011-03-08 04:50:23
在我看来,您不需要从Access数据库运行此代码--您所需要做的就是在所有数据库上运行它。
为此,您将使用DAO依次打开每个数据库,删除表,然后创建链接。您必须使用DAO来完成这两个步骤(从TableDefs集合中删除并向其添加),而不是使用DoCmd操作(通过DAO无法使用)。
当然,如果无法从中心位置访问数据库,则无法执行此操作。但如果是这种情况,您将如何更改代码或调用外接程序?
使用图书馆数据库的另一种方法是使用Application.Run:
Application.Run "\Server\PathToAddIn\MyLibrary.FixTables“
这里假设:
实际上,通过该方法,您实际上可以使用DoCmd,因为外接程序是在Access中运行的,并且是在当前打开的数据库的上下文中运行的。
然而,请注意,有些人似乎很难以这种方式执行插件(我目前正在与另一个论坛中的某个人进行长时间的讨论,他似乎不能让它工作)。我已经在图书馆数据库中使用这种方法很多年了,并且没有遇到任何问题(只要你指定了一个完整的路径),所以我不明白为什么我正在讨论它的那些人似乎不能使它工作。
https://stackoverflow.com/questions/5220613
复制相似问题