Solved

Excel 2007: Passing value from Excel to Access query

Posted on 2012-03-27
5
402 Views
Last Modified: 2012-04-01
Hi, I use the following code to execute an Access query with from Excel.  Then an input box pop up require user to enter a criteria for the query.  I'd like to automatically pass a value from Excel to the query criteria.  The value is stored in cell E5 or named range "Input" on the Excel file.  How do I do that?  Thanks.

Sub RunAccessQuery()

    Dim acObj As Object
   
    Set acObj = CreateObject("Access.Application")
    acObj.OpenCurrentDatabase "C:\Documents and Settings\Work\DB.accdb"
    acObj.DoCmd.OpenQuery ("qry_01")
    acObj.CloseCurrentDatabase
    Set acObj = Nothing

End Sub
0
Comment
Question by:JCJG
  • 3
  • 2
5 Comments
 
LVL 14

Expert Comment

by:pteranodon72
ID: 37774453
JCJG,

Give this code a shot.

' code in Excel
Sub RunAccessQuery()

    Dim acObj As Object 'Access.Application
    Dim db As Object 'DAO.Database
    Dim qdf As Object 'DAO.QueryDef

    Set acObj = CreateObject("Access.Application")
    acObj.OpenCurrentDatabase "C:\Documents and Settings\Work\DB.accdb"
    Set qdf = acObj.DBEngine(0)(0).QueryDefs("qry_01")
    qdf.Parameters(0) = Range("E5")
   
    'execute the action query
    qdf.Execute

    'If you want feedback on the passed parameter, recs affected
    'Debug.Print qdf.Parameters(0), qdf.RecordsAffected

    Set qdf = Nothing
    Set db = Nothing
    acObj.CloseCurrentDatabase
    acObj.Quit 2   'acQuitSaveNone = 2
    Set acObj = Nothing

End Sub

Open in new window


Hope this helps,
pT72
0
 

Author Comment

by:JCJG
ID: 37774588
Hi, I got an error message saying the Access table already exist.  I think it is because the query I executed is a make-table query and the existing table needs to be replaced.  How to fix it?

In addition, can you help me with a second set of code to run an Access macro instead?

Thanks!
0
 

Author Comment

by:JCJG
ID: 37774616
Actually I have one more question.  I'd like expand the criteria from cell E5 to E5:E6.  Is it doable?  Thanks.
0
 
LVL 14

Accepted Solution

by:
pteranodon72 earned 500 total points
ID: 37776367
JCJG,
You'll need to delete the table (if it exists) from the tabledefs collection:
' code in Excel
Sub RunAccessQuery()

    Dim acObj As Object 'Access.Application
    Dim db As Object 'DAO.Database
    Dim qdf As Object 'DAO.QueryDef

    Set acObj = CreateObject("Access.Application")
    acObj.OpenCurrentDatabase "C:\Documents and Settings\Work\DB.accdb"


    Set db = acObj.DBEngine(0)(0)

    'skip over table deletion if table does not exist
    On Error Resume Next

    'delete the old table
    db.TableDefs.Delete "YOUR_TABLE_NAME_HERE"
    db.TableDefs.Refresh 'update the collection
    On Error Goto 0 'resume breaking on errors

    Set qdf = db.QueryDefs("qry_01")
    qdf.Parameters(0) = Range("E5")
   
    'execute the action query
    qdf.Execute

    'If you want feedback on the passed parameter, recs affected
    'Debug.Print qdf.Parameters(0), qdf.RecordsAffected

    Set qdf = Nothing
    Set db = Nothing
    acObj.CloseCurrentDatabase
    acObj.Quit 2   'acQuitSaveNone = 2
    Set acObj = Nothing

End Sub

Open in new window



For your question on macros, the DoCmd object that you used in the old code has a RunMacro method:

acObj.DoCmd.RunMacro "macroname"

For expanding the criteria of the query, it involves modification of the query (adding a second parameter or changing the SQL), so I think it deserves a separate question.

Hope this helps,

pT72
0
 

Author Closing Comment

by:JCJG
ID: 37794668
Thanks for your help!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

763 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