Solved

Run Access Macro from Vb.net

Posted on 2010-08-13
4
2,477 Views
Last Modified: 2012-05-10
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
Comment
Question by:Delta7428
  • 3
4 Comments
 
LVL 22

Expert Comment

by:plusone3055
ID: 33433307
0
 

Author Comment

by:Delta7428
ID: 33436286
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
 

Author Comment

by:Delta7428
ID: 33445487
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
 

Accepted Solution

by:
Delta7428 earned 0 total points
ID: 33445844
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now