473,748 Members | 2,690 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
Not the Name of the Database but the Name of a Table.

OK--got it! That's what I meant, still learning the syntax.

Needs modification:
Expand|Select|Wrap|Line Numbers
  1. rSQL_2 ="SELECT" & Left$(strSQL, Len(strSQL)-2) & "FROM <Table Name> WHERE " & 'Criteria to follow
Still not sure I'm getting this right. Should <Table Name> be in brackets: [Table Name]? I've had to do that in the SQL view of the "old" query because the actual table name has a space in it. I'm assuming that this line of code defines the string "strSQL_2" which is then pasted by the "Set qdfDemo" command on the next line into an SQL statement much like what I constructed manually to build my "old" query in the SQL view. If this is true, I would assume that it needs the brackets, or I need to remove the space from the table name--something I'd rather not do because I have so much other code written which refers to it with the space in it.

Also, it looks like you've got everything between the FROM and the WHERE statement inside of double quotes. Then you put the & symbol, followed by single quotes, all the criteria code then...? a semi-colon, then double quotes?

Finally, I'm going to have to paste quite a few lines of code into the 'Criteria to follow...' section, because I've got about 20 parameters which are used in the query. Is there a length limit to strSQL_2 and, if so, is it possible to define multiple strings and them use them in the "Set qdfDemo" command line?

I will play around with different combinations to see what works--just asking the questions in advance in case I can't crack it.

Thanks again for your time! No way I'd even be close without your help.

Joe
Nov 12 '09 #11
ADezii
8,834 Recognized Expert Expert
Still not sure I'm getting this right. Should <Table Name> be in brackets:
If the Table Name contains a Space it must be in Brackets, if not no, as in:
Expand|Select|Wrap|Line Numbers
  1. [Table Name] <==> TableName
Also, it looks like you've got everything between the FROM and the WHERE statement inside of double quotes. Then you put the & symbol, followed by single quotes, all the criteria code then...? a semi-colon, then double quotes?
It can all be contained inside double quotes unless you have to refer to something explicitly. To must also use the proper Delimiters (' for Strings, # for Dates, etc.)
Numeric
Expand|Select|Wrap|Line Numbers
  1. "Select * From Employees Where [EmployeeID] = 9999;"
  2. "Select * From Employees Where [EmployeeID] = " & Me![txtEmployeeID] & ";"
String
Expand|Select|Wrap|Line Numbers
  1. "Select * From Employees Where [LastName] = 'Smith';"
  2. "Select * From Employees Where [LastName] = '" & Me![txtLast] & "';"
Date
Expand|Select|Wrap|Line Numbers
  1. "Select * From Employees Where [BirthDate] = #9/2/1954#;"
  2. "Select * From Employees Where [BirthDate] = #" & Me![txtDOB] & "#;"
Nov 12 '09 #12
dizzydangler
33 New Member
Still not working.

First question: looking back through what the code is doing, I just realized that the Check Box tags should probably be an exact match of the field names in the table being queried--when I've written code directly into the SQL view of a query (successfully!) , what follows after "SELECT" are strings like [Database 1]![Field 1], etc.

Quote:
If the Table Name contains a Space it must be in Brackets, if not no, as in:
Expand|Select|W rap|Line Numbers [Table Name] <==> TableName

Assume that if the answer to my first question is "yes," then this rule must apply to the Tags, as well, so I either write the tag name as [Field 1], or write the line 13 code as
Expand|Select|Wrap|Line Numbers
  1. strSQL = strSQL & "[ctl.Tag]" & ", "
  2.  
Does it make a difference which way I do it?

If the rest of the code was working, I would be able to solve this problem by trial and error, but I am still getting the same "Invalid SQL Statement; expected 'DELETE', etc..." when I run the query with one or more of the "field select" boxes ticked. Here is a simplified version of my code, with the criteria for only one field included in the "WHERE" statement (there are about 20 IIF statements, but I thought it would make more sense if I got the code to work with just one):
Expand|Select|Wrap|Line Numbers
  1. Private Sub SearchDB_Click()
  2. On Error Resume Next
  3. Dim ctl As Control
  4. Dim strSQL As String
  5. Dim strSQL_2 As String
  6. Dim qdf As DAO.QueryDef
  7. Dim qdfDemo As DAO.QueryDef
  8. Const conQUERY_NAME As String = "Search_Database"
  9.  
  10. For Each ctl In Me.Controls
  11. If ctl.ControlType = acCheckBox And ctl.Tag <> "" Then
  12. If ctl.Value Then
  13. strSQL = strSQL & ctl.Tag & ","
  14. End If
  15. End If
  16. Next
  17.  
  18. If strSQL = "" Then Exit Sub
  19. CurrentDb.QueryDefs.Delete conQUERY_NAME
  20. On Error GoTo Err_SearchDB_Click
  21. 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);"
  22.  
  23. Set qdfDemo = CurrentDb.CreateQueryDef(conQUERY_NAME, strSQL_2)
  24.  
  25.  
  26. Exit_SearchDB_Click:
  27. Exit Sub
  28.  
  29. Err_SearchDB_Click:
  30. MsgBox Err.Description, vbExclamation, "Error in SearchDB_Click()"
  31. Resume Exit_SearchDB_Click
  32. End Sub
  33.  
