473,395 Members | 1,679 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.

Handy routine for building an IN clause from multi-select listbox

DFS
You might also find other uses

================================================== =======
Public Function getSelections(selType As String, selSeparator As
Boolean, selList As ListBox, selCol As Byte) As String

'BUILD IN CLAUSE FROM ITEMS CHOSEN IN MULTI-SELECT LIST BOX

'ARGUMENTS
'selType = text or number
'selSeparator: true returns apostrophes between items
'selList = name of listbox control
'selCol = which column is the data found in

getSelections = ""
dim ctl as Control, i as integer

Set ctl = selList
For i = 0 To ctl.ListCount - 1
If ctl.Selected(i) = True Then
If selType = "text" Then
If selSeparator = True Then
getSelections = getSelections & "'" & ctl.Column(selCol, i) & "',"
Else
getSelections = getSelections & "" & ctl.Column(selCol, i) & ","
End If
ElseIf selType = "number" Then
getSelections = getSelections & ctl.Column(selCol, i) & ","
End If
End If
Next i

getSelections = Left(getSelections, Len(getSelections) - 1)

End Function
================================================== =======
Jan 26 '06 #1
9 1795
On Thu, 26 Jan 2006 02:20:03 -0500, DFS <nospam@dfs_.com> wrote:
You might also find other uses

================================================= ========
Public Function getSelections(selType As String, selSeparator As
Boolean, selList As ListBox, selCol As Byte) As String

'BUILD IN CLAUSE FROM ITEMS CHOSEN IN MULTI-SELECT LIST BOX

'ARGUMENTS
'selType = text or number
'selSeparator: true returns apostrophes between items
'selList = name of listbox control
'selCol = which column is the data found in

getSelections = ""
dim ctl as Control, i as integer

Set ctl = selList
For i = 0 To ctl.ListCount - 1
If ctl.Selected(i) = True Then
If selType = "text" Then
If selSeparator = True Then
getSelections = getSelections & "'" & ctl.Column(selCol, i) & "',"
Else
getSelections = getSelections & "" & ctl.Column(selCol, i) & ","
End If
ElseIf selType = "number" Then
getSelections = getSelections & ctl.Column(selCol, i) & ","
End If
End If
Next i

getSelections = Left(getSelections, Len(getSelections) - 1)

End Function
================================================= ========


Nice work.
I would make one suggestion however. I would make the separator a quote " rather than an apostrophe.
The apostrophe may cause errors in your IN cluse if the list box contains an entry like O'Reilly.

Wayne Gillespie
Gosford NSW Australia
Jan 26 '06 #2
DFS
Wayne Gillespie wrote:
On Thu, 26 Jan 2006 02:20:03 -0500, DFS <nospam@dfs_.com> wrote:

You might also find other uses

================================================ =========
Public Function getSelections(selType As String, selSeparator As
Boolean, selList As ListBox, selCol As Byte) As String

'BUILD IN CLAUSE FROM ITEMS CHOSEN IN MULTI-SELECT LIST BOX

'ARGUMENTS
'selType = text or number
'selSeparator: true returns apostrophes between items
'selList = name of listbox control
'selCol = which column is the data found in

getSelections = ""
dim ctl as Control, i as integer

Set ctl = selList
For i = 0 To ctl.ListCount - 1
If ctl.Selected(i) = True Then
If selType = "text" Then
If selSeparator = True Then
getSelections = getSelections & "'" & ctl.Column(selCol, i) & "',"
Else
getSelections = getSelections & "" & ctl.Column(selCol, i) & ","
End If
ElseIf selType = "number" Then
getSelections = getSelections & ctl.Column(selCol, i) & ","
End If
End If
Next i

getSelections = Left(getSelections, Len(getSelections) - 1)

End Function
================================================ =========

Nice work.
I would make one suggestion however. I would make the separator a quote " rather than an apostrophe.
The apostrophe may cause errors in your IN cluse if the list box contains an entry like O'Reilly..

Good suggestion, and I will implement it. I actually haven't ever had
that problem in an IN clause, because I almost always use ID numbers.
Having said that I'm sure tomorrow morning it'll pop up.

And when I said 'selList = name of listbox control' that's incorrect.
You actually pass in the listBox object, of course, not the name.

Usage example:

whereStr = whereStr & "AND Location IN (" & getSelections("text", True,
Me.listLocation, 0) & ") "
Jan 26 '06 #3
What about if it's measurements e.g. 3'4".

Whatever you use as the string delimiter just double up any occurences in
the string, so your original.
getSelections = getSelections & "'" & ctl.Column(selCol, i) & "',"
becomes
getSelections = getSelections & "'" & Replace(ctl.Column(selCol, i),
"'", "''", compare:=vbTextCompare) & "',"

