473,387 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to control which colums are displayed as output by using query?

The question is a bit more complicated than it sounds I think. What I am trying to accomplish I will explain using an example:

I have a subform based on a query. This query is a data table that contains 5 columns. I have a textbox into which I input data, hit a command button, and it searches the query and displays matching items in the subform. However, it shows all 5 columns... What I would ideally like to do is have a checkbox that correspnds to each column and, when checked, that column is displayed in the results. So, if I have checkbox1 and checkbox2 selected, column1 and column2 are the only columns displayed as results in the subform.

If anyone knows how to pull this off that would be awesome. Additionally, if the question is unclear just let me know - it might be hard to conceptualize.

Thanks :)
Jul 1 '10 #1

✓ answered by Jim Doherty

@VBnewb13
I have had a play this afternoon, not as a solution, rather merely to give you an idea of what can be done when looked at from different perspectives. The logic of this it seems to me is to merely have the ability to view only the columns in datasheet view that you are interested in based on a selection from a multi selection capable list.

The following paste coded represents that contained in the attached zip file.

What the code does is based on the northwind employees table.
1) A multi select listbox is populated with the field names of employees.
2) You choose which fieldnames you are interested in then click a button.

The code generates a new form based on copying an empty template. Textboxes are generated in code, the form saved and then displayed via the subform.

This happens each time you click the button.

The creation of the new form is necessary in this instance because creating textboxes in code on any form only has a certain shelf life ie 754 maximum count over the life of the form. By creating a new form the count resets to zero.

The code below is not as tidy as it could be (ie: error handling it either works or it does not) but it does give you a flavour of the type of thing you can do given you wanted to display the data in a datasheet gridview via a form. It is only an idea!! This code does NOT address all eventualities for instance it is only creating textboxes. It is not looking at field types to determine which control type to create and so on. It will not handle OLE or Binary data. Like I say..... it is merely a flavour

I am sure you can read into this the searchability aspect of your table given it is only an SQL statement you could fit it in somewhere to suit your needs I,m sure

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdProcess_Click()
  2.     On Error Resume Next    '<<< No error handling invoked it either works or it doesnt
  3.     Dim ctl As Control
  4.     Dim intDataX As Integer, intDataY As Integer, iinti As Integer, intj As Integer
  5.     'position the controls on the form
  6.     intX = 1000
  7.     intY = 100
  8.     If Me!lstEmployees.ItemsSelected.Count = 0 Then
  9.         MsgBox "Select at least one item from the list first", vbExclamation, "System Message"
  10.         Me!lstEmployees.SetFocus
  11.         Exit Sub
  12.     End If
  13.     'adjust the subform to hook to the placeholder then open the newly created form in degin
  14.     ' and set its recordsource accordingly
  15.     Me!Submain.SourceObject = "fsubPlaceHolder"
  16.     'delete pre- existing form
  17.     DoCmd.DeleteObject acForm, "fsubEmployees"
  18.     'copy the template to a new form
  19.     DoCmd.CopyObject "", "fsubEmployees", acForm, "fsubEmployeesEmptyTemplate"
  20.     'and open in design
  21.     DoCmd.OpenForm "fsubEmployees", acDesign, "", "", , acHidden
  22.     Forms!fsubEmployees.RecordSource = "SELECT * FROM Employees"
  23.     'loop through the listbox and create a textbox for each field selected in the list
  24.     For Each varItem In Me!lstEmployees.ItemsSelected
  25.         Set ctl = CreateControl("fsubEmployees", acTextBox, , acDetail, lstEmployees.ItemData(varItem), _
  26.                                 intX, intY)
  27.         ctl.Name = lstEmployees.ItemData(varItem)
  28.     Next varItem
  29.     'close the form and save
  30.     DoCmd.Close acForm, "fsubEmployees", acSaveYes
  31.     'now adjust the subform to pick up on the newly created form having the fields for display
  32.     Me!Submain.SourceObject = "fsubEmployees"
  33. End Sub
  34.  
  35. Private Sub Reset(lst As Access.ListBox)
  36.     On Error Resume Next
  37.     Dim i As Integer
  38.     With lst
  39.         For i = (.ItemsSelected.Count - 1) To 0 Step -1
  40.             .Selected(.ItemsSelected(i)) = False
  41.         Next i
  42.     End With
  43. End Sub
  44.  
  45. Private Sub cmdReset_Click()
  46.     On Error Resume Next
  47.     Call Reset(Me!lstEmployees)
  48.     Me!Submain.SourceObject = "fsubPlaceholder"
  49. End Sub

