Build the SQL statement dynamically : SQL Parameter « Access « VBA / Excel / Access / Word






Build the SQL statement dynamically

 
Sub MyFirstConnection2()
    Dim myConnection As ADODB.Connection
    Dim myRecordset As ADODB.Recordset
    Dim strSQL As String
    Dim strSearch As String
    
    strSearch = "Lee"
    
    strSQL = "SELECT FirstName, LastName FROM Employees" & _
              " WHERE LastName = " & " '" & strSearch & "'"
    
    Set myConnection = CurrentProject.Connection
    
    Set myRecordset = New ADODB.Recordset
    myRecordset.Open strSQL, myConnection
    
    Do Until myRecordset.EOF
       Debug.Print myRecordset.Fields("FirstName"), _
                   myRecordset.Fields("LastName")
       myRecordset.MoveNext
    Loop
    myRecordset.Close
    myConnection.Close
    Set myConnection = Nothing
    Set myRecordset = Nothing
End Sub

 








Related examples in the same category

1.Creat a SQL statement and append parameter as ceriteria
2.Creating a Parameter Query
3.Running Parameter Queries
4.Executing a SQL Statement Containing Parameters
5.User InputBox to read SQL statement parameter