I tried it again, even more simplified, with no "WHERE" criteria, so the line 25 code looks like:
Expand|Select|Wrap|Line Numbers
  1. strSQL_2 = "SELECT" & Left$(strSQL, Len(strSQL) - 2) & "FROM [Database 1];"
  2.  
...same result.

There's got to be something wrong with where I'm putting the quotes (delimiters?), I just can't see it.
Nov 12 '09 #13
MMcCarthy
14,534 Recognized Expert Moderator MVP
I've made two minor changes to your code which I have commented. This should get you a little further ahead.

Expand|Select|Wrap|Line Numbers
  1. Private Sub SearchDB_Click()
  2. On Error Resume Next
  3. Dim ctl As Control
  4. Dim strSQL As String
  5. Dim strSQL_2 As String
  6. Dim qdf As DAO.QueryDef
  7. Dim qdfDemo As DAO.QueryDef
  8. Const conQUERY_NAME As String = "Search_Database"
  9.  
  10.     For Each ctl In Me.Controls
  11.         If ctl.ControlType = acCheckBox And ctl.Tag <> "" Then
  12.             If ctl.Value Then
  13.                 ' put space after comma
  14.                 strSQL = strSQL & ctl.Tag & ", "
  15.             End If
  16.         End If
  17.     Next
  18.  
  19.  
  20.     If strSQL = "" Then Exit Sub
  21.     CurrentDb.QueryDefs.Delete conQUERY_NAME
  22.     On Error GoTo Err_SearchDB_Click
  23.  
  24.     ' add space after SELECT, before FROM and after WHERE
  25.     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);"
  26.  
  27.     Set qdfDemo = CurrentDb.CreateQueryDef(conQUERY_NAME, strSQL_2)
  28.  
  29.  
  30. Exit_SearchDB_Click:
  31.     Exit Sub
  32.  
  33. Err_SearchDB_Click:
  34.     MsgBox Err.Description, vbExclamation, "Error in SearchDB_Click()"
  35.     Resume Exit_SearchDB_Click
  36. End Sub
  37.  
Always remember the spaces before and after when substituting values into a string building a query statement.
Nov 12 '09 #14
dizzydangler
33 New Member
IT WORKS!!! (sort of!)

after reading over my last post, I went back and re-did all the tags on the "Select Field" checkboxes to match the field names in the [Database 1] table, including brackets [] when they were more than one word. I also realized, based on your explanation, that I needed to type a space after SELECT and before FROM

"SELECT " not "SELECT" and " FROM... not "FROM...

I ran it using a very simple criteria:
Expand|Select|Wrap|Line Numbers
  1. strSQL_2 = "SELECT" & Left$(strSQL, Len(strSQL) - 2) & "FROM [Database 1] WHERE [City] Is Not Null;"
  2.  
Nothing happened, but I got no error message. I went to close it out and, whaddaya know, a query table called Search_Database appears under an "Unrelated Objects" title bar in the Navigation View on the left!

I opened up the table and, sure enough, found all records listed where the field "City" was not null!

