473,395 Members | 1,539 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,395 software developers and data experts.

Form with 7 combo boxes

I have a form with 7 combo boxes to to search various fields in a table in Access 2007. I am trying to construct "dynamic" or conditional SQL for my subroutine, that would only use the values from selected comboboxes for the "where" criteria if a selection has been made, otherwise it will ignore the combo box.

For example if cmb1 is not selected, the where clause should look like:

select * from myTable where myField2= 'value of cmb2' and myField3= 'value of cmb3' and so on

If on the other hand cmb1 is selected, it would be included in the where clause.

How do I create this conditional SQL? or is there a better way to do it?

Thanks.
May 19 '10 #1
12 1733
TheSmileyCoder
2,322 Expert Mod 2GB
This is VBA code that allows you to dynamically createa SQL where clause.
Expand|Select|Wrap|Line Numbers
  1. Dim strWhere as string
  2.  
  3. If not isNull(Me.Cmb1) Then
  4.    if strWhere & "" <>"" then strWhere= strWhere & " AND "
  5.   strWhere=strWhere & " myField2='" & me.Cmb1 & "'"
  6. End If
Just repeat the If statement for Cmb2 and 3 (hopefully you give them more meaningfull names!)

Then you just need to combine the Where clause with the Select clause properly. If you don't know how to do this, I can write some more details on the matter.
May 19 '10 #2
@TheSmileyOne
Thanks for your response. I will try this tonight. Also, please provide the code for combining the "Where" with the "select" clause.

Thank you!
May 19 '10 #3
Jim Doherty
897 Expert 512MB
@rhapsodysolutions
Take a look at this little searchdemo db I did one time for a poster. Albeit done in an earlier version of Access ie not 2007 It has all the ingredients you need in the code behind to give you ideas

http://bytes.com/topic/access/answer...ed-search-form
May 20 '10 #4
@Jim Doherty
Thanks for the demo db. It is an impressive and exhaustive learning resource. I will save it and use it for my future projects.

@TheSmileyOne, your code worked perfectly! Here is what it looks like:

Expand|Select|Wrap|Line Numbers
  1.     Dim strWhere As String
  2.  
  3.     strWhere = " Where 1 = 1"
  4.  
  5.     If Not IsNull(Me.cmb_MediaType) Then
  6.        If strWhere & "" <> "" Then strWhere = strWhere & " AND "
  7.       strWhere = strWhere & " MediaType='" & Me.cmb_MediaType & "'"
  8.     End If
  9.  
  10.     If Not IsNull(Me.cmb_City) Then
  11.        If strWhere & "" <> "" Then strWhere = strWhere & " AND "
  12.       strWhere = strWhere & " Industry='" & Me.cmb_City & "'"
  13.     End If
  14.  
  15.     If Not IsNull(Me.cmb_market) Then
  16.        If strWhere & "" <> "" Then strWhere = strWhere & " AND "
  17.       strWhere = strWhere & " City='" & Me.cmb_market & "'"
  18.     End If
  19.  
  20.     If Not IsNull(Me.CmbMediaName) Then
  21.        If strWhere & "" <> "" Then strWhere = strWhere & " AND "
  22.       strWhere = strWhere & " MediaName='" & Me.CmbMediaName & "'"
  23.     End If
  24.  
  25.     If Not IsNull(Me.cmb_Client) Then
  26.        If strWhere & "" <> "" Then strWhere = strWhere & " AND "
  27.       strWhere = strWhere & " Client='" & Me.cmb_Client & "'"
  28.     End If
  29.  
  30.     If Not IsNull(Me.cmb_Lastname) Then
  31.        If strWhere & "" <> "" Then strWhere = strWhere & " AND "
  32.       strWhere = strWhere & " Lastname='" & Me.cmb_Lastname & "'"
  33.     End If
  34.  
  35.     If Not IsNull(Me.cmb_Title) Then
  36.        If strWhere & "" <> "" Then strWhere = strWhere & " AND "
  37.       strWhere = strWhere & " Title='" & Me.cmb_Title & "'"
  38.     End If
  39.  
  40.  
  41.         strSql = "select * from Master_Media_List" & strWhere & ";"
  42.         Me.RecordSource = strSql
Having tackled this, there are a couple of additional problems:

I also have a "Reset" button on that same form, that resets all comboboxes. However it does not reset the form in its original state- when it was first opened. Please let me know what I am doing wrong:

Expand|Select|Wrap|Line Numbers
  1. Private Sub btn_Reset_Click()
  2.  
  3.     Dim ctl As Control
  4.     For Each ctl In Me.Controls
  5.  
  6.     Select Case ctl.ControlType
  7.  
  8.     Case acComboBox
  9.     ctl.Value = Null
  10.  
  11.     End Select
  12.     Next ctl
  13.  
  14.     Me.Requery
  15.  
  16. End Sub