12 1755
NeoPa
32,556 Expert Mod 16PB
Is the intention also to limit the searching to just those fields selected too?

As far as limiting the display is concerned the bound controls must be there for all fields. However, it is perfectly possible to hide the controls of your choice depending on the values of the CheckBoxes. Does this sound like what you're after?

An explanation for why you need this, may help conceptualisation.
Jul 1 '10 #2
@NeoPa
I need this because some tables I want to search through contain 20+ columns, most of which are irrelevant. I want the user to be able to select which columns they want to appear so they may be able to view more relevant information instead of scrolling through everything.

I am interested in how to hide the controls based on checkboxes. Does this mean I will be able to hide what columns appear?

Thanks again
Jul 2 '10 #3
NeoPa
32,556 Expert Mod 16PB
Hiding yes. That's relatively straightforward. Every control has a .Visible property. What is more intriguing is what you want done with the gaps. Obviously the design of the form or report is mainly done manually, but you can resize and reposition controls with your code too if you feel that would be worth the effort.
Jul 2 '10 #4
The subform is based on a query data table. Is it possible to choose which columns on the table appear in the subform itself?
Jul 2 '10 #5
NeoPa
32,556 Expert Mod 16PB
Yes. That is possible. Unfortunately, I have no idea what you mean by a query data table.

This is possible both when designing the form manually and from within code. Which are you interested in? Presumably the latter.
Jul 2 '10 #6
Jim Doherty
897 Expert 512MB
@VBnewb13
I have had a play this afternoon, not as a solution, rather merely to give you an idea of what can be done when looked at from different perspectives. The logic of this it seems to me is to merely have the ability to view only the columns in datasheet view that you are interested in based on a selection from a multi selection capable list.

The following paste coded represents that contained in the attached zip file.

What the code does is based on the northwind employees table.
1) A multi select listbox is populated with the field names of employees.
2) You choose which fieldnames you are interested in then click a button.

The code generates a new form based on copying an empty template. Textboxes are generated in code, the form saved and then displayed via the subform.

This happens each time you click the button.

The creation of the new form is necessary in this instance because creating textboxes in code on any form only has a certain shelf life ie 754 maximum count over the life of the form. By creating a new form the count resets to zero.

The code below is not as tidy as it could be (ie: error handling it either works or it does not) but it does give you a flavour of the type of thing you can do given you wanted to display the data in a datasheet gridview via a form. It is only an idea!! This code does NOT address all eventualities for instance it is only creating textboxes. It is not looking at field types to determine which control type to create and so on. It will not handle OLE or Binary data. Like I say..... it is merely a flavour

