Help needed with simple MS Access query through Excel

minette

Board Regular
Joined
Jul 8, 2005
Messages
237
Hi All

I have the following code, which opens a query in an Access database and imports the data into excel. However, I want to add a parameter in the query, e.g. "where date between 1/01/2006 and 30/09/2006", but I'm not sure how or where to add it to the code. It might be useful if it can pop up a box so that I can type it in there, as opposed to having it hard-coded into the query.

Also, when I open the query in Access, it always prompts me for my username and password. Is there any way of adding this to the code so that I won't have to type this in each time?

Any help would be greatly appreciated.

Many thanks
Minette


Sub test()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim ws As Worksheet, i As Integer
Set ws = ThisWorkbook.Sheets.Add
Set db = OpenDatabase("C:\Noida WB\WORKbasket.mdb", False, True)
Set rs = db.OpenRecordset("Noida_with _pol_nos", dbOpenSnapshot)
With ws
For i = 1 To rs.Fields.Count
.Cells(1, i) = rs.Fields(i - 1).Name
Next i
.Range("a2").CopyFromRecordset rs
End With
db.Close
Set rs = Nothing
Set db = Nothing
End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

Forum statistics

Threads
1,141,720
Messages
5,708,086
Members
421,545
Latest member
TWR

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
Top