473,508 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Parameter to select which column to search

11 New Member
Hi everyone, I have used parameters before to search a column but is there a way to use a parameter to select which column I want to search?

Example:
I have three columns: a, b, c
I was the query to ask for input and the user can select which column to search. I thought this would be simple but I must be missing something.
Aug 6 '15 #1
6 1821
Seth Schrock
2,965 Recognized Expert Specialist
I don't know of a way to do this just using SQL, but it is very easy with a little VBA. I just use a combo box with the fields that I want to search and have a textbox for the value that I'm searching for. Then I use VBA to build the query string. From there, you can do all kinds of things including change a query's SQL code.
Aug 6 '15 #2
fyamoahaxracer
11 New Member
Thanks Seth! I was hoping to just use SQL but I do not think it is possible so I will probably have to use that method. Thanks for your help!

Edit: You don't happen to have a small sample of this do you?
Aug 6 '15 #3
Seth Schrock
2,965 Recognized Expert Specialist
I do somewhere, but it will take me a little while to find it. I will post it when I do (won't be until tomorrow).
Aug 6 '15 #4
Seth Schrock
2,965 Recognized Expert Specialist
Here is what I did. I have a textbox called txtValue, a combo box for my field list called cboField, and a button that performs the operation. In this case, I'm using a filter on my form and not editing the query's SQL.

This is in the button's OnClick event.
Expand|Select|Wrap|Line Numbers
  1. Dim strSearch As String
  2. Dim strFilter As String
  3.  
  4. If Me.txtValue & "" <> "" And Not IsNull(Me.cboField) Then
  5.     If Me.cboField = "CustSSN" Then
  6.         strSearch = SSNSearchString(Me.txtValue)
  7.     Else
  8.         strSearch = Me.txtValue & "*"
  9.     End If
  10.  
  11.     strFilter = Me.cboField & " Like """ & strSearch & """"
  12.     Me.Filter = strFilter
  13.     Me.FilterOn = True
  14. Else
  15.     MsgBox "You must provide both a search value and field to perform the search."
  16. End If
Aug 7 '15 #5
fyamoahaxracer
11 New Member
Expand|Select|Wrap|Line Numbers
  1. SELECT [Query].Name AS Name, Cint(Count( [Query].[Name] )) AS [Count]
  2. FROM [Query]
  3. WHERE (((Len([Query].[COLUMN_NAME]))<>"1" And (Len([Query].[COLUMN_NAME]))<>"2"))
  4. GROUP BY [Query].Name
Can you point me in the correct direction on how to use a combo box and VBA code for the above query? The combo box will have different values and set the value for COLUMN_NAME.

I created the combo box to be filled with the values from another table but am not sure where to start with this. I'm also not sure how to display a query on a form if the query is created with VBA code. I am looking into this now.
Aug 10 '15 #6
Seth Schrock
2,965 Recognized Expert Specialist
Please use [CODE/] tags when posting code.

In this case, you would need to edit the query's SQL because of the GROUP BY. So your code would then be
Expand|Select|Wrap|Line Numbers
  1. Dim strQuery As String
  2. Dim db As DAO.Database
  3. Dim qry As DAO.QueryDef
  4.  
  5. If Not IsNull(Me.cboField) Then
  6.     Set db = CurrentDb
  7.     Set qry = db.QueryDefs("Your Query Name Here")
  8.  
  9.     strQuery = "SELECT [Query].Name AS Name, Cint(Count( [Query].[Name] )) AS [Count] " & _
  10.                "FROM [Query] " & _
  11.                "WHERE (((Len(" & Me.cboField & "))<>1 " & _
  12.                "And (Len(" & Me.cboField & "))<>2)) " & _
  13.                "GROUP BY [Query].Name"
  14.  
  15.     qry.SQL = strQuery
  16.  
  17.     qry.Close
  18.     Set qry = Nothing
  19.     Set db = Nothing
  20.  
  21.     Me.Requery
  22. Else
  23.     MsgBox "You must provide a field to perform the search."
  24. End If
Aug 10 '15 #7

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

Similar topics

5
1826
by: Klemens | last post by:
Hi, select user from table will return the token user what can I do to get the information from the column user Thanks Klemens
0
1063
by: Sinan | last post by:
Hi I am using a datagrid with a Select Column and the SelectedIndexChanged event captures the values of the cells in the selected row. It works fine before sorting any column. The problem...
2
1125
by: cj | last post by:
This is only an example not an actual project. Say I have 200 lines of code like this: Dim reqType As String = wuReq.Substring(8, 3) Dim reqCust As String = wuReq.Substring(11, 23) Dim...
0
1037
by: Greg Smith | last post by:
I would like to assign the value of a column to the text of the 'Select' column. i.e. In stead of 'Select' it would have the value in the column. Is this do-able? Any help is greatly...
4
5996
by: Luqman | last post by:
I have populated the Child Accounts and Parent Accounts in a Grid View Control, I want to hide the Select Column of Parent Accounts, but not the Child Accounts, is it possible ? I am using VS...
8
1919
by: ponderena | last post by:
I have a situation where a person can have more then one item ordered. I need to layout the information as follows: Person Items Ordered Item Description...
4
2160
ilikesuresh
by: ilikesuresh | last post by:
hi i have a table (table_Days)that contains columns d0 d1 d2 d3 ...............d30 based on my result i want to either select d0 or d1 or d3 ....d30 at runtime and get the respected value on...
0
1276
by: Andrus | last post by:
How to create select columns list dynamically in DLinq ? I want to create something like Console.WriteLine("Enter list of columns to return, separated by commas:"); string list =...
1
2052
by: John Cazaly | last post by:
I have what wouold at first seem a simple request: to create a datagrid / View or similar for an Orders table with OrdID and OrderDate fields (plus some others). I want to select OrderDaate >= From...
2
2375
by: microsoftsucks | last post by:
I have two forms. One form is where user enters the search criteria and then on the second form he sees the search results where he has the ability to update, delete the records. My question the...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7115
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
7321
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,...
0
7377
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...
0
7489
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...
0
5624
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,...
0
4705
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...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.