[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2927
  • Last Modified:

Run Access Macro from Vb.net

When I run the following code to run an access macro from vb.net I get "Microsoft Office Access can't open the database because it is missing, or opened exclusively by another user."

The database is not missing or opened.
Private Sub RunMacro()
        Dim oAccess As Object
        Try
            oAccess = CreateObject("Access.Application")
            ' Open the database
            oAccess.OpenCurrentDatabase(PickTickMDBLocation, False)

            Try
                oAccess.DoCmd.RunMAcro("test")
                oAccess.DoCmd.Maximize()
                oAccess.Visible = False
            Catch ex As Exception
                sErrNo = Err.Number.ToString
                sError = "RunMacro" & vbCrLf & Err.Description & " " & "ErrNo: " & sErrNo
                dispError(sError)
            Finally
                oAccess.CloseCurrentDatabase()
                oAccess.Quit()
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oAccess)
                GC.Collect()
            End Try
        Catch ex As Exception
            sErrNo = Err.Number.ToString
            sError = "RunMacro" & vbCrLf & Err.Description & " " & "ErrNo: " & sErrNo
            dispError(sError)
        End Try
    End Sub

Open in new window

0
Delta7428
Asked:
Delta7428
  • 3
1 Solution
 
Delta7428Author Commented:
I am getting "Type Access.Application is not defined"

I have included the reference to Microsoft Access object library in COM.
Sub RunMacro()
        Dim Adb As New Access.Application
        Adb.OpenCurrentDatabase("PickTickMDBLocation")
        Adb.Run("test")
        Adb.CloseCurrentDatabase()
        Adb.Quit()

    End Sub

Open in new window

0
 
Delta7428Author Commented:
I added Imports Microsoft.Office.Interop.  That got rid of "Type Access.Application is not defined."

Now I am getting this again:
"Microsoft Office Access can't open the database because it is missing, or opened exclusively by another user."

Please help
0
 
Delta7428Author Commented:
I'm not sure what the difference is, but this works for me.
RunMacro3("test")

Private Sub RunMacro3(ByVal sMacro As String)
        Dim oAccess As Object

        oAccess = CreateObject("Access.Application")
        ' Open the database
        oAccess.OpenCurrentDatabase("c:\test.mdb")
        Try

            oAccess.DoCmd.RunMAcro(sMacro)
            oAccess.Visible = False
        Catch e As Exception
            MsgBox("Macro is cancelled or does not exist")
        Finally
            oAccess = Nothing
        End Try
    End Sub

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now