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

Wherecondition troubles with multiple entries

Hi everyone, I'm new to these forums, but I'm having a bit of an issue. I'm basically trying to get the coding right on the wherecondition, but can't seem to figure out the correct formatting for using multiple entries. This is being used to make a report that pulls selected addresses by name. List2 is a List box, and the For loop creates data that looks like this: "Bill" OR "Jim" OR "Sandy"

Anyway, here's the code.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command11_Click()
  2. On Error GoTo Err_Command11_Click
  3.  
  4.     Dim x As Integer
  5.     Dim y As String
  6.     Dim z As String
  7.     Dim stDocName As String
  8.  
  9.     z = ""
  10.     For x = 0 To List2.ListCount - 1
  11.         y = List2.Column(0, x)
  12.         y = """" & y & """"
  13.         If x = 0 Then
  14.             z = y
  15.         Else
  16.             z = z & " OR " & y
  17.         End If
  18.     Next x
  19.     stDocName = "rptAddresses"
  20.     DoCmd.OpenReport stDocName, acPreview, , "ccustno='" & z & "'"
  21.  
  22. Exit_Command11_Click:
  23.     Exit Sub
  24.  
  25. Err_Command11_Click:
  26.     MsgBox Err.Description
  27.     Resume Exit_Command11_Click
  28.  
  29. End Sub
Jul 13 '07 #1
9 1755
ADezii
8,834 Expert 8TB
Hi everyone, I'm new to these forums, but I'm having a bit of an issue. I'm basically trying to get the coding right on the wherecondition, but can't seem to figure out the correct formatting for using multiple entries. This is being used to make a report that pulls selected addresses by name. List2 is a List box, and the For loop creates data that looks like this: "Bill" OR "Jim" OR "Sandy"

Anyway, here's the code.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command11_Click()
  2. On Error GoTo Err_Command11_Click
  3.  
  4.     Dim x As Integer
  5.     Dim y As String
  6.     Dim z As String
  7.     Dim stDocName As String
  8.  
  9.     z = ""
  10.     For x = 0 To List2.ListCount - 1
  11.         y = List2.Column(0, x)
  12.         y = """" & y & """"
  13.         If x = 0 Then
  14.             z = y
  15.         Else
  16.             z = z & " OR " & y
  17.         End If
  18.     Next x
  19.     stDocName = "rptAddresses"
  20.     DoCmd.OpenReport stDocName, acPreview, , "ccustno='" & z & "'"
  21.  
  22. Exit_Command11_Click:
  23.     Exit Sub
  24.  
  25. Err_Command11_Click:
  26.     MsgBox Err.Description
  27.     Resume Exit_Command11_Click
  28.  
  29. End Sub
Here is 1 Not-So-Obvious solution run against my Test Data. It has been debugged and is fully functional. Copy and Paste the code, make your necessary substitutions, and you should be ready to go. Let me know how you make out.
Expand|Select|Wrap|Line Numbers
  1. Dim x As Integer
  2. Dim y As String
  3. Dim z As String
  4. Dim stDocName As String
  5.  
  6. stDocName = "rptEmployees"
  7.  
  8. z = ""
  9. For x = 0 To List2.ListCount - 1
  10.   y = "[LastName]='" & List2.Column(0, x) & "'"
  11.     If x = 0 Then
  12.       z = y
  13.     Else
  14.       z = z & " Or " & y
  15.     End If
  16. Next x
  17.  
  18. DoCmd.OpenReport stDocName, acViewPreview, , z
Jul 13 '07 #2
Rabbit
12,516 Expert Mod 8TB
You can't do:
Expand|Select|Wrap|Line Numbers
  1. SomeField = "Value1" OR "Value2"
  2.  
You have two options:
Expand|Select|Wrap|Line Numbers
  1. SomeField = "Value1" OR SomeField = "Value2"
  2. SomeField In ("Value1", "Value2")
  3.  
Jul 13 '07 #3
ADezii
8,834 Expert 8TB
You can't do:
Expand|Select|Wrap|Line Numbers
  1. SomeField = "Value1" OR "Value2"
  2.  
You have two options:
Expand|Select|Wrap|Line Numbers
  1. SomeField = "Value1" OR SomeField = "Value2"
  2. SomeField In ("Value1", "Value2")
  3.  
Hello Rabbit:
I don't think the IN approach will work within the current context. It interprets the individual items within the IN construct as additional, and illegal, Arguments to the OpenReport() Method. This was my initial approach to the problem but didn't seem to be workable because of the stated reason. Just some useless information I figured you may possibly want to know. See ya later!
Jul 13 '07 #4
MikeTheBike
639 Expert 512MB
Hi

Expand|Select|Wrap|Line Numbers
  1.  
  2. z = ""
  3.     For x = 0 To List2.ListCount - 1
  4.         y = List2.Column(0, x)
  5.         y = """" & y & """"
  6.         If x = 0 Then
  7.             z = y
  8.         Else
  9.             z = z & " OR " & y
  10.         End If
  11.     Next x
  12.     stDocName = "rptAddresses"
  13.     DoCmd.OpenReport stDocName, acPreview, , "ccustno='" & z & "'"
Jul 13 '07 #5
MikeTheBike
639 Expert 512MB
Hi

I think this may be a little simpler.

Expand|Select|Wrap|Line Numbers
  1.         z = "ccustno = '"  & List2.Column(0, 0) & "'"
  2.     For x = 1 To List2.ListCount - 1
  3.         z = z & " OR ccustno = '"  & List2.Column(0, x) & "'"
  4.     Next x
  5.     stDocName = "rptAddresses"
  6.     DoCmd.OpenReport stDocName, acPreview, , z
  7.  
This assumes there is at least one item in the list, and if not you would need to test for the condition anyway.

MTB
Jul 13 '07 #6
Rabbit
12,516 Expert Mod 8TB
Hello Rabbit:
I don't think the IN approach will work within the current context. It interprets the individual items within the IN construct as additional, and illegal, Arguments to the OpenReport() Method. This was my initial approach to the problem but didn't seem to be workable because of the stated reason. Just some useless information I figured you may possibly want to know. See ya later!
I just tried this and this works:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command0_Click()
  2.     Dim stDocName As String
  3.     Dim stLinkCriteria As String
  4.  
  5.     stLinkCriteria = "UserID In (""Andy"", ""Bill"")"
  6.     stDocName = "Form1"
  7.  
  8.     DoCmd.OpenForm stDocName, , , stLinkCriteria
  9. End Sub
  10.  
Jul 13 '07 #7
ADezii
8,834 Expert 8TB
I just tried this and this works:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command0_Click()
  2.     Dim stDocName As String
  3.     Dim stLinkCriteria As String
  4.  
  5.     stLinkCriteria = "UserID In (""Andy"", ""Bill"")"
  6.     stDocName = "Form1"
  7.  
  8.     DoCmd.OpenForm stDocName, , , stLinkCriteria
  9. End Sub
  10.  
Thanks for the info, Rabbit. I really wasn't kidding when I said that I was giving you some useless information. (LOL).
Jul 13 '07 #8
Thank you everyone for all your responses, I was able to get it working with Mike's simplified version. I've bookmarked this site and plan to return if I run into any more snags, thanks!
Jul 13 '07 #9
Rabbit
12,516 Expert Mod 8TB
Thank you everyone for all your responses, I was able to get it working with Mike's simplified version. I've bookmarked this site and plan to return if I run into any more snags, thanks!
Technically mines is simpler but no hard feelings. lol.

Anyways, glad you got it working.
Jul 13 '07 #10

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

Similar topics

2
by: Joe | last post by:
Hey, I'm going to give some background on my situation in case anyone can point out a way around my problem altogether... for the problem itself, please skip to the bottom of the post. thanks....
1
by: Job Lot | last post by:
I have tow columns in my DataTable Date, Amount. What would be the best way to create multiple entries in my DataTable? I am given a start date, end date and an amount. I want to create multiple...
1
by: ccr | last post by:
Please view in a text editor so that the columnar text lines up. I used Terminal 9pt font to compose this post and copied/pasted to my newsreader program. I am writing in the hope that one of...
1
by: ccr | last post by:
Reposted with a longer line length. Apologies if I did not cancel the 1st attempt before you got it. If necessary, please view in a text editor so the columnar text lines up. I used Terminal...
5
by: Sammy | last post by:
On a Windows 2003 Server machine that has multiple IIS web sites, how does one control which web site the new web application gets created under? Thank you, Sammy
3
by: Bernard Lebel | last post by:
Hello, Is there an option or a way to allow the selection of multiple entries in the Listbox widget? I could not find any, and would like to allow the end user to select multiple entries. ...
1
by: kevooo5 | last post by:
I use Access 2003 and I'm having trouble setting the wherecondition for the Openform functions. Usually it would look something like this DoCmd.OpenForm "Contact", , ,wherecondition here I...
0
by: TechnoAtif | last post by:
<?php include "dbconnect.php"; include "commonFunc.php"; ?> <!----------------------------------> <table width="80%" border="1" cellpadding="2" cellspacing="0"> <tr > <td...
7
by: rik | last post by:
I am trying to build an Access 2002/2003 database application that I would like to offer to other writers so they can track submissions. Just trying to make a helpful tool for writers, poets, or...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.