Change Access Query Criteria using Excel

mrholiday

Board Regular
Joined
Jan 27, 2006
Messages
57
Dear all,

I looking for a solution to change a Access Query Criteria using a VB code in Excel, without opening the Access DB and changing the criteria manualy?


My normal Job is it:
1) I Work all the time in Excel
2) At the end of the work in Excel, I need to open Access
3) Open a query (Called "MyQuery")
4) Change one parameter the Date: Between 01/10/2008 and 30/10/2008 (This for each month)
5) Run the query

I looking to get rid of points 2) to 5) by replacing with a Command button on an Excel sheet


Sub Change_Criteria_And_Run_Query()


Dim mydbase As Object
Set mydbase = CreateObject("Access.Application")
mydbase.OpenCurrentDatabase ("C:\My doucments\DB1.mdb")

mybase."MyQuery"."Date Criteria" = Between (Worksheet("Sheet1").Range("A1").value) and (Worksheet("Sheet1").Range("A2").value)

>>>>>The above line of code I have problems to get right!

mydbase.DoCmd.RunMacro "MyQuery"

End Sub


Is this possible to do?



Thanks for any tip
Jerry
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Does your query return a recordset or is it an action query?
 
Upvote 0
Hello Rory,

MyQuery is a simple query on a database, which result is used by other users.

Thanks
Jerry
 
Upvote 0
That doesn't really answer my question - does it write records to a table, for example, or display them? If it displays them, what do you want to do with the data after you have run the query from Excel?
 
Upvote 0
Hello Rory,

Actually I using a Command button in Excel to:
1) Run the query for all dates (+ 300,000 records)
2) I import the data in Excel
3) Filter the data to the latest month (which I only need)

Point 1 takes up to 5 minutes to compete and freezes all the applications, where 1 month takes only 1 second.

When I could change the date criteria range before runing the query would save time.
 
Upvote 0
You can use code like this to run a parameter query in an Access database. I have commented the bits you need to change:
Code:
Sub GetAccessParameterData()
   ' Sample demonstrating how to return a recordset from a parameterised query
   '#############################################################
   ' Requires reference to Microsoft ActiveX Data Objects library
   '#############################################################
   Dim cnn As ADODB.Connection
   Dim strQuery As String
   Dim cmd As ADODB.Command
   Dim rst As New ADODB.Recordset
   Dim prm As ADODB.Parameter, prms As ADODB.Parameters
   Dim strPathToDB As String
   Dim wks As Worksheet
   Dim i As Long
   
   Set wks = ActiveSheet
   
   ' change database path and query name as required
   strPathToDB = "C:\Test\db1.mdb"
   strQuery = "qselTestParam"
   
   Set cnn = New ADODB.Connection
   With cnn
      .Provider = "Microsoft.Jet.OLEDB.4.0"
      .ConnectionString = "Data Source=" & strPathToDB & ";"
      .Open
   End With
   Set cmd = New ADODB.Command
   With cmd
      Set .ActiveConnection = cnn
      .CommandText = strQuery
      .CommandType = adCmdTable
      .Parameters.Refresh
      
      ' Change parameter names as necessary
      .Parameters("[Start_ID]").Value = 1
      .Parameters("[Stop_ID]").Value = "Test man"
   End With
   
   rst.Open cmd
   With rst
      If Not (.EOF And .BOF) Then
               'Populate field names
         For i = 1 To .Fields.Count
            wks.Cells(1, i) = .Fields(i - 1).Name
         Next i
         ' Copy data
         wks.Range("A2").CopyFromRecordset rst
      End If
   End With
   rst.Close
   Set rst = Nothing
   Set cmd = Nothing
   cnn.Close
   Set cnn = Nothing
End Sub

HTH
 
Upvote 0
Hello Rorya,

Thanks a lot this code works perfect and super quick :)

There is just one more little small problem, the Access Database requires an Password to Open!

This makes the VB code not to work once the runing macro reachs ".Open"

Do you know perhaps as well where to place the "PASSWORD" code line in your VB code?


Set cnn = New ADODB.Connection
With cnn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source=" & strPathToDB & ";" > ?? PASSWORD:="TEST" ??
.Open > the error comes at this line due to the missing Password!

End With
 
Upvote 0
If it's just a database password, then change the connection string to:
Code:
Data Source=C:\mydatabase.mdb;Jet OLEDB:Database Password=MyDbPassword;
changing path and password as required.
 
Upvote 0
that was a very quick reply Rorya! :)
This tip worked fine

Thanks once more and wish you a nice day....

Jerry
 
Upvote 0
I have a similar project, and I tried to use the code you provided, but I am getting a:

"Run-time error '-2147217900 (80040e14)': Syntax error in FROM clause."

and it brings me to the line .Parameters.Refresh

I know almost absolutely nothing about this type of code, so all I did was copy and past into the VB editor and change my database, query, and parameters.
I am using Excel 2007 and Access 2003. My query is a select query where I have added user prompts for the parameters.

Any ideas why it might not be working? Please help! Thanks a lot!
Also, I've been scouring the internet for 1.5 days trying to learn how to do this - any good reference book ideas?
 
Upvote 0

Forum statistics

Threads
1,215,061
Messages
6,122,921
Members
449,094
Latest member
teemeren

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top