I am sure you can read into this the searchability aspect of your table given it is only an SQL statement you could fit it in somewhere to suit your needs I,m sure

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdProcess_Click()
  2.     On Error Resume Next    '<<< No error handling invoked it either works or it doesnt
  3.     Dim ctl As Control
  4.     Dim intDataX As Integer, intDataY As Integer, iinti As Integer, intj As Integer
  5.     'position the controls on the form
  6.     intX = 1000
  7.     intY = 100
  8.     If Me!lstEmployees.ItemsSelected.Count = 0 Then
  9.         MsgBox "Select at least one item from the list first", vbExclamation, "System Message"
  10.         Me!lstEmployees.SetFocus
  11.         Exit Sub
  12.     End If
  13.     'adjust the subform to hook to the placeholder then open the newly created form in degin
  14.     ' and set its recordsource accordingly
  15.     Me!Submain.SourceObject = "fsubPlaceHolder"
  16.     'delete pre- existing form
  17.     DoCmd.DeleteObject acForm, "fsubEmployees"
  18.     'copy the template to a new form
  19.     DoCmd.CopyObject "", "fsubEmployees", acForm, "fsubEmployeesEmptyTemplate"
  20.     'and open in design
  21.     DoCmd.OpenForm "fsubEmployees", acDesign, "", "", , acHidden
  22.     Forms!fsubEmployees.RecordSource = "SELECT * FROM Employees"
  23.     'loop through the listbox and create a textbox for each field selected in the list
  24.     For Each varItem In Me!lstEmployees.ItemsSelected
  25.         Set ctl = CreateControl("fsubEmployees", acTextBox, , acDetail, lstEmployees.ItemData(varItem), _
  26.                                 intX, intY)
  27.         ctl.Name = lstEmployees.ItemData(varItem)
  28.     Next varItem
  29.     'close the form and save
  30.     DoCmd.Close acForm, "fsubEmployees", acSaveYes
  31.     'now adjust the subform to pick up on the newly created form having the fields for display
  32.     Me!Submain.SourceObject = "fsubEmployees"
  33. End Sub
  34.  
  35. Private Sub Reset(lst As Access.ListBox)
  36.     On Error Resume Next
  37.     Dim i As Integer
  38.     With lst
  39.         For i = (.ItemsSelected.Count - 1) To 0 Step -1
  40.             .Selected(.ItemsSelected(i)) = False
  41.         Next i
  42.     End With
  43. End Sub
  44.  
  45. Private Sub cmdReset_Click()
  46.     On Error Resume Next
  47.     Call Reset(Me!lstEmployees)
  48.     Me!Submain.SourceObject = "fsubPlaceholder"
  49. End Sub
Attached Files
File Type: zip vbNewb13.zip (291.3 KB, 55 views)
Jul 3 '10 #7
@Jim Doherty
That worked out great once I renamed what I needed to! That is exactly what I was trying to accomplish
Jul 6 '10 #8
Jim Doherty
897 Expert 512MB
@VBnewb13
Glad it helped you :)

Like I say with development you could enhance it by coding into the routines things like fixing up a neater column headers by created labels in addition to screen textbox controls and also checking the datatypes of fields so that the control type created aligns with the datatype (ie: a checkbox for a Yes/No column instead of textboxes) and so forth. I have coded this into my copy since my last post, but given you are now happy that the initial version services your needs I will leave it at that.

Regards
Jul 6 '10 #9
@Jim Doherty
Hey Jim,

I thought I would pick your brain since you were clever enough to develop the previous code. I had a question about what you suggested...

Expand|Select|Wrap|Line Numbers
  1. Forms!fsubEmployees.RecordSource = "SELECT * FROM Employees"
I inserted a text box (named searchALL) to act as an 'all encompassing search' of the displayed data. I tried to be more specific with this line by saying

Expand|Select|Wrap|Line Numbers
  1. Forms!fsubEmployees.RecordSource = "SELECT * FROM Employees WHERE FIELD1 Like '*" & [Forms]![frmDataSelect]![SearchALL] & "*'" & "or FIELD2 Like '*" & [Forms]![frmDataSelect]![SearchALL] & "*'"
...etc etc

The problem with this is as follows: Say I have FIELD1 selected as the only column in the display and I search for the word "dog." Dog is in FIELD2 and, even though I don't have FIELD2 selected, the cell in the corresponding row in FIELD1 will show up.

Is there any way to make this searchbox so it only sorts through visible cells?

Thanks again
Jul 7 '10 #10
Jim Doherty
897 Expert 512MB
@VBnewb13
This seems to me to be the chicken/egg scenario. The form generation and accompanying controls created on the fly are based on your fields selections from a list. The logic goes no further than that currently.

Obviously whichever fields you select in that list currently will be displayed exactly in accordance with that and logically field1 WILL indeed be displayed 'irrespective' of the fact that field2 is the only column that contains the word 'DOG' if you have only field1 selected for display and not field2. The computer is in effect doing what it is told.

The process sequence here it seems to me is this.... in reality you are saying in data terms 'You have no idea WHICH column contains the word 'DOG' but whichever column it is you want THAT column to display and not irrelevant columns' ........(if I am reading you right)