That is replace all single instances of an apostrophe with two instances of
an apostrphe.
--

Terry Kreft
"DFS" <nospam@dfs_.com> wrote in message
news:1Z******************@bignews4.bellsouth.net.. .
Wayne Gillespie wrote:
On Thu, 26 Jan 2006 02:20:03 -0500, DFS <nospam@dfs_.com> wrote:

You might also find other uses

================================================ =========
Public Function getSelections(selType As String, selSeparator As
Boolean, selList As ListBox, selCol As Byte) As String

'BUILD IN CLAUSE FROM ITEMS CHOSEN IN MULTI-SELECT LIST BOX

'ARGUMENTS
'selType = text or number
'selSeparator: true returns apostrophes between items
'selList = name of listbox control
'selCol = which column is the data found in

getSelections = ""
dim ctl as Control, i as integer

Set ctl = selList
For i = 0 To ctl.ListCount - 1
If ctl.Selected(i) = True Then
If selType = "text" Then
If selSeparator = True Then
getSelections = getSelections & "'" & ctl.Column(selCol, i) & "',"
Else
getSelections = getSelections & "" & ctl.Column(selCol, i) & ","
End If
ElseIf selType = "number" Then
getSelections = getSelections & ctl.Column(selCol, i) & ","
End If
End If
Next i

getSelections = Left(getSelections, Len(getSelections) - 1)

End Function
================================================ =========

Nice work.
I would make one suggestion however. I would make the separator a quote " rather than an apostrophe. The apostrophe may cause errors in your IN cluse if the list box

contains an entry like O'Reilly..

Good suggestion, and I will implement it. I actually haven't ever had
that problem in an IN clause, because I almost always use ID numbers.
Having said that I'm sure tomorrow morning it'll pop up.

And when I said 'selList = name of listbox control' that's incorrect.
You actually pass in the listBox object, of course, not the name.

Usage example:

whereStr = whereStr & "AND Location IN (" & getSelections("text", True,
Me.listLocation, 0) & ") "

Jan 26 '06 #4
DFS <nospam@dfs_.com> wrote in
news:1Z******************@bignews4.bellsouth.net:
Wayne Gillespie wrote:

I would make one suggestion however. I would make the separator a
quote " rather than an apostrophe. The apostrophe may cause
errors in your IN cluse if the list box contains an entry like
O'Reilly..


Good suggestion, and I will implement it. I actually haven't ever
had that problem in an IN clause, because I almost always use ID
numbers. Having said that I'm sure tomorrow morning it'll pop up.


Don't you need to check the data type of the column you're putting
the criteria on? That is, if you're using IN on a numeric column,
you want no quotes, if on a text column, you want them.

You could, theoretically, pass one of the Access constants for data
type to your routine and use it to choose to quote the strings or
not.

--
David W. Fenton http://www.dfenton.com/
usenet at dfenton dot com http://www.dfenton.com/DFA/
Jan 26 '06 #5
On Thu, 26 Jan 2006 11:21:55 -0600, "David W. Fenton" <XX*******@dfenton.com.invalid> wrote:
DFS <nospam@dfs_.com> wrote in
news:1Z******************@bignews4.bellsouth.ne t:
Wayne Gillespie wrote:

I would make one suggestion however. I would make the separator a
quote " rather than an apostrophe. The apostrophe may cause
errors in your IN cluse if the list box contains an entry like
O'Reilly..


Good suggestion, and I will implement it. I actually haven't ever
had that problem in an IN clause, because I almost always use ID
numbers. Having said that I'm sure tomorrow morning it'll pop up.


Don't you need to check the data type of the column you're putting
the criteria on? That is, if you're using IN on a numeric column,
you want no quotes, if on a text column, you want them.

You could, theoretically, pass one of the Access constants for data
type to your routine and use it to choose to quote the strings or
not.


The OP's function has a SelType argument to indicate whether the data is text ot numeric.
Wayne Gillespie
Gosford NSW Australia
Jan 26 '06 #6
Wayne Gillespie <be*****@NOhotmailSPAM.com.au> wrote in
news:dl********************************@4ax.com:
On Thu, 26 Jan 2006 11:21:55 -0600, "David W. Fenton"
<XX*******@dfenton.com.invalid> wrote:
DFS <nospam@dfs_.com> wrote in
news:1Z******************@bignews4.bellsouth.net :
Wayne Gillespie wrote:

I would make one suggestion however. I would make the separator
a quote " rather than an apostrophe. The apostrophe may cause
errors in your IN cluse if the list box contains an entry like
O'Reilly..

