Results 1 to 13 of 13
  1. #1
    Auto is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Jul 2013
    Posts
    60

    Run Access Sub from excel

    hi.

    i am having a module in access named "GlobalFunctions"
    in that module i have alot of sub's and functions.
    there is a public sub named "ImportOldData"



    i need to run this sub from my excel application, i have created a code in vba to do it, but somehow it generates an error number '2485' that mydatabase cant find my module named 'GlobalFunctions'

    i tried so many diffrent ways, but no luck, maybe anyone here can help.

    here is my code.
    Dim appAccess As Object

    strDatabasePath = "C:\Test.accde"
    Set appAccess = GetObject(, "Access.Application")

    With appAccess
    .Visible = True
    .DoCmd.RunMacro "GlobalFunctions.ImportOldData"
    End With
    Set appAccess = Nothing

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    This is code I use in Access VBA to run VBA procedure in another Access db.

    Dim A As New Access.Application
    Set A = CreateObject("Access.Application")
    A.Visible = False
    A.OpenCurrentDatabase "filepath\filename.accdb", False
    A.Run "GetPaverData"
    Set A = Nothing
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    As you see from June's code, your code creates an instance of Access, but fails to open a database.

    I did find two sites that might help you. I think the 2nd link has more info...
    See:
    http://accessprogrammer.blogspot.com...procedure.html
    http://www.mrexcel.com/forum/excel-q...ule-excel.html

  4. #4
    Auto is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Jul 2013
    Posts
    60
    Quote Originally Posted by June7 View Post
    This is code I use in Access VBA to run VBA procedure in another Access db.

    Dim A As New Access.Application
    Set A = CreateObject("Access.Application")
    A.Visible = False
    A.OpenCurrentDatabase "filepath\filename.accdb", False
    A.Run "GetPaverData"
    Set A = Nothing
    thanks for your reply, i tried that too, but it dont work, it opens up the db, and it throws me an error that my db cant find that procedure.

  5. #5
    Auto is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Jul 2013
    Posts
    60
    Quote Originally Posted by ssanfu View Post
    As you see from June's code, your code creates an instance of Access, but fails to open a database.

    I did find two sites that might help you. I think the 2nd link has more info...
    See:
    http://accessprogrammer.blogspot.com...procedure.html
    http://www.mrexcel.com/forum/excel-q...ule-excel.html
    thanks for your reply, i went to both links but it dont help me, the error still comes up no matter what i change in the code, in regards that you say it fails to open the db, it dont fail, it opens it and it tries to run the macro but it gets back from access that this macro dont exist, thats how i understand the error msg, might be i am wrong, but i tried to do it with creatObject as well with GetObject, when its createObject it opens the db and sends me the error msg as well as if its GetObject and the db is open.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Show your attempted code.

    Is the macro Embedded or a general macro?

    My code runs a VBA procedure in a general module, not a macro.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    Auto is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Jul 2013
    Posts
    60
    just wondering, the db which had the module is a accde file, could it be that you cant call a macro from an accde file?

  8. #8
    Auto is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Jul 2013
    Posts
    60
    Quote Originally Posted by June7 View Post
    Show your attempted code.

    Is the macro Embedded or a general macro?

    My code runs a VBA procedure in a general module, not a macro.
    my macro sits in a module called GlobalFunctions and in that module i have a public sub named ImportOldData.
    i am not sure what you mean an Embedded, you mean a private sub? or a sub within a form or so?

  9. #9
    Auto is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Jul 2013
    Posts
    60
    Quote Originally Posted by June7 View Post
    Show your attempted code.
    .
    here is what i tried last.

    Public Sub ProcedureInAccess()

    Dim acApp As Object
    Dim db As Object
    Set acApp = CreateObject("Access.Application")
    acApp.OpenCurrentDatabase ("C:\Test.accde")
    acApp.Run "ImportOldData"


    Set acApp = Nothing


    End Sub
    here is my sub from the access db

    Public Sub ImportOldData()Dim OldDb As String
    OldDb = Form_Main.Version.Caption - 0.1


    DoCmd.SetWarnings False
    DoCmd.TransferDatabase acImport, "Microsoft Access", "c:\TestOld.accde", acTable, "Customers", "Customers", False
    DoCmd.TransferDatabase acImport, "Microsoft Access", "c:\TestOld.accde", acTable, "Orders", "Orders", False
    DoCmd.SetWarnings True


    End Sub

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Macros and VBA code are very different in Access. If your procedures in Access are entirely VBA then you are not calling a macro.

    Even in Excel, when you create a macro, really creating a VBA procedure.

    You are trying to call a procedure in an accde file. If I understand accde correctly, it is a compiled executable program. The original VBA code modules do not exist in accde. http://office.microsoft.com/en-us/ac...x#BMextensions

    However, might be able to call a general macro that is in an accde. I've never used accde file.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    Auto is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Jul 2013
    Posts
    60
    Quote Originally Posted by June7 View Post
    Macros and VBA code are very different in Access. If your procedures in Access are entirely VBA then you are not calling a macro.

    Even in Excel, when you create a macro, really creating a VBA procedure.

    You are trying to call a procedure in an accde file. If I understand accde correctly, it is a compiled executable program. The original VBA code modules do not exist in accde. http://office.microsoft.com/en-us/ac...x#BMextensions

    However, might be able to call a general macro that is in an accde. I've never used accde file.
    thanks june7, thats exactly what i thought the problem is.

    now i really have no experience with macros in access, i do all my work in vba since this is my first language
    what is the limitations of macros in access 2007? can i do the same stuff as i do in my sub? and where do i need to hook up that macro?

    is there any workaround what i can do? maybe manipulating with excel vba and send all functions from excel to access, would that work?

  12. #12
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    I haven't used macros much. I know they can call a VBA function procedure. No, they cannot do everything that VBA can do.

    I don't know if accde loses macros (if they are somehow compiled like the VBA).

    Suggest you do some research and study on macros. Two basic types - embedded (alternative to [Event Procedure]) and general standalone. Build a standalone macro, just to open a message box maybe, in the original accdb file then publish as accde. See if the Excel code can call it.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  13. #13
    Auto is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Jul 2013
    Posts
    60
    hi june7.

    i got it work, and here is the workaround.

    first of all, we were right with that, you cannot call a vba function or sub from an accde file.

    so what i did is, i created a macro as you mentioned before, and i called the function from the macro, and i named the macro a friendly name, then i call the macro from the excel vba, and now it works.

    THANK YOU ALL FOR HELPING ME TO SOLVE MY ISSUE!
    Cheers.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Import access query data into excel file, using excel vba
    By exceljockey in forum Import/Export Data
    Replies: 3
    Last Post: 11-15-2013, 02:36 PM
  2. Replies: 7
    Last Post: 04-25-2013, 03:47 PM
  3. Replies: 10
    Last Post: 12-28-2012, 02:06 PM
  4. Replies: 4
    Last Post: 12-17-2012, 01:21 PM
  5. Replies: 1
    Last Post: 08-31-2009, 10:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums