You could amend a query's SQL property in code behind a button on the form.
Firstly create and save a query to return all columns. In form design by
naming the checkboxes so that they reflect the column names you can then
build a string expression by looping through them. Say they are named
chkFirstName, chkLastName etc where FirstName and LastName are column names
the code in a button's Click event would go something like this:
Dim dbs as DAO.Database, qdf As DAO.QueryDef
Dim ctrl As Control
Dim strSQL As String
Set dbs = CurrentDb
Set qdf = dbs.Querydefs("YourQuery")
For each ctrl In Me.Controls
If Left(ctrl.Name,3) = "chk" Then
If ctrl = True Then
strSQL = strSQL & ",[" & Mid(ctrl.Name,4) & "]"
End If
End If
Next ctrl
' remove leading comma
strSQL = Mid(strSQL,2)
' build SQL for query
strSQL = "SELECT " & strSQL & " FROM [YourTable]"
' assign query's SQL property
qdf.SQL = strSQL
' open query
DoCmd.OpenQuery "YourQuery"
The above code uses DAO, so ensure that you have a reference to the
Microsoft DAO object library (Tools|References on the VBA menu bar) as since
2000 Access has used ADO as the default data access technology.
Ken Sheridan
Stafford, England
Clay C said:
I have a table with 35 attributes. I would like to use a form with check
boxes on it so that the user could select only the attributes that they
would like to view, i.e. if they only needed to compare 3 of the 35 columns
they could just check those 3 boxes. I have the form with the check boxes
and a button to run the query but I can't figure out the query. Is this even
possible?