473,748 Members | 2,670 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Checkbox form to allow user to select query output fields

33 New Member
Hi, I'm new to access (2007, running on XP), but I've gotten some great help from this site on my project so far. However, the more I know, the more complex the problems seem to become....

I'm working with all of my data in a single table and, right now, I've got a form linked to a query that allows the user to input whatever search criteria they want through a variety of controls. I've also created a series of check boxes on the form representing every possible field (columns in the table) that the query could return. Since not every user needs or wants to see every field contained in the records, I would like the user to be able to limit the fields returned by the query to only the checkboxes he/she ticks. After the fields are selected, the query should run as usual, returning only the records that satisfy the specified criteria, but only displaying the fields ticked off in the boxes.

I'm building the query in SQL View, and I figure that the key to this must have something to do with the "SELECT" line. Since a normal "fixed-field" query has the fields to be return specified by
Expand|Select|Wrap|Line Numbers
  1. SELECT [NameOfTable]![NameOfField1], [NameOfTable]![NameOfField2], etc...
I've tried to build a "dynamic" SELECT statement using the IIF function:
Expand|Select|Wrap|Line Numbers
  1. SELECT IIF([Form]![chkbx1]=True, [NameOfTable]![NameOfField1], Null), IIF([Form]![chkbx2]=True, [NameOfTable]![NameOfField2], Null), etc...
This is not working, of course--I don't think "Null" is the proper argument to enter for the condition "chkbx1=Fal se", but I don't know what else to do. Are there any other conditional functions that could help me build a list of SELECT fields based on the Yes/No value of the checkbox controls in the form?

Or am I going about this in completely the wrong way. I think there must be a way to do this, because what I'm trying to get the query to do doesn't seem to be anything too out of the ordinary. Any help would be appreciated!

Thanks in advance,

Joe
Nov 10 '09
45 27734
dizzydangler
33 New Member
Mary, in plain english, my criteria says:

"If the value in the control is null, return records with all values (both null and not null) for this field. If the control is not null, return all records which include the text entered in the control"

It's a little ungainly, but it has worked just fine as a criteria in the "standalone " query I built using the SQL view and linked to the original data entry form.

I tried your latest suggestion, and didn't get an error message, but didn't get it to return any results, either. Then I re-wrote my original criteria, taking your advice to replace double quotes inside the IIF statement with single quotes:

Expand|Select|Wrap|Line Numbers
  1. strSQL_2 = "SELECT " & Left$(strSQL, Len(strSQL) - 2) & " FROM [Database 1] " & "WHERE " & "((IIf([Forms]![Search_Database]![City] Is Null,(([Database 1].[City]) Like '*' & [Forms]![Search_Database]![City] & '*' Or ([Database 1].[City]) Is Null),(([Database 1].[City]) Like ('*' & [Forms]![Search_Database]![City] & '*'))))<>False);"
Something interesting happened. If I leave the <City> control blank, it returns all records in the table (which it should!) only problem is, I can only see the resulting table when I switch the form to design view. When I do this, the table
<Search_Databas e> drops down from top of the navigation bar and shows up under a bar labeled "Unrelated Objects." If I double-click, I can see the results.

If I enter a text string in the <City> control, the same thing happens--when I switch the form to Design View, the <Search_Databas e> table appears. However, when I double-click on the table to open it, I receive an "Enter Parameter" prompt for

Forms]![Search_Database]![City

If I enter a text string in this box, the query opens with exactly the data I'm looking for: all records which contain the text string. I don't know why the results aren't appearing until after I switch to Design View, and I can't write the criteria in such a way that it will run the query with no additional input. It's GOT to have something to do with they syntax used to "feed" the text string defined by strSQL_2 into the currentDb.Creat QueryDef function, because when I enter the criteria directly into the SQL view of a query, it runs just fine.

I've got to take a break to get some sleep, but I'll be back at it again tomorrow....
Nov 12 '09 #21
MMcCarthy
14,534 Recognized Expert Moderator MVP
Add this line to the end of your code to open the query.

Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenQuery "conQUERY_NAME"
Nov 12 '09 #22
NeoPa
32,571 Recognized Expert Moderator MVP
Wouldn't that be :
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenQuery conQUERY_NAME
I'm going on the name of the item only here of course.
Nov 12 '09 #23
MMcCarthy
14,534 Recognized Expert Moderator MVP
@NeoPa
LOL!

You are perfectly right Ade, didn't realise that was a variable name and not a query name.

Use NeoPa's example Joe.
Nov 12 '09 #24
NeoPa
32,571 Recognized Expert Moderator MVP
I think, together, we add up to an almost decent expert :D
Nov 12 '09 #25
MMcCarthy
14,534 Recognized Expert Moderator MVP
@NeoPa
Ha Ha ... :P
Nov 12 '09 #26
ADezii
8,834 Recognized Expert Expert
What are you guys doing to my code! (LOL)!
Nov 12 '09 #27
MMcCarthy
14,534 Recognized Expert Moderator MVP
@ADezii
I didn't touch it Sir, it was Ade Sir, really Sir, I'm innocent ... RUN ADE!

LOL!
Nov 13 '09 #28
ADezii
8,834 Recognized Expert Expert
dizzydangler, I think that part of the problem is that you are attempting to modify and/or incorrectly copying perfectly functioning code such as provided and later modified in Post #5. In order to build your WHERE Clause into the logic, I included a Text Box on the Form named txtCity, within which you can specify a City to Filter on. Take notice of the code changes specified below:
  1. New Variable Declaration in Line #4 to hold the WHERE Clause.
  2. Code to check the Status of the txtCity Text Box, and to Set (Check) the City Check Box if necessary (Lines 9 thru 15).
  3. Building of a simple WHERE Clause Lines 12 and 14).
  4. Removal of the semi-colon in Line #32, and the concatenating of the WHERE Clause (strWhere).
