Sub btnSQL_Click()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim sSQL1 As String
Dim sSQL2 As String
Dim iChecked As Integer
Set db = CurrentDb
On Error Resume Next ' just in case there is alread a query called TempQuery
DoCmd.DeleteObject acQuery, "tempquery"
' create a tempquery
Set qdf = db.CreateQueryDef("tempquery", "select * from Locations")
sSQL1 = "SELECT "
sSQL2 = " FROM LOCATIONS"
iChecked = 0 ' counter of number of check boxes ticked
On Error GoTo Err_btnSQL_Click
' check value of -1 means IT IS CHECKED
If Me.Check0.Value = -1 Then
iChecked = iChecked + 1
sSQL1 = sSQL1 & "EmployeeID " 'field to select/display
End If
If Me.Check2.Value = -1 Then
iChecked = iChecked + 1
If iChecked > 1 Then
sSQL1 = sSQL1 & "," ' if a field already selected, must follow with a comma SQL SYNTAX
End If
sSQL1 = sSQL1 & "EmployeeName "
End If
If Me.Check4.Value = -1 Then
iChecked = iChecked + 1
bla bla....