If this analogy is correct then in any 'real sense' your thought processes should be fashioned to say this to yourself in the early design stages...'I need to determine where my data lies and once i have identified this only THEN I will go about generating the coding and display formats/workarounds whichever the case may be to make this display happen'.

From a practical, aesthetically pleasing visual display standpoint, if I were looking at an available list of fields contained in a listbox and my intention was to search ANY ONE of those columns for the word 'DOG' and using a generic unbound textbox mounted on a form and intending it to be a searchbox I would be wanting to do some 'pre-processing' before displaying anything.

In other words... If I entered something into that textbox and hit the enter key I would probably want to make the system search relevant columns according to the datatype ie 'DOG?' is text so ignore photo columns and datetime columns and only go for text columns that kind of thing.

With this logic in mind I would certainly consider 'looping' the data and returning a numeric count of which pertinent columns 'potentially' contain data relevant to a furry little animal that barks! and 'present' this as a numeric count PER FIELD listed as a second column adjacent in that listbox.

THIS then gives you the overall general idea of WHICH columns you should be selecting from that list before any display takes place.

With this type of design logic in place, you might even consider making the computer loop the listbox picking out those numeric values that have a 'data' count greater than zero and making the computer 'multi select' them itself. ONLY THEN would you hit a display button to present those columns accordinging to that auto selection.

Obviously this is a whole new raft of different coding technique that would need to be applied to your system carrying a good degree of sophistication (I accept you would only seek to employ this type of thing once you fully understood the concepts of how you might go about 'actually' implementing it technically)

It is most probably the way I would seek to attack it personally it if it were mine. I don't believe a straight SQL solution is particularly flexible nor friendly enough its own. That is why we have alternatives such as arrays and recordsets and a plethora of other data manipulation techniques.

Regards
Jul 8 '10 #11
@Jim Doherty
Yea, you're right, that makes perfect sense. I've given it some thought and I feel that I am goign to forgo that capability. It really does not affect what I am trying to do in such a way that it needs to be given attention.

I greatly appreciate your detailed answers. They were very helpful :)

Best
Jul 8 '10 #12
Jim Doherty
897 Expert 512MB
@VBnewb13
You're very welcome :)
Jul 8 '10 #13

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

Similar topics

1
by: bjbounce2002 | last post by:
Hello, I am using forms with command buttons to close form or run action queries. The error messages such as "Null value in required field" or "duplicate value in primary key" are suppressed....
1
by: anshul | last post by:
Can somebody tell me about state management in asp.net using Query Strings. I am just unable to understand this. Anshul
18
by: A.M | last post by:
Hi, Is there any way to call a WSS web service method by using browser and see the XML result in browser as well? I have been told that there is query string syntax for calling...
3
by: planetthoughtful | last post by:
Hi All, I'm curious to know if anyone can point me to some basic but complete examples of C# ASP.NET code that retrieves and displays records from an SQL Server table? I've been experimenting...
1
by: Anoop Nair | last post by:
Hi I am developing scripts in C# which can be used to test windows based applications. I use Win32 API's to perform click operations etc. To uniquely identify a control in a window rather than ...
2
by: dollyvishal | last post by:
How to get constraint information for MS Access tables using Query?
0
by: =?Utf-8?B?VG9ueSBBLg==?= | last post by:
I'm trying to develop a parameter query using Query Builder, the database is Access 2003. I placed a ? in the column to we quered and linked it to a checkbox on the form. When the user selects an...
3
manoj9849967222
by: manoj9849967222 | last post by:
HI All I have two tables One is "master" & the other one is "child". The master Table have filds Productcode -----Number Productname-------Text Child Table
1
by: viper888 | last post by:
Hi there, How can I automate may database backup using query analyser? My routine goes this way: 1. Every 5:00pm check Enterprise manager who is currenlty logged on to sql server. 2. If there...
1
by: baburmm | last post by:
Hi all, Can any one tell me, Export datas to a Excel file using query without openrowset method is there any other methods are posible for creating an Excel file to export it Thanks,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...

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.