Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Performing Access query with Parameters from inside Excel

Posted on 2009-04-01
3
Medium Priority
?
611 Views
Last Modified: 2012-05-06
I need some help performing an access query in excel.
The access data is all in one table (and its about 3 million records) and I have the query with the right parameters set up and it works fine but I want to be able to run the query and get the data from excel because I have to perform several calculations on the data (so I don't want to build a form). I need the query to be dynamic - i.e. it chooses parameters from user entries in the worksheet.

I have tried using the get data "from other sources" through MS query, and it sort of works including looking up the parameters from the cells i tell it to, but it is painfully, painfully slow and sometimes the connections break.

Is there any other way, perhaps with some vba code or is the MS Query the only way?
0
Comment
Question by:mstringfield
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 15

Expert Comment

by:yuk99
ID: 24046162
This is how to do Access query in Excel by VBA:
http://support.microsoft.com/kb/q147739/
0
 
LVL 17

Expert Comment

by:Natchiket
ID: 24047226
hi mstringfield, one option would be to use a recordset, however with over 3m records the results won't be blisteringly fast, however the excel range's copyfromrecordset method makes it fairly straightforward.  Here's some air code to give you an idea as to how it could be done.



Sub GetData()
Dim db as DAO.Database
Dim rst as DAO.Recordset
Dim strSQL as String
Dim wksD as Excel.WorkSheet   'Destination worksheet
Dim wksS as Excel.Worksheet   'Worksheet with parameters
Dim rng as Excel.Range
 
Set wksD = ActiveWorkbook.Worksheets("Data")
Set wksS = ActiveWorkbook.Worksheets("Source")
 
'Grab the database you want to get the data from
Set db = DAO.DBEngine.OpenDatabase("<full path of the db you want use>")
 
'Create the SQL to capture the data, you could modify this bit to dynamically alter the WHERE clause depending which parameters were specified.
strSQL = "SELECT <field1,field2,fieldn etc> FROM <the table you want to use> "
strSQL = strSQL & " WHERE field1=" & wksS.Cells(1,1)    'Pretending there is a parameter in A1 in the 'Source' sheet
 
'Open the recordset to capture the data
Set rst = db.Openrecordset(strSQL,dbOpenSnapshot)
 
'Plonk the data into Excel
Set rng = wksD.Cells(1,1)
rng.CopyFromRecordset rst
 
End Sub

Open in new window

0
 

Accepted Solution

by:
mstringfield earned 0 total points
ID: 24074332
I found MS Query to be the best solution. I just needed to structure my query better. Another thing I was doing wrong was not saving the MS query inside the folder of the workbook and data set.
0

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

609 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