I played with the checkboxes and got it to build the query results table using with columns only showing the fields I selected. Getting a little cocky now, I went ahead and inserted one of my full-length criteria strings:
Expand|Select|Wrap|Line Numbers
  1. strSQL_2 = "SELECT" & Left$(strSQL, Len(strSQL) - 2) & "FROM [Database 1] WHERE ((IIF([City] Is Null, (([Database 1].[City] Like "*" & [City] & "*" Or (([Database 1].[City] Like "*" & [City] & "*")))<>False);"
  2.  
This criteria worked fine in the original query (run from the form called <Search Database>), written like:
Expand|Select|Wrap|Line Numbers
  1. WHERE ((IIF([Forms]![Search Database]![City] Is Null, (([Database 1].[City] Like "*" & [Forms]![Search Database]![City] & "*" Or (([Database 1].[City] Like "*" & [Forms]![Search Database]![City] & "*")))<>False);
  2.  
It returns all records if the control <City> is left null but if anything is entered, it returns all records containing the text entered in the control.

All I did was drop the [Forms]![Search Database]! from in front of the control fields. I ran it that way the first time, got a "Type Mismatch" error, then figured I didn't need the form reference since I was running the query straight from the form itself (and your examples didn't have it, either).

No dice, still got the "Type Mismatch" error.

So, my questions are, in order of importance:

1) what is wrong with my criteria formatting?
2) how do I get the query results to automatically pop up in a new table, instead of showing up in the navigation bar after I close the form? (when I hang the DB on the server, the navigation bar is hidden to protect the users--all excel junkies like myself--from the temptation to open up the master table and start plowing through the data to find what they want. So, I need the table results to pop up automatically)
3) Is there a way to control the order of the columns in the query results?

ADezii, you've helped me way more than I can tell you. This is not a life-or-death issue, by any means, and I don't want to impose on your time and patience. At this point, I'm working to provide a more convenient and user-friendly product to the users and trying to learn something useful in the process. (and, OK, I am REALLY stubborn, especially when I KNOW there's got to be a way to get this thing to do what I want it to.) Any help is appreciated, but I'll understand if you want to call it quits. I'll just take my clown show to another forum...:)

Joe
Nov 12 '09 #15
dizzydangler
33 New Member
Hey again, msquared! I caught the spacing error in the "," and the "SELECT" and "FROM... statements, but I didn't get the ...WHERE " & "((IIF... bit. Let me give it a try....
Nov 12 '09 #16
dizzydangler
33 New Member
Nope, no luck. I tried:
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);"
  2.  
and
Expand|Select|Wrap|Line Numbers
  1. strSQL_2 = "SELECT " & Left$(strSQL, Len(strSQL) - 2) & " FROM [Database 1] WHERE ” & “((IIf([City] Is Null,(([Database 1].[City]) Like " * " & [City] & " * " Or ([Database 1].[City]) Is Null),(([Database 1].[City]) Like (" * " & [City] & " * "))))<>False);"
  2.  
both give me the "Type Mismatch" error. Could it be something else?

Joe
Nov 12 '09 #17
MMcCarthy
14,534 Recognized Expert Moderator MVP
Hi Joe

Firstly remember to add code tags to your posts as it is confusing to follow otherwise.

I'm just looking at your criteria now and will get back shortly.

Mary
Nov 12 '09 #18
MMcCarthy
14,534 Recognized Expert Moderator MVP
Joe

Your Iff statement doesn't make sense to me. Can you tell me in English what the logic of this is supposed to say.

Mary
Nov 12 '09 #19
MMcCarthy
14,534 Recognized Expert Moderator MVP
OK Joe

Try this ...

Expand|Select|Wrap|Line Numbers
  1. strSQL_2 = "SELECT " & Left$(strSQL, Len(strSQL) - 2) & " FROM [Database 1] " & _
  2. "WHERE [Forms]![Search Database]![City] Is Null " & _
  3. "OR [Database 1].[City]) Like ' * ' & [Forms]![Search Database]![City] & ' * ' "
  4.  
I don't think you should have been using IIF at all. Also I replaced the double quotes around the * with single quotes. You can't put double quotes inside a string (not easily at least) so you have to use single quotes.

Mary
Nov 12 '09 #20

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
8832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
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...
1
9332
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.