Secondly, how can I modify the code so that hitting "Enter" on the keyboard would run the query, instead of having to click the search button?

Thanks for all your help!
May 20 '10 #5
Jim Doherty
897 Expert 512MB
@rhapsodysolutions
You,re welcome with the db......The reason I picked up on your last post particularly was the long list of code blocks for each where clause. When you get chance look at the AddToWhere function that is in that db disassemble it, understand it and see how it is being called in the search routine. It cuts out needing to code block for each and every control used as criteria because it deals only with the value that is actually IN the control.

To make your search button just search when you hit enter make its property 'default' button setting = yes in the properties dialog for the button
May 20 '10 #6
@Jim Doherty
Thanks for the tip with the enter button! I will lookup the AddToWhere function as you recommend.

Could you please also check what is wrong with my "reset" code posted in #5 above.

And finally, how do I export the results into Excel? or should I post it in a seperate thread?

Thanks
May 20 '10 #7
Jim Doherty
897 Expert 512MB
@rhapsodysolutions
There is nothing wrong with you combobox clearance code. It does what it programmed to do...clear a combo and any others on screen. It is not programmed to do anything else. What do you expect it to do?

As for your excel output I dont want to keep promoting that db as the panacea to everything but you really have to LOOK at it. It has a full blown export to excel code module synchronised to the results of any forms underlying dataset. It is called by ONE line of code from any form.
May 20 '10 #8
@Jim Doherty
It does clear the combo boxes but does not reset the form in its "initially opened" state, going back to the first record. That is what I need it to do.

Thanks.
May 20 '10 #9
TheSmileyCoder
2,322 Expert Mod 2GB
You have to reset the Recordsource of the form:
Expand|Select|Wrap|Line Numbers
  1. Me.RecordSource="select * from Master_Media_List"
You should post any new question in a seperate thread, but before doing so try to search this forum (or the VBA browser) for Docmd.TransferSpreadSheet.
May 20 '10 #10
Jim Doherty
897 Expert 512MB
@rhapsodysolutions
Not that this makes much difference...but is your form single view or tabular if all you want is to go to the first record then create a command button using the wizard and look at what is offered to you for record navigation ie goto first next last previous. Just create a button and use the code that it generates in your own routine ie: rather obviously pasted into your reset procedure
May 20 '10 #11
TheSmileyOne, and Jim, Thank you for all your help. I have been able to complete the project. The search works fine. The reset button works fine too after resetting the RecordSource. I will go ahead and do some more poking around into Jim's db for the Excel export.
May 20 '10 #12
Jim Doherty
897 Expert 512MB
@rhapsodysolutions
Good luck with your project :)
May 20 '10 #13

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

Similar topics

3
by: vgrssrtrs | last post by:
<html> <head> <script language="JavaScript"> <!-- /* *** Multiple dynamic combo boxes *** by Mirko Elviro, 9 Mar 2005 *** ***Please do not remove this comment
2
by: visionstate | last post by:
Hi there, I am working on a form that uses 3 text boxes and 3 combo boxes. When any data is entered into any of these, I click a command button and this requeries a sub query in the form and...
1
by: akhenaton | last post by:
I have 6 tables total, 5 of them have two fields, ID and name, like so: Table1 (ID1, name1) Table2 (ID2, name2) .... Table5 (ID5, name5) The last table is an intersection table that brings...
6
by: Dave | last post by:
I want to put the information that the user selects in my combo boxes into a subform that lies on the same form as the combo boxes. Thanks for your help already, Dave
1
by: Dave | last post by:
Hello all, First I'd like to apologize...This post was meant to be put in my previous post, but I tried many times without success to reply within my previous post. Now here goes... I have a...
5
by: a Wellner | last post by:
I am trying to create a from that the user can select a street from a combo box (named street), then pick a second street from another combo box, containing only valid cross streets(named Cross). ...
2
by: Dave | last post by:
I have 3 tables of information feeding into 4 combo boxes on my main form (DR Form). I have as many list boxes (acting as text boxes) as there are fields in each one of the 3 tables. Once...
3
Zwoker
by: Zwoker | last post by:
Hi Everyone, I have a query that uses criteria from combo boxes in the form that runs it. When I had only discrete values in the combo boxes that matched the data that was being queried, the...
11
by: jgoodnight | last post by:
Hi, I have a form with three combo boxes: Area, Sub-Area, and Factor. I've set up the Sub-Area combo box to list only those areas that are part of the selected area. I've set up the Factor combo...
4
by: matt753 | last post by:
I am trying to move the location of two combo boxes to a different form, but it is not working very well. the two combo boxes are cmbEmployeeID and cmdPerformance. I am trying to move them to a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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
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...
0
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...

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.