Hello, i received this code and it is suppossed to be used with an option button that when sleceted will take all the selected values from a list box and put them together, which will allow me to do queries bases on the AND. Will it work? alos, can i just change the word “AND” at the end to “OR” in order to incorporate a option button that when selected will put all selected values together as OR statements and allow me to query???? Thank you.
![]() |
There are isolated problems with current patches, but they are well-known and documented on this site. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
AND/OR statements (97)
Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » AND/OR statements (97)
- This topic has 10 replies, 3 voices, and was last updated 23 years, 5 months ago.
AuthorTopicWSscrappe7
AskWoody LoungerDecember 27, 2001 at 2:40 pm #364576Viewing 0 reply threadsAuthorReplies-
WSKevin
AskWoody Lounger -
WSscrappe7
AskWoody LoungerDecember 27, 2001 at 7:32 pm #560144forgetting the code might be a problem:
This simple code will pull the selected values from a list box, separate them by ” AND “, and append them to a string variable.
Dim x As Integer
Dim i As Integer
Dim strCriteria As StringFor x = 0 To List1.ListCount – 1
If List1.Selected(x) Then
i = i + 1
strCriteria = strCriteria & x
If i < List1.SelCount Then
strCriteria = strCriteria & " AND "
End If
End If
Next x -
WSscrappe7
AskWoody LoungerDecember 27, 2001 at 8:33 pm #560174what i actually need though is a AND/OR over an entire form. I have a form with 6 or so list boxes. i want to select 1 piece of info from each list box and then run my query based on my selections. I have that part programmed. i would like to add 2 check boxes to eah list box. 1 for AND and 1 for OR statements. I would the user to be able to select which list box values must be (AND) contained in the query and which ones are may be conatined (OR). It should let the user have full control on which dcriteria ia mandatory and which is optional. Make sense? this code does it within te listbox correct? i need one to set AND/OR properties for comparable listboxes against eachother not within 1 listbox. hope i didnt confuse you.
-
WSKevin
AskWoody LoungerDecember 27, 2001 at 9:38 pm #560195Use radio (option) buttons instead of checkboxes to select the operator.
Then just make your operators variables set by the radio button value.
LstBox1Op = iif(optBox1,”AND”,”OR”)
LstBox2Op = iif(optBox2,”AND”,”OR”), etc.Then build your criteria string.
I’d create a function to that takes as arguments the listbox and the bitwise operator (and or).
strCriteria = MakeCriteria(lstBox1,LstBox1Op)
strCriteria = strCriteria & MakeCriteria(lstBox2,LstBox2Op)
etc. calling the function once for each list box. -
WSscrappe7
AskWoody LoungerDecember 28, 2001 at 2:38 pm #560297Thanks Kevin,
I am not to familiar with VB and i really need this code. Do you think you could give me some more details on what EXACTLY to do. for example where does this code get entered within each option button? If it is the code per each option button if you could write out the full sample code then i can just paste and fix it for the other buttons. my problem is i can’t really tell what you are doing so i am completly lost. Thank you very much, this would solve a huge problem for us.
Ed
-
WScharlotte
AskWoody LoungerDecember 28, 2001 at 8:53 pm #560415You don’t put it behind the radio buttons at all. All they are there for is to capture a flag that says “use an AND” or “use an OR” to join this condition to the others. In a single routine that actually runs the query, you would have code that reads the individual option groups and uses their values to build a critieria string. However, if this is a saved query, you have a somewhat different problem because you’ll have to find a way to pass the parameters to it.
Is this a saved query or are you creating it in code? If you’re creating it in code, what are you doing with it?
-
WSscrappe7
AskWoody LoungerDecember 31, 2001 at 2:54 pm #560828The query was made the normal way, i did not use code to create it. It is a basic query all i entered was teh field, table, show(check/uncheck), and the criteria and then repeated for each field. I think that is what you are looking for.
So i just add the radio buttons assign them names and do nothing woth them but place a caption for each that says “AND” and 1 that says “OR”. Alll the coding would be in my command button that executes the query. When entering the code i assume it would be:
Private Sub Command28_Click()
LstBox1Op = iif(optBox1,”AND”,”OR”) ‘ where LstBox1Op is the AND option button
LstBox2Op = iif(optBox2,”AND”,”OR”) ‘where LstBox2Op is also the AND option for the 2nd listboxSo I take it my OR codes that will be placed in my command button would be
LstBox3Op = iif(optBox1,”OR”, “AND”) ‘ where LstBox1Op is the 1st OR option button
LstBox4Op = iif(optBox2,”OR”,”AND”) ‘where LstBox2Op is the 2nd OR option for the 2nd listbox
??????what the heck is this part doing:
strCriteria = MakeCriteria(lstBox1,LstBox1Op)
strCriteria = strCriteria & MakeCriteria(lstBox2,LstBox2Op)My guess is it assigns the values in the 1st list box based on whther or not the option button is selected, to a string variable and then adds that to the selected options for the 2nd list box. Thus making a long string to perform the query. If there were more than 2 listboxes the next lines would read
strCriteria = MakeCriteria(lstBox1,LstBox1Op)
strCriteria = strCriteria & MakeCriteria(lstBox2,LstBox2Op)
strCriteria = strCriteria & strCriteria & (lstBox3,LstBox3op)
strCriteria = strCriteria & strCriteria & strCriteria & (lstBox4,LstBox4op)I look at this and it makes no programming sense to me, i guess because i am not a good programmer. Am i on the right track at least?? So ill put this all into by command button on the event click and this will allow me to select multiple choices from a list box, decide which listboxes must(AND) / may(OR) be included in the query. Others have mentioned a foreign term to be called OLE that i will need, is this it? I hope so becuase i am lost enough as is. Thank you very much, happy new year!!!
-
WScharlotte
AskWoody LoungerJanuary 1, 2002 at 4:12 am #560922I wasn’t asking how you created the query, I was asking whether it was a saved query (one you can see in the database window in Access) or if you were generating the SQL for the query in code and then running it.
MakeCriteria doesn’t do anything unless you’ve created the function as Kevin suggested. I believe his suggestion was intended to give you a function that would, when called, create that piece of the criteria string but would hide the ugly details. So instead of putting all that detail behind your command button, you would put code there tha calls MakeCriteria as many times as is required to generate the criteria string.
This line would call the function and pass it the listbox and the option button controls. The function would take those two controls and create and appropriate expression based on the value in lstBox1 and tack either an and or or onto the string based on the value in LstBox1Op.
strCriteria = MakeCriteria(lstBox1,LstBox1Op)
When you needed the second piece of the expression, you would call the MakeCriteria function again, passing it a different listbox and option button, and it would obligingly return a string based on those two controls. The concatenation simply means take whatever is already in strCriteria and concatenate the result I get from this function onto it.
strCriteria = strCriteria & MakeCriteria(lstBox2,LstBox2Op)
You would call it the same way each time, and you could even call it that way the first time, since strCriteria will be an empty string at that point. So for the 3rd listbox, your expression would be:
strCriteria = strCriteria & (lstBox3,LstBox3op)
-
WSscrappe7
AskWoody LoungerJanuary 2, 2002 at 1:50 pm #561071OK charlotte,
So i think i have the part that checks each button and appropriate list box. Since each listbox has 2 possibel options (AND/OR) wouldn’t it look more like this:
strCriteria = strCriteria& MakeCriteria(lstBox1,LstBox1Op)
strCriteria = strCriteria & MakeCriteria(lstBox1,LstBox2Op)OR am i doing something that says
IF lstbox1op = 1 then strCriteria = strCriteria& MakeCriteria(lstBox1,LstBox1Op)
IF lstbox2op =1 then strCriteria = strCriteria& MakeCriteria(lstBox1,LstBox2Op)Siince it is a saved query what is this whole pasing of the parameters issue. Doesnt sound too tempting for me. I guess the biggest problem is what that function would look like, i am assuming it is a function called MakeCriteria and tells access to look in that listbox for the values selected and decide whether or not it is an AND/OR field to be included in the query? If i’m right (slim chance) how would you write something like that, are their reserved words that do this for you? Thank you.
-
WScharlotte
AskWoody LoungerJanuary 3, 2002 at 3:37 am #561214Are you saying that each list box has an option *group* ? In that case, it only has one possible option–either AND or OR–and you can determine the value by referring to the group, which is what I assumed LstBox1Op was. Keep in mind that MakeCriteria will do whatever you need it to do. If I were writing it, I might create something like this:
Public Function MakeCriteria(ByRef lst As ListBox) As String Dim strField as String Dim varValue As Variant Dim strDelimiter as String varValue = lst.Value 'Create the field name part of the criteria 'based on which listbox was passed 'Note: this is where you could also set a delimiter ' these are just examples Select Case lst.Name Case "lstBox1" strField = "[Field1]" strDelimiter = Chr(39) Case "lstBox2" strField = "[Field2]" strDelimiter = "#" ...... End Select 'now piece them together to get the string MakeCriteria = strField & "=" & strDelimiter & varValue _ & strDelimiter End Function
Then in the calling routine, I would do something like this:
strCriteria = strCriteria & Choose(Me!LstBox1Op.Value, ” AND “, ” OR “) & MakeCriteria(Me!lstBox1)
So If the field for lstBox1 was called Field1, and it was a text field, and you had clicked on the “Widget” item in lstBox1, MakeCriteria would return a string like this:
"[Field1]='Widget'"
If you had selected the AND option in your option group, the strCriteria expression would result in this:
" AND [Field1]='Widget'"
Then the next time you called MakeCriteria, you would be concatenating a similar expression onto the existing strCriteria.
I’m not clear on what you want to do with the AND/OR, so I just guessed that it was intended to be used between expression and indicates that the matching listbox value was supposed to be joined to the criteria with an AND or OR. I also had to guess at how your routine might know which field it was supposed to be using for the listbox. I don’t recall you including that information or anything about the datatypes involved. It it were me, I’d either include a zero-width column with the field name in it and another with a datatype indicator, although you could also use the listbox’s tag property to hold one or the other as well.
As to the parameters issue, how were you planning to use the criteria string once you created it? That has never been clear to me.
-
-
-
-
Viewing 0 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
.NET 8.0 Desktop Runtime (v8.0.16) – Windows x86 Installer
by
WSmeyerbos
9 hours, 27 minutes ago -
Neowin poll : What do you plan to do on Windows 10 EOS
by
Alex5723
5 hours, 11 minutes ago -
May 31, 2025—KB5062170 (OS Builds 22621.5415 and 22631.5415 Out-of-band
by
Alex5723
8 hours, 1 minute ago -
Discover the Best AI Tools for Everything
by
Alex5723
8 hours, 10 minutes ago -
Edge Seems To Be Gaining Weight
by
bbearren
8 hours, 53 minutes ago -
Rufus is available from the MSFT Store
by
PL1
6 hours, 22 minutes ago -
Microsoft : Ending USB-C® Port Confusion
by
Alex5723
1 day, 9 hours ago -
KB5061768 update for Intel vPro processor
by
drmark
15 hours, 37 minutes ago -
Outlook 365 classic has exhausted all shared resources
by
drmark
11 hours, 49 minutes ago -
My Simple Word 2010 Macro Is Not Working
by
mbennett555
1 day, 4 hours ago -
Office gets current release
by
Susan Bradley
1 day, 7 hours ago -
FBI: Still Using One of These Old Routers? It’s Vulnerable to Hackers
by
Alex5723
2 days, 21 hours ago -
Windows AI Local Only no NPU required!
by
RetiredGeek
2 days, 6 hours ago -
Stop the OneDrive defaults
by
CWBillow
2 days, 22 hours ago -
Windows 11 Insider Preview build 27868 released to Canary
by
joep517
3 days, 8 hours ago -
X Suspends Encrypted DMs
by
Alex5723
3 days, 10 hours ago -
WSJ : My Robot and Me AI generated movie
by
Alex5723
3 days, 10 hours ago -
Botnet hacks 9,000+ ASUS routers to add persistent SSH backdoor
by
Alex5723
3 days, 11 hours ago -
OpenAI model sabotages shutdown code
by
Cybertooth
3 days, 12 hours ago -
Backup and access old e-mails after company e-mail address is terminated
by
M W Leijendekker
3 days ago -
Enabling Secureboot
by
ITguy
3 days, 7 hours ago -
Windows hosting exposes additional bugs
by
Susan Bradley
3 days, 20 hours ago -
No more rounded corners??
by
CWBillow
3 days, 15 hours ago -
Android 15 and IPV6
by
Win7and10
3 days, 5 hours ago -
KB5058405 might fail to install with recovery error 0xc0000098 in ACPI.sys
by
Susan Bradley
4 days, 8 hours ago -
T-Mobile’s T-Life App has a “Screen Recording Tool” Turned on
by
Alex5723
4 days, 11 hours ago -
Windows 11 Insider Preview Build 26100.4202 (24H2) released to Release Preview
by
joep517
4 days, 5 hours ago -
Windows Update orchestration platform to update all software
by
Alex5723
4 days, 18 hours ago -
May preview updates
by
Susan Bradley
4 days, 5 hours ago -
Microsoft releases KB5061977 Windows 11 24H2, Server 2025 emergency out of band
by
Alex5723
3 days, 21 hours ago
Recent blog posts
Key Links
S | M | T | W | T | F | S |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.