Solved

Excel 2007: Passing value from Excel to Access query

Posted on 2012-03-27
5
356 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

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

Suggested Solutions

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

911 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

22 Experts available now in Live!

Get 1:1 Help Now