Expand|Select|Wrap|Line Numbers
  1. Dim ctl As Control
  2. Dim strSQL As String
  3. Dim strSQL_2 As String
  4. Dim strWhere As String
  5. Dim qdf As DAO.QueryDef
  6. Dim qdfDemo As DAO.QueryDef
  7. Const conQUERY_NAME As String = "Selective Employee Fields"     'Physical Query Name
  8.  
  9. 'If Criteria is specified for the [City] Field, the Check Box must be selected
  10. If Not IsNull(Me![txtCity]) Then
  11.   Me![Check10].Value = True
  12.     strWhere = " WHERE [City] ='" & Me![txtCity] & "';"
  13. Else    'No Criteria
  14.    strWhere = ";"
  15. End If
  16.  
  17. For Each ctl In Me.Controls                 'Query every Control on the Form
  18.   If ctl.ControlType = acCheckBox Then      'Is it a Check Box?
  19.     If ctl.Value Then                       'Is it selected?
  20.       strSQL = strSQL & ctl.Tag & " ,"      'Build the Field Selection(s)
  21.     End If
  22.   End If
  23. Next
  24.  
  25. If strSQL = "" Then Exit Sub        'No Fields selected, get out!
  26.  
  27. CurrentDb.QueryDefs.Delete conQUERY_NAME        'DELETE existing Query
  28.  
  29. On Error GoTo Err_cmdTest_Click
  30.  
  31. 'Build complete SQL Statement
  32. strSQL_2 = "SELECT " & Left$(strSQL, Len(strSQL) - 2) & " FROM Employees" & strWhere
  33.  
  34. 'Create the QueryDef Object
  35. Set qdfDemo = CurrentDb.CreateQueryDef(conQUERY_NAME, strSQL_2)
Nov 13 '09 #29
dizzydangler
33 New Member
IT WORKS!!!

I haven't checked the code that you posted yet, ADezii, but with Ade and Mary's input, I was able to get the query table to open automatically when the search button is clicked. Then it was just a matter of starting with a very basic criteria and building it up into a more complex expression.

First, to make the query return ALL records, null and not null:

Expand|Select|Wrap|Line Numbers
  1. strSQL_2="SELECT " & Left$(strSQL, Len(strSQL)-2) & " FROM [Database 1] " & "Where [City] Is Not Null Or [City] Is Null;"
Then I figured out how to write an IIF statement that returns all records with "null" values if the control is left blank and all records with "not null" values if anything is typed in:

Expand|Select|Wrap|Line Numbers
  1. strSQL_2="SELECT " & Left$(strSQL, Len(strSQL)-2) & " FROM [Database 1] " & "Where IIF([Forms]![Search Database]![City] Is Null, [City] Is Null, [City] Is Not Null;"
Then I got the LIKE statement working, returning all records which contain the field that is entered in the control:

Expand|Select|Wrap|Line Numbers
  1. strSQL_2="SELECT " & Left$(strSQL, Len(strSQL)-2) & " FROM [Database 1] " & "Where [City] Like '*'&[Forms]![Search Database]![City]&'*';"
And, finally, put it all together to give me the criteria that I want (everything if the control is left blank, all records which contain the entered text if anything is typed into the control:

Expand|Select|Wrap|Line Numbers
  1. strSQL_2="SELECT " & Left$(strSQL, Len(strSQL)-2) & " FROM [Database 1] " & "Where IIF([Forms]![Search Database]![City] Is Null, [City] Is Null Or [City] Is Not Null, [City] Like '*'&[Forms]![Search Database]![City]&'*';"
In this format, I should be able to string criteria together with the AND function (I've already gotten it to work with two, it's just a matter of jamming the rest in now that I know the correct syntax).

Speaking of that, I've found that editing code in the VB editor attached to the form is much less forgiving than what I was doing previously in the SQL View of the query. Spaces matter, for instance! I wasn't able to get the Like function working until I took out all the spaces in the expression <'*"&[ControlName]&"*">, and "SELECT" didn't work until I added a space afterwards and made it "SELECT ". Wow, I feel like I have learned a lot thanks to all three of your help and patience--and I know I'm not even scratching the surface.

I only have three (minor) questions left (for this project, at least!):

1) when I start stringing my criteria expressions together, they get really long really fast, but when I type "enter" to break the line and start a new one, like I used to do in SQL View, the code won't compile. What's the syntax for inserting a line break in the code, for ease of reading/editing?

