Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ms query in excel

Status
Not open for further replies.

CraigMan

Programmer
Apr 27, 2004
33
US
Trying to restrict the records returned by a query in excel. I would like to specify a from and to date in two excel cells and use these two dates in the query. Can this be done? Programatically?

Craig Meyers, BSNucE, PE
 
yup - pretty easily

You can access the Querytable object and interrogate the .CommandText property. This holds the SQL statement as a string. You can therefore manipulate the string as with any strings to insert variables (ie your From & To dates).

Code:
FromDate = Format(Range("A1").value,"dd/mm/yyyy")
ToDate = Format(Range("A2").value,"dd/mm/yyyy")

With sheets("Sheetname").Querytables(1)
.CommandText = "SELECT Data from Table WHERE " & _
               "DateField >= '# " & FromDate & " #' AND " & _
               "DateField <= '# " & ToDate & " #'"
.Refresh(false)
End With

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 
Thanks for the help!!

Craig Meyers, BSNucE, PE
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top