473,748 Members | 2,625 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
ADezii
8,834 Recognized Expert Expert
I prefer the single strWhere approach, and I'll show you how it can be done with Multiple criteria when I get a chance. Someone will probably beat me to the punch, and that is fine.
Nov 19 '09 #41
NeoPa
32,571 Recognized Expert Moderator MVP
@Tarheel
That sounds like a good design.

As far as one or more string variables goes, it really only matters what you end up with as a result (in the SQL string). If you use multiple variables, be careful not to concatenate them together, forgetting to leave a space or other white-space characters between them. I include below some advice I often give out about working with SQL in string variables.
One of the most popular (frequently occurring rather than best liked) problems we get is with SQL strings being manipulated in VBA code.

The reason this is so difficult is that all the work is being done at a level of redirection. What I mean by this is that the coder is never working directly with the SQL itself, but rather with code which in turn, is relied on to produce the SQL that they are envisaging is required. It's rather similar to the problems coders have historically had dealing with pointers.

Anyway, a technique I often suggest to coders struggling with this (at any level. This happens to experienced coders too.) is to use either the MsgBox() function, or Debug.Print into the Immediate Pane of the debugger window, to display the value of the SQL in the string before using it (That's assuming you're not good with debugging generally. Personally I would trace to the line, then display the value prior to allowing execution of the string - See Debugging in VBA). It's really much easier to appreciate what a SQL string is meant to do, and where there may be problems, when you can see it in its entirety, and in its true form, rather than as the code is about to create it.
Nov 19 '09 #42
NeoPa
32,571 Recognized Expert Moderator MVP
NB. I moved another (hijack) post by the same member (Tarheel) to Cascading ComboBoxes.
Nov 19 '09 #43
ADezii
8,834 Recognized Expert Expert
@Tarheel
Hello Tarheel. Since you have been closely monitoring this Thread, I used the existing code, only revised to demo every possible combination of Criteria on three Fields, namely: [Region], [City], and [Title]. SQL is not my strong point, so do not take my code as the ultimate truth. Members like NeoPa, Stewart, missinglinq, etc. are better than I at this. Use this as a guideline, but follow their further advice if given.
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 = " [City] ='" & Me![txtCity] & "' AND "
  13. End If
  14.  
  15. 'If Criteria is specified for the [Region] Field, the Check Box must be selected
  16. If Not IsNull(Me![txtRegion]) Then
  17.   Me![Check14].Value = True
  18.     strWhere = strWhere & " [Region] ='" & Me![txtRegion] & "' AND "
  19. End If
  20.  
  21. 'If Criteria is specified for the [Title] Field, the Check Box must be selected
  22. If Not IsNull(Me![txtTitle]) Then
  23.   Me![Check6].Value = True
  24.     strWhere = strWhere & " [Title] ='" & Me![txtTitle] & "' AND "
  25. End If
  26.  
  27. For Each ctl In Me.Controls                 'Query every Control on the Form
  28.   If ctl.ControlType = acCheckBox Then      'Is it a Check Box?
  29.     If ctl.Value Then                       'Is it selected?
  30.       strSQL = strSQL & ctl.Tag & " ,"      'Build the Field Selection(s)
  31.     End If
  32.   End If
  33. Next
  34.  
  35. If strSQL = "" Then Exit Sub        'No Fields selected, get out!
  36.  
  37. CurrentDb.QueryDefs.Delete conQUERY_NAME        'DELETE existing Query
  38.  
  39. On Error GoTo Err_cmdTest_Click
  40.  
  41. 'Build complete SQL Statement
  42. If strWhere = "" Then
  43.   strSQL_2 = "SELECT " & Left$(strSQL, Len(strSQL) - 2) & " FROM Employees;"
  44. Else
  45.   strWhere = Left$(strWhere, Len(strWhere) - 5)
  46.   strSQL_2 = "SELECT " & Left$(strSQL, Len(strSQL) - 2) & " FROM Employees WHERE " & strWhere & ";"
  47. End If
  48.  
  49. 'Create the QueryDef Object
  50. Set qdfDemo = CurrentDb.CreateQueryDef(conQUERY_NAME, strSQL_2)
  51.  
  52. DoCmd.OpenQuery "Selective Employee Fields", acViewNormal, acReadOnly
P.S. - Combo Boxes vs Text Boxes does not make a differe3nce, in this case it is only the Name of the Control that matters. Pay close attention to Lines 9 thru 25 and 42 thru 47.
Nov 19 '09 #44
Tarheel
7 New Member
Brilliant. Thanks so much, ADezii!

Works Perfectly

Sorry NeoPa, I figured I would pose a follow-up question while I had ADezii's attention. No cause for alarm.
Nov 19 '09 #45
NeoPa
32,571 Recognized Expert Moderator MVP
Absolutely no cause (As long as it doesn't happen again) ;D
Nov 19 '09 #46

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
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
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
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
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...
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.