2) is there a way to control the order of the columns when the query table is built? Not a major deal, because I can always move them around after the table appears. It looks like they're coming up in a random order, but they come up this way every time--maybe in the order in which I created the check boxes? Is there any way to change the order besides physically moving the boxes around on the form?

3) I created a macro to prevent mutually exclusive boxes from being checked at the same time (for instance, if the "return all fields" radio button is ticked, it automatically checks all 32 available fields, but as soon as you "uncheck" one of the 32 checkboxes, the radio button switches from "return all fields" to "return selected fields:") This macro won't run unless you manually enable it every time you open the file. Is there a way to save the file to sharepoint so that the macro remains permanently enabled no matter who opens it, eliminating the extra step?

Thank you, thank you, thank you all for your help!

Joe
Nov 13 '09 #30

Sign in to post your reply or Sign up for a free account.

Similar topics

4
4633
by: Jay | last post by:
Hi everybody ! I am currently writing a webpage that displays a list of records ( each record has a checkbox associated with it) to allow users to select any record they want to delete (much like in "hotmail" or "yahoo" e-mail where u can select particular message to delete) ? Anybody have any idea how to do it ? And one more question, I like to write my page so that when the user
4
6170
by: Shufen | last post by:
Hi, I'm a newbie that just started to learn python, html and etc. I have some questions to ask and hope that someone can help me on. I'm trying to code a python script (with HTML) to get values from a html form that consists of about 10 checkbox and a textbox where user have to key in a value to perform a search. From python tutors, I learned that I have to use the following method:
6
1951
by: Megan | last post by:
Hi everybody- I'm trying to use a checkbox to control whether or not a date field in a query "Is Null" or "Is Not Null." I have 2 date fields: InDate and OutDate. If there is an OutDate, then the record is finished. If there is no OutDate, then the record is not finished. I'm using an unbound form (frmStatus) to enter data into a query to
4
7017
by: Dave Boyd | last post by:
Hi, I have two very similar forms each with a subform. The main form gets a few fields from the user and passes this back to a query that the subform is bound to. The requery is done when the user enters the last qualifying field on the main form. In one case this works fine, the subform shows the data the user wants to update -- which means showing all the data put in previously (ie showing this via the requery and the continuous...
3
11847
by: elyob | last post by:
Okay, I'm about to add some checkboxes to a form, and then store the data in a column on a MySQL database. Just wanted to know best practice, at the moment I'm writing and thinking .. God that's ungly code ... <input type="checkbox" name="delta" <?php if ($delta==1) { echo "checked"; }?>> Delta<br> <input type="checkbox" name="mastercard" <?php if ($mastercard==1) { echo "checked"; }?>> Mastercard<br> <input type="checkbox" name="solo"...
8
4462
by: Sid | last post by:
I hope someone could help me with this. I am trying to setup a criteria to decide when to allow/not allow user to click on the check box. logically it looks simple but I am not able to incorporate in my data access page. I have a numerical field called tier status and right next to it I have a checkbox. I would like to allow user to check and uncheck the checkbox as long as the tier
0
4100
by: cyberdawg999 | last post by:
Greetings all in ASP land I have overcome one obstacle that took me 2 weeks to overcome and I did it!!!!! I am so elated!! thank you to all who invested their time and energy towards helping me with my problems. Now for my new little problem,I had a problem posting the values from checkbox fields to a database and thats the obstacle I overcame. Now the second part is my new problem is that I want that the next time that page loads for...
11
4314
by: =?Utf-8?B?UGFyYWcgR2Fpa3dhZA==?= | last post by:
Hi All, I have a large recordset to be displayed on a ASP 3.0 page. I am using recordset paging for this. For the next and previous link i am passing href as <a href=<Page URl>?page=<%= iPageCurrent - 1 %for Previous. Now when i display the contents i also add a checkbox (for deletion) to each of the records. Now user should be able to select one or more checkboxes across pages and then should be allow to delete all selections together....
3
1632
scubak1w1
by: scubak1w1 | last post by:
Hello, I posted this in the HTML forum, but it was suggested I post it over here as well by a moderator. I have a form that will not 'fire' in non-IE browsers?? I have poked and poked at the code and can not find why. The AJAX changes the query button in non-IE browsers, at least in the sense it is not opaque anymore, the 'title' check changes, etc - it just doesn't "fire" when you click the icon/image in Mozilla, etc but DOES in Internet...
0
9561
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9381
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8252
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3316
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.