Good suggestion, and I will implement it. I actually haven't
ever had that problem in an IN clause, because I almost always
use ID numbers. Having said that I'm sure tomorrow morning it'll
pop up.


Don't you need to check the data type of the column you're putting
the criteria on? That is, if you're using IN on a numeric column,
you want no quotes, if on a text column, you want them.

You could, theoretically, pass one of the Access constants for
data type to your routine and use it to choose to quote the
strings or not.


The OP's function has a SelType argument to indicate whether the
data is text ot numeric.


What data? The data from the listbox? That won't necessarily map
correctly to the data type of the underlying field that you're
writing your WHERE clause against. Numbers might very well be stored
in a text field, for instance.

--
David W. Fenton http://www.dfenton.com/
usenet at dfenton dot com http://www.dfenton.com/DFA/
Jan 26 '06 #7
DFS
David W. Fenton wrote:
What data? The data from the listbox? That won't necessarily map
correctly to the data type of the underlying field that you're
writing your WHERE clause against. Numbers might very well be stored
in a text field, for instance.


???

While you're busy sorting that out, just pass in the argument "number"
when querying a number field and "text" when querying a text field.
Jan 27 '06 #8
DFS
Terry Kreft wrote:
What about if it's measurements e.g. 3'4".
There's no end to the what-ifs.

The only data handling feature I added beyond the basic number/text
choice is an option to return text data without separators, which is
more suitable for showing on screen or a report.

Whatever you use as the string delimiter just double up any occurences in
the string, so your original.
getSelections = getSelections & "'" & ctl.Column(selCol, i) & "',"
becomes
getSelections = getSelections & "'" & Replace(ctl.Column(selCol, i),
"'", "''", compare:=vbTextCompare) & "',"

That is replace all single instances of an apostrophe with two instances of
an apostrphe.

Jan 27 '06 #9
You seem to have missed the point of my post.

Changing the text delimiter from apostrophe to double quote doesn't remove
the problem caused by the delimiter being embedded in the string you are
delimiting.
--

Terry Kreft
"DFS" <nospam@dfs_.com> wrote in message
news:ME****************@bignews1.bellsouth.net...
Terry Kreft wrote:
What about if it's measurements e.g. 3'4".


There's no end to the what-ifs.

The only data handling feature I added beyond the basic number/text
choice is an option to return text data without separators, which is
more suitable for showing on screen or a report.

Whatever you use as the string delimiter just double up any occurences in the string, so your original.
getSelections = getSelections & "'" & ctl.Column(selCol, i) & "',"
becomes
getSelections = getSelections & "'" & Replace(ctl.Column(selCol, i),
"'", "''", compare:=vbTextCompare) & "',"

That is replace all single instances of an apostrophe with two instances of an apostrphe.

Jan 30 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Jenta | last post by:
A World Beyond Capitalism 2005, An Annual International Multiracial Alliance Building Peace Conference Is Accepting Proposals... ...and Online Registration is now available if you plan to table...
8
by: Roberto Dias | last post by:
I developed a DFT routine using a language named "ATP Model Language". It is based on FORTRAN and specific to electromagnetic transients simulations, a study field in electrical engineering. Few...
14
by: joshsackett | last post by:
I have a WHERE clause that could be an "=" or a "LIKE" depending upon if the passed variable is populated or not. I would like to know the best way to write the WHERE clause to make it dynamically...
4
by: Barry Edmund Wright | last post by:
I would really appreciate your assistance. I am using Access 2000 to create a form that Lists Names and Addresses based on a number of selection criteria one of which is a combo box cboPCZip. All...
4
by: Brian Shannon | last post by:
I have 3 combo boxes and two date text boxes on a .aspx page. The user can fill in any of the 5 controls or none to filter a datagrid. I was hoping someone could explain how to efficiently build...
3
by: Bob Stearns | last post by:
When you're constructing sql in php or anywhere else and don't know whether an INSERT will work, use MERGE with a VALUES clause as shown below rather than either intercepting the diagnostic or...
9
by: Siv | last post by:
Hi, I am trying to find a wrapping routine for text and not having much luck. The reason I need one is that I am producing a report where the text that was stored in the database was written into...
3
by: garrettm | last post by:
What if you want to search using FTS with AND logic using the FORMSOF(inflectional,...) inside the CONTAINS() clause??? if my search phrase is "light hearted" I can easily do an OR search using...
6
by: ARC | last post by:
I'm almost ready to build an upgrade routine for a commercial app that will need to import data from an existing Access 97 back-end database to an Access 2007 back-end blank database. Ideally, it...
4
by: nanabuch | last post by:
Hello, I am new to this forum, and I am a newbit in Oracle, I do have a background in MS Access, VBA, SQL server development and some Oracle backend development work. I have been giving a task...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.