Solved

Run Access Macro from Vb.net

Posted on 2010-08-13
4
2,626 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Import Data from Multiple Text Files in Excel 12 86
.NET 2008 VB and C# 6 48
vb.net and creating a class 5 31
visual studio vb.net windows onkeyup 2 29
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

733 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