Solved

Excel 2007: Passing value from Excel to Access query

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 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

23 Experts available now in Live!

Get 1:1 Help Now