Using variables in SQL statements can be tricky, but they can give you the flexibility needed to reuse a single SQL statement to query different data. In Visual Basic for Applications (VBA) you can build SQL statements that can contain string criteria. To use a string variable in a SQL string statement you must use the (") as the string delimiter and apply single quotation marks (’) around the variable. Use variables in your SQL string instead of rewriting a SQL statement over and over to query data using different criteria. Dim mySQLVariable As String Dim strSQL As String mySQLVariable = “Sales Manger” strSQL = “SELECT Employees.[First Name], " strSQL = strSQL & “Employees.[Last Name], " strSQL = strSQL & “Employees.[Job Title] " strSQL = strSQL & “FROM Employees " strSQL = strSQL & “WHERE (((Employees.[Job Title])=‘Sales Manager’));” strSQL = strSQL & “WHERE (((Employees.[Job Title])=’” & (mySQLVariable) & “’));” Writer Bio

How To Use a Variable In a SQL String VBA - 59