Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating global VBA code in Access
    primarykey
    data
    text
    <p>I want to embed a simple piece of VBA Code in Access 2007. I need to execute this code on hundreds of different Access DBs, so I don't want to manually paste the code into each and every DB. Is it possible to do this? Maybe by way of an add-in?</p> <p>Thanks<br> Karl</p> <p>EDIT</p> <p>I want to execute the following VBA code:</p> <pre><code>DoCmd.DeleteObject acTable, "LastNum" DoCmd.TransferDatabase acLink, "ODBC Database", "ODBC;DSN=myDB;UID=User1;PWD=123;LANGUAGE=u s_english;" &amp; "DATABASE=LastNumber", acTable, "LastNum", "LastNum" </code></pre> <p>How would I translate this into a VB addin?</p> <p>The visual studio VB add-in template looks like this:</p> <pre><code>imports Extensibility imports System.Runtime.InteropServices &lt;GuidAttribute("B61E2444-F46E-4591-A8BA-3D06A4E5D84C"), ProgIdAttribute("MyAddin1.Connect")&gt; _ 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 </code></pre> <p>EDIT PART 2:</p> <p>Ok, so I figured out I should do the following:</p> <pre><code>imports Extensibility Imports System.Runtime.InteropServices Imports Microsoft.Office.Core Imports Access = Microsoft.Office.Interop.Access &lt;GuidAttribute("B61E2444-F46E-4591-A8BA-3D06A4E5D84C"), ProgIdAttribute("MyAddin1.Connect")&gt; _ 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;" &amp; "DATABASE=EDIPolicyNumber", Access.AcObjectType.acTable, "LastPolnum", "LastPolNum") End Sub End Class </code></pre> <p>Now what I want is to be able to execute ChangeLink() from Access. How do I do that?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload