Advertisement
jdelano

Untitled

Jun 23rd, 2025
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Private Sub btnFindRecords_Click()
  2.  
  3.     Dim cn As ADODB.Connection
  4.     Dim rs As ADODB.Recordset
  5.     Dim sqlQuery As String
  6.    
  7.     ' open a connection to the access database file
  8.    Set cn = New ADODB.Connection
  9.    
  10.     cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\Temp\FoodDistribution.accdb;Persist Security Info=False;"
  11.    
  12.     ' run the query to get the matching records of the requested state
  13.    Set rs = New ADODB.Recordset
  14.    
  15.     ' **** place the query from Access in the value of this variable ******
  16.    sqlQuery = "Select * from tblFamilies where State='" & Sheet1.Cells(2, 2).Value & "' Order By LastName"
  17.     rs.Open sqlQuery, cn
  18.    
  19.     ' Excel has a built in method for placing data in the sheet
  20.    Sheet1.Range("A4").CopyFromRecordset rs
  21.    
  22.     ' close the recrodset and the connection to the Access database file and release the
  23.    ' objects
  24.    
  25.     rs.Close
  26.     Set rs = Nothing
  27.    
  28.     cn.Close
  29.     Set conn = Nothing
  30. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement