Results 1 to 5 of 5
  1. #1
    Lounger
    Join Date
    Jan 2004
    Location
    Prague, Czech Republic
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Calling Access code/UDF from Excel (Office 2k)

    Hello,
    I have a code and UDFs in MS Access database. How can I use them / call them from Excel VBA module?

    Thank you
    Martin
    Regards,
    Martin

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Calling Access code/UDF from Excel (Office 2k)

    Are you sure you want to do this? It's a lot of overhead! You would need to use Automation to start an instance of Access, open the database and run the code. Example: say you have

    Public Function TestMe(MyValue As Integer) As Integer
    ...
    End Function

    in a standard module in the database C:AccessMyDatabase.mdb. The following code in Excel will call this function and put the result in a cell on the active worksheet.

    Sub RunCodeFromAccess()
    Dim objAcc As New Access.Application

    On Error GoTo ErrHandler

    objAcc.OpenCurrentDatabase "C:AccessMyDatabase.mdb"
    Range("A1") = objAcc.Run("TestMe", 37)

    ExitHandler:
    On Error Resume Next
    objAcc.Quit acQuitSaveNone
    Set objAcc = Nothing
    Exit Sub

    ErrHandler:
    MsgBox Err.Description, vbExcalamation
    Resume ExitHandler
    Exit Sub

  3. #3
    Lounger
    Join Date
    Jan 2004
    Location
    Prague, Czech Republic
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Calling Access code/UDF from Excel (Office 2k)

    Thank you for the prompt answer Hans,

    no problem with Automation and yes, I'd like to do this - if possible <img src=/S/smile.gif border=0 alt=smile width=15 height=15> .
    <pre>Sub RunAccessMacro()
    Dim Cn As Object
    Dim myConnString As String

    myConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=CataAccess2000MISMIS.mdb;" & _
    "Jet OLEDB:System database=CataAccess2000MIS.mdw;" & _
    "User ID=******;Password=*******;"

    Set Cn = CreateObject("ADODB.Connection")

    Cn.Open myConnString

    Cn.Run ("ImportSupply") ' This does not work
    End Sub</pre>



    Can I build up on this or do I have to use the OpenCurrentDatabase method. Please note my db is user-level secured.

    Thanks

    Martin
    Regards,
    Martin

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Calling Access code/UDF from Excel (Office 2k)

    ADO approaches an Access database at a low level; as far as I know, you can't get at VBA code using ADO, only at tables and queries.

    See if this newsgroup post helps.

  5. #5
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Calling Access code/UDF from Excel (Office 2k)

    if you're trying to run an Access query that might not work -- you may have to re-write the query using ADO in Excel VBA. I am involved in a project doing precisely that and, although it's a pain to re-write the Access queries, at least I can organize them a little better than what the Access query window allows.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •