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!

Filtering OpenRecordSet

Status
Not open for further replies.

dfarland

Technical User
May 9, 2003
16
US
I am trying to open an Access DB in an Excel VBA. This following code works well, but now I would like to filter the the records by one of the fields in the table "data". Can I add a SQL statement somehow to accomplish that?

Here is my simple example:

Sub GetTable2()
Dim rs As Recordset

Set rs = DBEngine.OpenDatabase("C:\Sales\StoreDB.mdb").OpenRecordset("data")
Range("A1").CopyFromRecordset rs

End Sub

Dean
 
Hi dfarland

You Can Use A query to get a filtered recordset like this

Dim DbEngine As DataBase //DAO, Can Also use ADO
Dim rs As Recordset
Dim strSQL As String

Set DbEngine = OpenDatabase("C:\Sales\StoreDB.mdb")
strSql = "SELECT * FROM data ORDER BY /*ENTER FIELD HERE*/;"
Set rs = DbEngine.OpenRecordset(strSQL)

Or you Can use sort once you have the rs entered in Excel

xlSht.Cells.Sort Key1:=xlSht.Range("A1"), Order1:=xlDescending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal

Hope this helps

Aine
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top