Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Private Sub btnFindRecords_Click()
- Dim cn As ADODB.Connection
- Dim rs As ADODB.Recordset
- Dim sqlQuery As String
- ' open a connection to the access database file
- Set cn = New ADODB.Connection
- cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\Temp\FoodDistribution.accdb;Persist Security Info=False;"
- ' run the query to get the matching records of the requested state
- Set rs = New ADODB.Recordset
- ' **** place the query from Access in the value of this variable ******
- sqlQuery = "Select * from tblFamilies where State='" & Sheet1.Cells(2, 2).Value & "' Order By LastName"
- rs.Open sqlQuery, cn
- ' Excel has a built in method for placing data in the sheet
- Sheet1.Range("A4").CopyFromRecordset rs
- ' close the recrodset and the connection to the Access database file and release the
- ' objects
- rs.Close
- Set rs = Nothing
- cn.Close
- Set conn = Nothing
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement