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

Searching with multi-selects and textboxes.

Hello Bytes,

I hope this post finds you well on this wonderful Friday!

I've been kicking this code around for over a week now and cannot seem to find the correct syntax to handle all of the possible selections of my search form.

Would someone please help me correct my IF statements in this code used for searching.

Here's how it works.

I have a form with four or five unbound text boxes AND two multi-select list-boxes for criteria used in searching data.
What I cannot grasp is what my code needs to look like in order to handle the possible selections.

Every time I think I have it, I'll run another search with more or less criteria and it breaks.
Meaning, I'll add one thing in one textbox, and make two selections in the first list-box and search, it works.
Then I'll add one item in the second list-box and it breaks.
Or I'll add two items from the second list box and remove one from the first and it breaks.

I'm struggling with my 'If count > then mycriteria = and
If argcount > # then mycriteria =

I cannot work the logic out and it's killing me. Oh and I've learned that those parenthesis are crucial in getting back the right data.

Search Button Code
Expand|Select|Wrap|Line Numbers
  1.  
  2.   Public Sub Command4_ClickO
  3. 'On Error GoTo Err_View3lick
  4. Dim mysql As String, mycriteria As String, myrecordsource As String
  5. Dim argcount As Integer
  6. Dim count As Integer
  7. Dim i As Integer
  8. Dim x As Integer
  9.  
  10.  
  11.  
  12. mysql ="select * from [queryreport] where"
  13. addtowher [Find1], ''[ONS ID]", mycriteria, argcount
  14. addtowher [Find2], "[NOMENCLATURE]", mycriteria, argcount
  15. addtowher [Find3], "[LIN]", mycriteria, argcount
  16. addtowher [Find4], "[UICNEEDUNITNAME]", mycriteria, argcount
  17.  
  18.  
  19. 'first multi-select
  20. count = 0
  21. i = 0
  22. While i < Find8.ListCount
  23. If Find8.Selected(i) Then
  24. count =count + 1
  25. If count = 1 Then
  26. If argcount > 0 Then
  27. mycriteria = mycriteria & " AND"
  28. End If
  29. mycriteria = mycriteria & " ("
  30. Else
  31. mycriteria = mycriteria & " OR "
  32. End If
  33. mycriteria = mycriteria & "[G3 APPROVE] = """ & Find8.Column(O, i) & """"
  34. End If
  35. i =i + 1
  36. Wend
  37.  
  38. 'Second Multi-Select
  39. count =0
  40. x=O
  41. While x < Find9.ListCount
  42. If Find9.Selected(x) Then
  43. count = count + 1
  44. If count> 1 Then
  45. If argcount > 0 Then
  46. mycriteria = mycriteria & " OR "
  47. End If
  48. mycriteria = mycriteria & " ("
  49. Else
  50. mycriteria = mycriteria & " AND "
  51. End If
  52. mycriteria =mycriteria & "[STATUS] ='''''' & Find9.Column(O, x) & """"
  53. End If
  54. x=x+1
  55. Wend
  56.  
  57. If argcount = 1 Then
  58. mycriteria = mycriteria
  59. End If
  60.  
  61. 'if nothing specified return all
  62.  
  63. If mycriteria = "" Then
  64. mycriteria = "True"
  65. End If
  66.  
  67. myrecordsource = mysql & mycriteria
  68.  
  69. Me![subsearch].Form.RecordSource = myrecordsource
  70.  
  71. Exit_VIEW_Click:
  72. Exit Sub
  73.  
  74. Err VIEW Click:
  75. MsgBoxError$
  76. Resume Exit_VIEW_Click
  77.  
  78. End Sub
  79.  



Module Code

Expand|Select|Wrap|Line Numbers
  1.  
  2. Public Sub addtowher(fieldvalue As Variant, fieldname As String, mycriteria As String, argcount As Integer)
  3.  
  4. 'Create criteria for WHERE clause
  5. If fieldvalue <> "" Then
  6.  
  7. 'Add "and" if other criterion exist
  8. If argcount > 0 Then
  9. mycriteria =mycriteria & " And"
  10. End If
  11. mycriteria =(mycriteria & fieldname & " Like" & Chr(39) & fieldvalue & Chr(42) & Chr(39))
  12.  
  13. 'increase arg count
  14. argcount = argcount + 1
  15. End If  
  16.  

All the best Bytes and thank heavens it's Friday!

-Aaron
US ARMY
Dec 12 '08 #1
4 2003
FishVal
2,653 Expert 2GB
Hello, Aaron.

Well. Without getting deep into your code I could see that while you add left bracket to criteria string there is no code that add right bracket.
Did you try to debug your code?

Also, I recommend you to read the following article
Quotes (') and Double-Quotes (") - Where and When to use them.

Regards,
Fish.

P.S. The code doesn't look well structured - this results in duplicate similar code fragments and use of unnecessary variables.
Dec 13 '08 #2
@FishVal
Thanks for the links, I'll spend some time with them on Monday, perhaps I'll be able to work it out.
I'm sure the code looks fragged to someone versed in vb, no doubt it's from my many attempts at trying to learn/make it work.

Kind Regards,
Aaron
Dec 13 '08 #3
puppydogbuddy
1,923 Expert 1GB
Aaron,
I don't know if this is going to help or not, or whether the code works. I started this code when your post first appeared, then got distracted by clients, and did not finish my original thought until now. In the meantime you've changed your code. Hopefully, if nothing else, it may give you some ideas on how to approach your problem.

Expand|Select|Wrap|Line Numbers
  1. Public Sub AddToWhere(FieldValue As Variant, FieldName As String, MyCriteria As String) 
  2.  
  3. Dim ctrl as Control
  4.  
  5. 'The multiselect list box is trickier then textboxes because the user might have chosen  'more than one item, and you have to loop through the list box's items to see which ones 'have been selected:
  6.  
  7.  
  8.     '  Create criteria for WHERE clause. 
  9.     If Me.Ctrl = acTextbox and FieldValue <> "" Then 
  10.         '  Add "and" if textbox criterion exists. 
  11.             MyCriteria = MyCriteria & " and " 
  12.  
  13.        '  Add "Or" if listbox criterion exists.
  14.     ElseIf Me.Ctrl = acListbox and FieldValue <> "" Then
  15.                '  Append criterion to existing criteria. 
  16.                MyCriteria = MyCriteria & "("
  17.                       For Each [FieldName] In Me!lstBox.ItemsSelected
  18.                                MyCriteria = MyCriteria & "[FieldName]=" & Chr(34) & Me!lstBox.ItemData(FieldValue) & Chr(34) & " Or "
  19.                       Next
  20.                MyCriteria = MyCriteria & ")"
  21.              End If 
  22.   End If
  23.  
  24. End Sub
Dec 13 '08 #4
Thank you very much for the code snippet.
I'm going to try an integrate it with what I have above.

Very Respectfully,
Aaron
Dec 15 '08 #5

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

Similar topics

3
by: sal achhala | last post by:
I'm working with java and XML documents in order to search for keywords in a given element name, eg element name 'author' == "jo blogs". The problem is the XML documents are downloaded (this...
12
by: * ProteanThread * | last post by:
but depends upon the clique: ...
0
by: frankenberry | last post by:
I have multi-page tiff files. I need to extract individual frames from the multi-page tiffs and save them as single-page tiffs. 95% of the time I receive multi-page tiffs containing 1 or more black...
6
by: cody | last post by:
What are multi file assemblies good for? What are the advantages of using multiple assemblies (A.DLL+B.DLL) vs. a single multi file assembly (A.DLL+A.NETMODULE)?
5
by: bobwansink | last post by:
Hi, I'm relatively new to programming and I would like to create a C++ multi user program. It's for a project for school. This means I will have to write a paper about the theory too. Does anyone...
4
by: Jon Paal | last post by:
how can I search for a value in a multi - item arraylist to prevent adding duplicates ?
3
by: Aaron | last post by:
I'm trying to parse a table on a webpage to pull down some data I need. The page is based off of information entered into a form. when you submit the data from the form it displays a...
15
by: Gigs_ | last post by:
Hi all! I have text file (english-croatian dictionary) with words in it in alphabetical order. This file contains 179999 words in this format: english word: croatian word I want to make...
5
by: peruron | last post by:
Hello again! I've been discussing the advantages of Python over C-Shell. I was asked if I can replace the use of "grep" in searching for a specific string in a multi-file directory - I have to go...
9
by: drhowarddrfine | last post by:
I don't want to use a db manager, like mysql, for such a small database but I'm finding this trickier than I thought and hope someone can provide some guidance. I have a restaurant menu with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
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,...

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.