简体   繁体   中英

MS Access Filter Form Results Using Checkboxes

I have a single table with contact info and about 25 checkboxes. I have a form to input the contact info, and almost all the checkboxes are to specify which power tool was purchased, with the last two checkboxes specifying whether marketing should come via US Mail or Email.

My end-game is to have a CSV file containing the name, address, and email of a subset of all the records. Which records go in is simple: if I check "chainsaw" and "email", then everyone who didn't buy a chainsaw AND want email communication is filtered out. If I check "chainsaw" and "weedwacker" and "US mail" I want all records who bought EITHER a chainsaw OR a weedwacker OR both AND want US mail communications.

I envision using a form with checkboxes where checking any box filters the records to only include those records where that checkbox = yes in the table (for the power tools), and the mail or email filters it further, and then to generate a query that contains those records, then export as a CSV.

My Google-ing skills are good, my VBA coding skills are nonexistent (I'm an HTML/CSS/Joomla guy but got handed this mini project), and the results of searching Google and stackoverflow have yielded nothing that would guide me in the right direction.

I know the table structure is not optimal, but it is manageable for the person I'm making it for who would struggle with more than one table in Access. And it's based off of some archaic database software and they want the structure to stay the same.

You will need to build a sql statement in your code. It can start with:

sql = "select * from MyTable where ID > 0" 'the "Where ID > 0" is here so the remaining sql additions can all say AND

then have a series of if statments that will add to the sql statement:

if chkChainsaw = -1 then
sql = sql & " AND Chainsaw = 1"
end if
if chkweedwacker = -1 then
sql = sql & " AND weedwacker= 1"
end if

When you are done building your sql statement, you can build a recordset and export a csv file from that.

dim rs as recordset
set rs = currentdb.openrecordset(sql, dbopendynaset)
do while not rs.eof
'export data
rs.movenext
loop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM