Multiple-select from list

B

BethHill

Hello,

I want to be able to make multiple selections on my form from the same field
in the same table but allow specific records to be selected, as well as a
range of records and certain records (comma delimited?). After the fields
are selected, I want to pass this value into a SQL statement to perform the
query. How do I do this?
 
K

Klatuu

You will need a List Box control with the Multi Select property set to
Extended. This will allow you to use the familiar, Click to get one, <Ctl>
Click to add another, or <Shift> Click to select a range.

Now, you need a Row Source for the field. It should look something like:

SELECT DISTINCT SomeFieldName FROM SomeTableName

This will return one occurance of every value in the field.

Once the user has selected a field, you can use the ItemsSelected collection
of the List Box control to put together a where condition for your SQL
statement:

Dim varItem As Variant
Dim strWhere as String

strWhere = "IN("
For Each varItem In Me.MyListBoxName.ItemsSelected
strWhere = strWhere & Chr(34) & Me.MyListBoxName.ItemData(varItem) & _
Chr(34) & ","
Next varItem
'Take off the last comma
strWhere = Left(strWhere, Len(strWhere) -1)
'Add the closing paren
strWhere = strWhere & ")"

Now you have a string you can use in your SQL that will filter based on the
selections maked in the list box.
 
B

BethHill

Thank you! I am now able to make multiple selections in my list box. Now
the problem lies with the text that is in the list box. There is just field
for the control source on the list box but multiple of records. The field is
just an AutoNumber in the list box. I don't want to have to scroll down all
the way through the list to choose the records I want to send to SQL
statement. Is there a way to make it wrap the text by using columns in the
list box?
 
K

Klatuu

You can't use a multi select list box control as a bound control. It will
always return a Null value.
I'm not sure I understand the issue.
 
B

BethHill

Let me give you my scenario:

In the form, there is a multi-select list box with the control source as
"SELECT [tbl Event].[Event ID] FROM [tbl Event]; " This correctly displays
all the records for the Event ID field.

In this list box, there are many records that I have to scroll through in
order to select the ones that I want. I was wondering if there is a way to
display more records in the list box rather than scroll through each of them.
 
K

Klatuu

If you are talking about putting multiple [Event ID]s in the list box item (1
id per column), it is possible, but not easy, and would be difficult to
select. How would the user know which column to select? How would you, as
the developer, know which column to use? Is the [Event ID] text that is
meaningful to a user? Is it unique? How many are we talking about?

BethHill said:
Let me give you my scenario:

In the form, there is a multi-select list box with the control source as
"SELECT [tbl Event].[Event ID] FROM [tbl Event]; " This correctly displays
all the records for the Event ID field.

In this list box, there are many records that I have to scroll through in
order to select the ones that I want. I was wondering if there is a way to
display more records in the list box rather than scroll through each of them.



Klatuu said:
You can't use a multi select list box control as a bound control. It will
always return a Null value.
I'm not sure I understand the issue.
 
B

BethHill

The [Event ID] text is very meaningful to the user and is unique. (It is the
primary key in the "tbl Event" table.) There are currently about 100+
records or [Event ID]s that the user could choose from but could be more very
soon. If columns is not a good alternative, do you know of another option?

Klatuu said:
If you are talking about putting multiple [Event ID]s in the list box item (1
id per column), it is possible, but not easy, and would be difficult to
select. How would the user know which column to select? How would you, as
the developer, know which column to use? Is the [Event ID] text that is
meaningful to a user? Is it unique? How many are we talking about?

BethHill said:
Let me give you my scenario:

In the form, there is a multi-select list box with the control source as
"SELECT [tbl Event].[Event ID] FROM [tbl Event]; " This correctly displays
all the records for the Event ID field.

In this list box, there are many records that I have to scroll through in
order to select the ones that I want. I was wondering if there is a way to
display more records in the list box rather than scroll through each of them.



Klatuu said:
You can't use a multi select list box control as a bound control. It will
always return a Null value.
I'm not sure I understand the issue.

:

Thank you! I am now able to make multiple selections in my list box. Now
the problem lies with the text that is in the list box. There is just field
for the control source on the list box but multiple of records. The field is
just an AutoNumber in the list box. I don't want to have to scroll down all
the way through the list to choose the records I want to send to SQL
statement. Is there a way to make it wrap the text by using columns in the
list box?

:

You will need a List Box control with the Multi Select property set to
Extended. This will allow you to use the familiar, Click to get one, <Ctl>
Click to add another, or <Shift> Click to select a range.

Now, you need a Row Source for the field. It should look something like:

SELECT DISTINCT SomeFieldName FROM SomeTableName

This will return one occurance of every value in the field.

Once the user has selected a field, you can use the ItemsSelected collection
of the List Box control to put together a where condition for your SQL
statement:

Dim varItem As Variant
Dim strWhere as String

strWhere = "IN("
For Each varItem In Me.MyListBoxName.ItemsSelected
strWhere = strWhere & Chr(34) & Me.MyListBoxName.ItemData(varItem) & _
Chr(34) & ","
Next varItem
'Take off the last comma
strWhere = Left(strWhere, Len(strWhere) -1)
'Add the closing paren
strWhere = strWhere & ")"

Now you have a string you can use in your SQL that will filter based on the
selections maked in the list box.

:

Hello,

I want to be able to make multiple selections on my form from the same field
in the same table but allow specific records to be selected, as well as a
range of records and certain records (comma delimited?). After the fields
are selected, I want to pass this value into a SQL statement to perform the
query. How do I do this?
 
K

Klatuu

There is not a better solution that I can think of. I tried using a combo
with auto expand to position the list box, but anything previously selected
became unselected.

BethHill said:
The [Event ID] text is very meaningful to the user and is unique. (It is the
primary key in the "tbl Event" table.) There are currently about 100+
records or [Event ID]s that the user could choose from but could be more very
soon. If columns is not a good alternative, do you know of another option?

Klatuu said:
If you are talking about putting multiple [Event ID]s in the list box item (1
id per column), it is possible, but not easy, and would be difficult to
select. How would the user know which column to select? How would you, as
the developer, know which column to use? Is the [Event ID] text that is
meaningful to a user? Is it unique? How many are we talking about?

BethHill said:
Let me give you my scenario:

In the form, there is a multi-select list box with the control source as
"SELECT [tbl Event].[Event ID] FROM [tbl Event]; " This correctly displays
all the records for the Event ID field.

In this list box, there are many records that I have to scroll through in
order to select the ones that I want. I was wondering if there is a way to
display more records in the list box rather than scroll through each of them.



:

You can't use a multi select list box control as a bound control. It will
always return a Null value.
I'm not sure I understand the issue.

:

Thank you! I am now able to make multiple selections in my list box. Now
the problem lies with the text that is in the list box. There is just field
for the control source on the list box but multiple of records. The field is
just an AutoNumber in the list box. I don't want to have to scroll down all
the way through the list to choose the records I want to send to SQL
statement. Is there a way to make it wrap the text by using columns in the
list box?

:

You will need a List Box control with the Multi Select property set to
Extended. This will allow you to use the familiar, Click to get one, <Ctl>
Click to add another, or <Shift> Click to select a range.

Now, you need a Row Source for the field. It should look something like:

SELECT DISTINCT SomeFieldName FROM SomeTableName

This will return one occurance of every value in the field.

Once the user has selected a field, you can use the ItemsSelected collection
of the List Box control to put together a where condition for your SQL
statement:

Dim varItem As Variant
Dim strWhere as String

strWhere = "IN("
For Each varItem In Me.MyListBoxName.ItemsSelected
strWhere = strWhere & Chr(34) & Me.MyListBoxName.ItemData(varItem) & _
Chr(34) & ","
Next varItem
'Take off the last comma
strWhere = Left(strWhere, Len(strWhere) -1)
'Add the closing paren
strWhere = strWhere & ")"

Now you have a string you can use in your SQL that will filter based on the
selections maked in the list box.

:

Hello,

I want to be able to make multiple selections on my form from the same field
in the same table but allow specific records to be selected, as well as a
range of records and certain records (comma delimited?). After the fields
are selected, I want to pass this value into a SQL statement to perform the
query. How do I do this?
 
B

BethHill

Could I use something else, like a text box and have the user to sepearte the
choices with commas or do a range with a hyphen? Similar to print options
when selecting the pages to be printed. If so, do you have the code to do
this?

Klatuu said:
There is not a better solution that I can think of. I tried using a combo
with auto expand to position the list box, but anything previously selected
became unselected.

BethHill said:
The [Event ID] text is very meaningful to the user and is unique. (It is the
primary key in the "tbl Event" table.) There are currently about 100+
records or [Event ID]s that the user could choose from but could be more very
soon. If columns is not a good alternative, do you know of another option?

Klatuu said:
If you are talking about putting multiple [Event ID]s in the list box item (1
id per column), it is possible, but not easy, and would be difficult to
select. How would the user know which column to select? How would you, as
the developer, know which column to use? Is the [Event ID] text that is
meaningful to a user? Is it unique? How many are we talking about?

:

Let me give you my scenario:

In the form, there is a multi-select list box with the control source as
"SELECT [tbl Event].[Event ID] FROM [tbl Event]; " This correctly displays
all the records for the Event ID field.

In this list box, there are many records that I have to scroll through in
order to select the ones that I want. I was wondering if there is a way to
display more records in the list box rather than scroll through each of them.



:

You can't use a multi select list box control as a bound control. It will
always return a Null value.
I'm not sure I understand the issue.

:

Thank you! I am now able to make multiple selections in my list box. Now
the problem lies with the text that is in the list box. There is just field
for the control source on the list box but multiple of records. The field is
just an AutoNumber in the list box. I don't want to have to scroll down all
the way through the list to choose the records I want to send to SQL
statement. Is there a way to make it wrap the text by using columns in the
list box?

:

You will need a List Box control with the Multi Select property set to
Extended. This will allow you to use the familiar, Click to get one, <Ctl>
Click to add another, or <Shift> Click to select a range.

Now, you need a Row Source for the field. It should look something like:

SELECT DISTINCT SomeFieldName FROM SomeTableName

This will return one occurance of every value in the field.

Once the user has selected a field, you can use the ItemsSelected collection
of the List Box control to put together a where condition for your SQL
statement:

Dim varItem As Variant
Dim strWhere as String

strWhere = "IN("
For Each varItem In Me.MyListBoxName.ItemsSelected
strWhere = strWhere & Chr(34) & Me.MyListBoxName.ItemData(varItem) & _
Chr(34) & ","
Next varItem
'Take off the last comma
strWhere = Left(strWhere, Len(strWhere) -1)
'Add the closing paren
strWhere = strWhere & ")"

Now you have a string you can use in your SQL that will filter based on the
selections maked in the list box.

:

Hello,

I want to be able to make multiple selections on my form from the same field
in the same table but allow specific records to be selected, as well as a
range of records and certain records (comma delimited?). After the fields
are selected, I want to pass this value into a SQL statement to perform the
query. How do I do this?
 
K

Klatuu

I guess you could, but I really think that would take more time and be more
prone to errors. Also, remember, you want to be able to select a range or
use <ctl>Click to select multiple noncontiguous items. You would not be able
to do that easily.

I really believe the multi select list box is the easiest to develop and
would be the most user friendly.

BethHill said:
Could I use something else, like a text box and have the user to sepearte the
choices with commas or do a range with a hyphen? Similar to print options
when selecting the pages to be printed. If so, do you have the code to do
this?

Klatuu said:
There is not a better solution that I can think of. I tried using a combo
with auto expand to position the list box, but anything previously selected
became unselected.

BethHill said:
The [Event ID] text is very meaningful to the user and is unique. (It is the
primary key in the "tbl Event" table.) There are currently about 100+
records or [Event ID]s that the user could choose from but could be more very
soon. If columns is not a good alternative, do you know of another option?

:

If you are talking about putting multiple [Event ID]s in the list box item (1
id per column), it is possible, but not easy, and would be difficult to
select. How would the user know which column to select? How would you, as
the developer, know which column to use? Is the [Event ID] text that is
meaningful to a user? Is it unique? How many are we talking about?

:

Let me give you my scenario:

In the form, there is a multi-select list box with the control source as
"SELECT [tbl Event].[Event ID] FROM [tbl Event]; " This correctly displays
all the records for the Event ID field.

In this list box, there are many records that I have to scroll through in
order to select the ones that I want. I was wondering if there is a way to
display more records in the list box rather than scroll through each of them.



:

You can't use a multi select list box control as a bound control. It will
always return a Null value.
I'm not sure I understand the issue.

:

Thank you! I am now able to make multiple selections in my list box. Now
the problem lies with the text that is in the list box. There is just field
for the control source on the list box but multiple of records. The field is
just an AutoNumber in the list box. I don't want to have to scroll down all
the way through the list to choose the records I want to send to SQL
statement. Is there a way to make it wrap the text by using columns in the
list box?

:

You will need a List Box control with the Multi Select property set to
Extended. This will allow you to use the familiar, Click to get one, <Ctl>
Click to add another, or <Shift> Click to select a range.

Now, you need a Row Source for the field. It should look something like:

SELECT DISTINCT SomeFieldName FROM SomeTableName

This will return one occurance of every value in the field.

Once the user has selected a field, you can use the ItemsSelected collection
of the List Box control to put together a where condition for your SQL
statement:

Dim varItem As Variant
Dim strWhere as String

strWhere = "IN("
For Each varItem In Me.MyListBoxName.ItemsSelected
strWhere = strWhere & Chr(34) & Me.MyListBoxName.ItemData(varItem) & _
Chr(34) & ","
Next varItem
'Take off the last comma
strWhere = Left(strWhere, Len(strWhere) -1)
'Add the closing paren
strWhere = strWhere & ")"

Now you have a string you can use in your SQL that will filter based on the
selections maked in the list box.

:

Hello,

I want to be able to make multiple selections on my form from the same field
in the same table but allow specific records to be selected, as well as a
range of records and certain records (comma delimited?). After the fields
are selected, I want to pass this value into a SQL statement to perform the
query. How do I do this?
 
B

BethHill

I have a new problem. I am using the multi-select list box and it is
correctly allowing the user to make multiple selections. I am using the code
you gave me to store the selected records in a string. (I put msgbox in my
code to see the string and it is correctly storing the selected records.)
The problem I am now having is using the IN statement of SQL.

For example, I want my "stWhere" to look like the following if the user
selects Event ID "1" and Event ID "2". How would I achieve this using your
For Each....statement?


WHERE ((([tbl Event].[Event ID])=1)) OR ((([tbl Event].[Event ID])=3));


Klatuu said:
I guess you could, but I really think that would take more time and be more
prone to errors. Also, remember, you want to be able to select a range or
use <ctl>Click to select multiple noncontiguous items. You would not be able
to do that easily.

I really believe the multi select list box is the easiest to develop and
would be the most user friendly.

BethHill said:
Could I use something else, like a text box and have the user to sepearte the
choices with commas or do a range with a hyphen? Similar to print options
when selecting the pages to be printed. If so, do you have the code to do
this?

Klatuu said:
There is not a better solution that I can think of. I tried using a combo
with auto expand to position the list box, but anything previously selected
became unselected.

:

The [Event ID] text is very meaningful to the user and is unique. (It is the
primary key in the "tbl Event" table.) There are currently about 100+
records or [Event ID]s that the user could choose from but could be more very
soon. If columns is not a good alternative, do you know of another option?

:

If you are talking about putting multiple [Event ID]s in the list box item (1
id per column), it is possible, but not easy, and would be difficult to
select. How would the user know which column to select? How would you, as
the developer, know which column to use? Is the [Event ID] text that is
meaningful to a user? Is it unique? How many are we talking about?

:

Let me give you my scenario:

In the form, there is a multi-select list box with the control source as
"SELECT [tbl Event].[Event ID] FROM [tbl Event]; " This correctly displays
all the records for the Event ID field.

In this list box, there are many records that I have to scroll through in
order to select the ones that I want. I was wondering if there is a way to
display more records in the list box rather than scroll through each of them.



:

You can't use a multi select list box control as a bound control. It will
always return a Null value.
I'm not sure I understand the issue.

:

Thank you! I am now able to make multiple selections in my list box. Now
the problem lies with the text that is in the list box. There is just field
for the control source on the list box but multiple of records. The field is
just an AutoNumber in the list box. I don't want to have to scroll down all
the way through the list to choose the records I want to send to SQL
statement. Is there a way to make it wrap the text by using columns in the
list box?

:

You will need a List Box control with the Multi Select property set to
Extended. This will allow you to use the familiar, Click to get one, <Ctl>
Click to add another, or <Shift> Click to select a range.

Now, you need a Row Source for the field. It should look something like:

SELECT DISTINCT SomeFieldName FROM SomeTableName

This will return one occurance of every value in the field.

Once the user has selected a field, you can use the ItemsSelected collection
of the List Box control to put together a where condition for your SQL
statement:

Dim varItem As Variant
Dim strWhere as String

strWhere = "IN("
For Each varItem In Me.MyListBoxName.ItemsSelected
strWhere = strWhere & Chr(34) & Me.MyListBoxName.ItemData(varItem) & _
Chr(34) & ","
Next varItem
'Take off the last comma
strWhere = Left(strWhere, Len(strWhere) -1)
'Add the closing paren
strWhere = strWhere & ")"

Now you have a string you can use in your SQL that will filter based on the
selections maked in the list box.

:

Hello,

I want to be able to make multiple selections on my form from the same field
in the same table but allow specific records to be selected, as well as a
range of records and certain records (comma delimited?). After the fields
are selected, I want to pass this value into a SQL statement to perform the
query. How do I do this?
 
K

Klatuu

Why?

The IN() is a whole lot easier than a string or ORs.

To change it, you will have to recode where strWhere is created.
Even if you choose to do that, all those useless ((((((((((((((((( that
Access puts in can be omitted.

WHERE [tbl Event].[Event ID])=1 OR [tbl Event].[Event ID]=3;




BethHill said:
I have a new problem. I am using the multi-select list box and it is
correctly allowing the user to make multiple selections. I am using the code
you gave me to store the selected records in a string. (I put msgbox in my
code to see the string and it is correctly storing the selected records.)
The problem I am now having is using the IN statement of SQL.

For example, I want my "stWhere" to look like the following if the user
selects Event ID "1" and Event ID "2". How would I achieve this using your
For Each....statement?


WHERE ((([tbl Event].[Event ID])=1)) OR ((([tbl Event].[Event ID])=3));


Klatuu said:
I guess you could, but I really think that would take more time and be more
prone to errors. Also, remember, you want to be able to select a range or
use <ctl>Click to select multiple noncontiguous items. You would not be able
to do that easily.

I really believe the multi select list box is the easiest to develop and
would be the most user friendly.

BethHill said:
Could I use something else, like a text box and have the user to sepearte the
choices with commas or do a range with a hyphen? Similar to print options
when selecting the pages to be printed. If so, do you have the code to do
this?

:

There is not a better solution that I can think of. I tried using a combo
with auto expand to position the list box, but anything previously selected
became unselected.

:

The [Event ID] text is very meaningful to the user and is unique. (It is the
primary key in the "tbl Event" table.) There are currently about 100+
records or [Event ID]s that the user could choose from but could be more very
soon. If columns is not a good alternative, do you know of another option?

:

If you are talking about putting multiple [Event ID]s in the list box item (1
id per column), it is possible, but not easy, and would be difficult to
select. How would the user know which column to select? How would you, as
the developer, know which column to use? Is the [Event ID] text that is
meaningful to a user? Is it unique? How many are we talking about?

:

Let me give you my scenario:

In the form, there is a multi-select list box with the control source as
"SELECT [tbl Event].[Event ID] FROM [tbl Event]; " This correctly displays
all the records for the Event ID field.

In this list box, there are many records that I have to scroll through in
order to select the ones that I want. I was wondering if there is a way to
display more records in the list box rather than scroll through each of them.



:

You can't use a multi select list box control as a bound control. It will
always return a Null value.
I'm not sure I understand the issue.

:

Thank you! I am now able to make multiple selections in my list box. Now
the problem lies with the text that is in the list box. There is just field
for the control source on the list box but multiple of records. The field is
just an AutoNumber in the list box. I don't want to have to scroll down all
the way through the list to choose the records I want to send to SQL
statement. Is there a way to make it wrap the text by using columns in the
list box?

:

You will need a List Box control with the Multi Select property set to
Extended. This will allow you to use the familiar, Click to get one, <Ctl>
Click to add another, or <Shift> Click to select a range.

Now, you need a Row Source for the field. It should look something like:

SELECT DISTINCT SomeFieldName FROM SomeTableName

This will return one occurance of every value in the field.

Once the user has selected a field, you can use the ItemsSelected collection
of the List Box control to put together a where condition for your SQL
statement:

Dim varItem As Variant
Dim strWhere as String

strWhere = "IN("
For Each varItem In Me.MyListBoxName.ItemsSelected
strWhere = strWhere & Chr(34) & Me.MyListBoxName.ItemData(varItem) & _
Chr(34) & ","
Next varItem
'Take off the last comma
strWhere = Left(strWhere, Len(strWhere) -1)
'Add the closing paren
strWhere = strWhere & ")"

Now you have a string you can use in your SQL that will filter based on the
selections maked in the list box.

:

Hello,

I want to be able to make multiple selections on my form from the same field
in the same table but allow specific records to be selected, as well as a
range of records and certain records (comma delimited?). After the fields
are selected, I want to pass this value into a SQL statement to perform the
query. How do I do this?
 
B

BethHill

I am using the IN() instead of all of those OR statements in SQL. I am
getting an error message that says "Data type mismatch in criteria
expression." What does this mean? Here is the "stWhere" string when the
user selects "1" and "3" in the list box.
"WHERE [tbl Event].[Event ID] IN ("1", "3");"

Thank you for your help! I have never used the IN part of SQL before.

Klatuu said:
Why?

The IN() is a whole lot easier than a string or ORs.

To change it, you will have to recode where strWhere is created.
Even if you choose to do that, all those useless ((((((((((((((((( that
Access puts in can be omitted.

WHERE [tbl Event].[Event ID])=1 OR [tbl Event].[Event ID]=3;




BethHill said:
I have a new problem. I am using the multi-select list box and it is
correctly allowing the user to make multiple selections. I am using the code
you gave me to store the selected records in a string. (I put msgbox in my
code to see the string and it is correctly storing the selected records.)
The problem I am now having is using the IN statement of SQL.

For example, I want my "stWhere" to look like the following if the user
selects Event ID "1" and Event ID "2". How would I achieve this using your
For Each....statement?


WHERE ((([tbl Event].[Event ID])=1)) OR ((([tbl Event].[Event ID])=3));


Klatuu said:
I guess you could, but I really think that would take more time and be more
prone to errors. Also, remember, you want to be able to select a range or
use <ctl>Click to select multiple noncontiguous items. You would not be able
to do that easily.

I really believe the multi select list box is the easiest to develop and
would be the most user friendly.

:

Could I use something else, like a text box and have the user to sepearte the
choices with commas or do a range with a hyphen? Similar to print options
when selecting the pages to be printed. If so, do you have the code to do
this?

:

There is not a better solution that I can think of. I tried using a combo
with auto expand to position the list box, but anything previously selected
became unselected.

:

The [Event ID] text is very meaningful to the user and is unique. (It is the
primary key in the "tbl Event" table.) There are currently about 100+
records or [Event ID]s that the user could choose from but could be more very
soon. If columns is not a good alternative, do you know of another option?

:

If you are talking about putting multiple [Event ID]s in the list box item (1
id per column), it is possible, but not easy, and would be difficult to
select. How would the user know which column to select? How would you, as
the developer, know which column to use? Is the [Event ID] text that is
meaningful to a user? Is it unique? How many are we talking about?

:

Let me give you my scenario:

In the form, there is a multi-select list box with the control source as
"SELECT [tbl Event].[Event ID] FROM [tbl Event]; " This correctly displays
all the records for the Event ID field.

In this list box, there are many records that I have to scroll through in
order to select the ones that I want. I was wondering if there is a way to
display more records in the list box rather than scroll through each of them.



:

You can't use a multi select list box control as a bound control. It will
always return a Null value.
I'm not sure I understand the issue.

:

Thank you! I am now able to make multiple selections in my list box. Now
the problem lies with the text that is in the list box. There is just field
for the control source on the list box but multiple of records. The field is
just an AutoNumber in the list box. I don't want to have to scroll down all
the way through the list to choose the records I want to send to SQL
statement. Is there a way to make it wrap the text by using columns in the
list box?

:

You will need a List Box control with the Multi Select property set to
Extended. This will allow you to use the familiar, Click to get one, <Ctl>
Click to add another, or <Shift> Click to select a range.

Now, you need a Row Source for the field. It should look something like:

SELECT DISTINCT SomeFieldName FROM SomeTableName

This will return one occurance of every value in the field.

Once the user has selected a field, you can use the ItemsSelected collection
of the List Box control to put together a where condition for your SQL
statement:

Dim varItem As Variant
Dim strWhere as String

strWhere = "IN("
For Each varItem In Me.MyListBoxName.ItemsSelected
strWhere = strWhere & Chr(34) & Me.MyListBoxName.ItemData(varItem) & _
Chr(34) & ","
Next varItem
'Take off the last comma
strWhere = Left(strWhere, Len(strWhere) -1)
'Add the closing paren
strWhere = strWhere & ")"

Now you have a string you can use in your SQL that will filter based on the
selections maked in the list box.

:

Hello,

I want to be able to make multiple selections on my form from the same field
in the same table but allow specific records to be selected, as well as a
range of records and certain records (comma delimited?). After the fields
are selected, I want to pass this value into a SQL statement to perform the
query. How do I do this?
 
S

SusanV

Is the field numeric? If so, drop the quotes
"WHERE [tbl Event].[Event ID] IN (1, 3);"
--
hth,
SusanV


BethHill said:
I am using the IN() instead of all of those OR statements in SQL. I am
getting an error message that says "Data type mismatch in criteria
expression." What does this mean? Here is the "stWhere" string when the
user selects "1" and "3" in the list box.
"WHERE [tbl Event].[Event ID] IN ("1", "3");"

Thank you for your help! I have never used the IN part of SQL before.

Klatuu said:
Why?

The IN() is a whole lot easier than a string or ORs.

To change it, you will have to recode where strWhere is created.
Even if you choose to do that, all those useless ((((((((((((((((( that
Access puts in can be omitted.

WHERE [tbl Event].[Event ID])=1 OR [tbl Event].[Event ID]=3;




BethHill said:
I have a new problem. I am using the multi-select list box and it is
correctly allowing the user to make multiple selections. I am using
the code
you gave me to store the selected records in a string. (I put msgbox
in my
code to see the string and it is correctly storing the selected
records.)
The problem I am now having is using the IN statement of SQL.

For example, I want my "stWhere" to look like the following if the user
selects Event ID "1" and Event ID "2". How would I achieve this using
your
For Each....statement?


WHERE ((([tbl Event].[Event ID])=1)) OR ((([tbl Event].[Event ID])=3));


:

I guess you could, but I really think that would take more time and
be more
prone to errors. Also, remember, you want to be able to select a
range or
use <ctl>Click to select multiple noncontiguous items. You would not
be able
to do that easily.

I really believe the multi select list box is the easiest to develop
and
would be the most user friendly.

:

Could I use something else, like a text box and have the user to
sepearte the
choices with commas or do a range with a hyphen? Similar to print
options
when selecting the pages to be printed. If so, do you have the
code to do
this?

:

There is not a better solution that I can think of. I tried
using a combo
with auto expand to position the list box, but anything
previously selected
became unselected.

:

The [Event ID] text is very meaningful to the user and is
unique. (It is the
primary key in the "tbl Event" table.) There are currently
about 100+
records or [Event ID]s that the user could choose from but
could be more very
soon. If columns is not a good alternative, do you know of
another option?

:

If you are talking about putting multiple [Event ID]s in the
list box item (1
id per column), it is possible, but not easy, and would be
difficult to
select. How would the user know which column to select? How
would you, as
the developer, know which column to use? Is the [Event ID]
text that is
meaningful to a user? Is it unique? How many are we talking
about?

:

Let me give you my scenario:

In the form, there is a multi-select list box with the
control source as
"SELECT [tbl Event].[Event ID] FROM [tbl Event]; " This
correctly displays
all the records for the Event ID field.

In this list box, there are many records that I have to
scroll through in
order to select the ones that I want. I was wondering if
there is a way to
display more records in the list box rather than scroll
through each of them.



:

You can't use a multi select list box control as a bound
control. It will
always return a Null value.
I'm not sure I understand the issue.

:

Thank you! I am now able to make multiple selections
in my list box. Now
the problem lies with the text that is in the list box.
There is just field
for the control source on the list box but multiple of
records. The field is
just an AutoNumber in the list box. I don't want to
have to scroll down all
the way through the list to choose the records I want
to send to SQL
statement. Is there a way to make it wrap the text by
using columns in the
list box?

:

You will need a List Box control with the Multi
Select property set to
Extended. This will allow you to use the familiar,
Click to get one, <Ctl>
Click to add another, or <Shift> Click to select a
range.

Now, you need a Row Source for the field. It should
look something like:

SELECT DISTINCT SomeFieldName FROM SomeTableName

This will return one occurance of every value in the
field.

Once the user has selected a field, you can use the
ItemsSelected collection
of the List Box control to put together a where
condition for your SQL
statement:

Dim varItem As Variant
Dim strWhere as String

strWhere = "IN("
For Each varItem In
Me.MyListBoxName.ItemsSelected
strWhere = strWhere & Chr(34) &
Me.MyListBoxName.ItemData(varItem) & _
Chr(34) & ","
Next varItem
'Take off the last comma
strWhere = Left(strWhere, Len(strWhere) -1)
'Add the closing paren
strWhere = strWhere & ")"

Now you have a string you can use in your SQL that
will filter based on the
selections maked in the list box.

:

Hello,

I want to be able to make multiple selections on my
form from the same field
in the same table but allow specific records to be
selected, as well as a
range of records and certain records (comma
delimited?). After the fields
are selected, I want to pass this value into a SQL
statement to perform the
query. How do I do this?
 
B

BethHill

Thank you, thank you, thank you! That worked great! :)

SusanV said:
Is the field numeric? If so, drop the quotes
"WHERE [tbl Event].[Event ID] IN (1, 3);"
--
hth,
SusanV


BethHill said:
I am using the IN() instead of all of those OR statements in SQL. I am
getting an error message that says "Data type mismatch in criteria
expression." What does this mean? Here is the "stWhere" string when the
user selects "1" and "3" in the list box.
"WHERE [tbl Event].[Event ID] IN ("1", "3");"

Thank you for your help! I have never used the IN part of SQL before.

Klatuu said:
Why?

The IN() is a whole lot easier than a string or ORs.

To change it, you will have to recode where strWhere is created.
Even if you choose to do that, all those useless ((((((((((((((((( that
Access puts in can be omitted.

WHERE [tbl Event].[Event ID])=1 OR [tbl Event].[Event ID]=3;




:

I have a new problem. I am using the multi-select list box and it is
correctly allowing the user to make multiple selections. I am using
the code
you gave me to store the selected records in a string. (I put msgbox
in my
code to see the string and it is correctly storing the selected
records.)
The problem I am now having is using the IN statement of SQL.

For example, I want my "stWhere" to look like the following if the user
selects Event ID "1" and Event ID "2". How would I achieve this using
your
For Each....statement?


WHERE ((([tbl Event].[Event ID])=1)) OR ((([tbl Event].[Event ID])=3));


:

I guess you could, but I really think that would take more time and
be more
prone to errors. Also, remember, you want to be able to select a
range or
use <ctl>Click to select multiple noncontiguous items. You would not
be able
to do that easily.

I really believe the multi select list box is the easiest to develop
and
would be the most user friendly.

:

Could I use something else, like a text box and have the user to
sepearte the
choices with commas or do a range with a hyphen? Similar to print
options
when selecting the pages to be printed. If so, do you have the
code to do
this?

:

There is not a better solution that I can think of. I tried
using a combo
with auto expand to position the list box, but anything
previously selected
became unselected.

:

The [Event ID] text is very meaningful to the user and is
unique. (It is the
primary key in the "tbl Event" table.) There are currently
about 100+
records or [Event ID]s that the user could choose from but
could be more very
soon. If columns is not a good alternative, do you know of
another option?

:

If you are talking about putting multiple [Event ID]s in the
list box item (1
id per column), it is possible, but not easy, and would be
difficult to
select. How would the user know which column to select? How
would you, as
the developer, know which column to use? Is the [Event ID]
text that is
meaningful to a user? Is it unique? How many are we talking
about?

:

Let me give you my scenario:

In the form, there is a multi-select list box with the
control source as
"SELECT [tbl Event].[Event ID] FROM [tbl Event]; " This
correctly displays
all the records for the Event ID field.

In this list box, there are many records that I have to
scroll through in
order to select the ones that I want. I was wondering if
there is a way to
display more records in the list box rather than scroll
through each of them.



:

You can't use a multi select list box control as a bound
control. It will
always return a Null value.
I'm not sure I understand the issue.

:

Thank you! I am now able to make multiple selections
in my list box. Now
the problem lies with the text that is in the list box.
There is just field
for the control source on the list box but multiple of
records. The field is
just an AutoNumber in the list box. I don't want to
have to scroll down all
the way through the list to choose the records I want
to send to SQL
statement. Is there a way to make it wrap the text by
using columns in the
list box?

:

You will need a List Box control with the Multi
Select property set to
Extended. This will allow you to use the familiar,
Click to get one, <Ctl>
Click to add another, or <Shift> Click to select a
range.

Now, you need a Row Source for the field. It should
look something like:

SELECT DISTINCT SomeFieldName FROM SomeTableName

This will return one occurance of every value in the
field.

Once the user has selected a field, you can use the
ItemsSelected collection
of the List Box control to put together a where
condition for your SQL
statement:

Dim varItem As Variant
Dim strWhere as String

strWhere = "IN("
For Each varItem In
Me.MyListBoxName.ItemsSelected
strWhere = strWhere & Chr(34) &
Me.MyListBoxName.ItemData(varItem) & _
Chr(34) & ","
Next varItem
'Take off the last comma
strWhere = Left(strWhere, Len(strWhere) -1)
'Add the closing paren
strWhere = strWhere & ")"

Now you have a string you can use in your SQL that
will filter based on the
selections maked in the list box.

:

Hello,

I want to be able to make multiple selections on my
form from the same field
in the same table but allow specific records to be
selected, as well as a
range of records and certain records (comma
delimited?). After the fields
are selected, I want to pass this value into a SQL
statement to perform the
query. How do I do this?
 
S

SusanV

Glad you got it working - mostly with Klatuu's excellent help!

;-D

SusanV

BethHill said:
Thank you, thank you, thank you! That worked great! :)

SusanV said:
Is the field numeric? If so, drop the quotes
"WHERE [tbl Event].[Event ID] IN (1, 3);"
--
hth,
SusanV


BethHill said:
I am using the IN() instead of all of those OR statements in SQL. I am
getting an error message that says "Data type mismatch in criteria
expression." What does this mean? Here is the "stWhere" string when
the
user selects "1" and "3" in the list box.
"WHERE [tbl Event].[Event ID] IN ("1", "3");"

Thank you for your help! I have never used the IN part of SQL before.

:

Why?

The IN() is a whole lot easier than a string or ORs.

To change it, you will have to recode where strWhere is created.
Even if you choose to do that, all those useless (((((((((((((((((
that
Access puts in can be omitted.

WHERE [tbl Event].[Event ID])=1 OR [tbl Event].[Event ID]=3;




:

I have a new problem. I am using the multi-select list box and it
is
correctly allowing the user to make multiple selections. I am using
the code
you gave me to store the selected records in a string. (I put
msgbox
in my
code to see the string and it is correctly storing the selected
records.)
The problem I am now having is using the IN statement of SQL.

For example, I want my "stWhere" to look like the following if the
user
selects Event ID "1" and Event ID "2". How would I achieve this
using
your
For Each....statement?


WHERE ((([tbl Event].[Event ID])=1)) OR ((([tbl Event].[Event
ID])=3));


:

I guess you could, but I really think that would take more time
and
be more
prone to errors. Also, remember, you want to be able to select a
range or
use <ctl>Click to select multiple noncontiguous items. You would
not
be able
to do that easily.

I really believe the multi select list box is the easiest to
develop
and
would be the most user friendly.

:

Could I use something else, like a text box and have the user to
sepearte the
choices with commas or do a range with a hyphen? Similar to
print
options
when selecting the pages to be printed. If so, do you have the
code to do
this?

:

There is not a better solution that I can think of. I tried
using a combo
with auto expand to position the list box, but anything
previously selected
became unselected.

:

The [Event ID] text is very meaningful to the user and is
unique. (It is the
primary key in the "tbl Event" table.) There are currently
about 100+
records or [Event ID]s that the user could choose from but
could be more very
soon. If columns is not a good alternative, do you know of
another option?

:

If you are talking about putting multiple [Event ID]s in
the
list box item (1
id per column), it is possible, but not easy, and would be
difficult to
select. How would the user know which column to select?
How
would you, as
the developer, know which column to use? Is the [Event
ID]
text that is
meaningful to a user? Is it unique? How many are we
talking
about?

:

Let me give you my scenario:

In the form, there is a multi-select list box with the
control source as
"SELECT [tbl Event].[Event ID] FROM [tbl Event]; " This
correctly displays
all the records for the Event ID field.

In this list box, there are many records that I have to
scroll through in
order to select the ones that I want. I was wondering
if
there is a way to
display more records in the list box rather than scroll
through each of them.



:

You can't use a multi select list box control as a
bound
control. It will
always return a Null value.
I'm not sure I understand the issue.

:

Thank you! I am now able to make multiple
selections
in my list box. Now
the problem lies with the text that is in the list
box.
There is just field
for the control source on the list box but multiple
of
records. The field is
just an AutoNumber in the list box. I don't want to
have to scroll down all
the way through the list to choose the records I
want
to send to SQL
statement. Is there a way to make it wrap the text
by
using columns in the
list box?

:

You will need a List Box control with the Multi
Select property set to
Extended. This will allow you to use the
familiar,
Click to get one, <Ctl>
Click to add another, or <Shift> Click to select a
range.

Now, you need a Row Source for the field. It
should
look something like:

SELECT DISTINCT SomeFieldName FROM SomeTableName

This will return one occurance of every value in
the
field.

Once the user has selected a field, you can use
the
ItemsSelected collection
of the List Box control to put together a where
condition for your SQL
statement:

Dim varItem As Variant
Dim strWhere as String

strWhere = "IN("
For Each varItem In
Me.MyListBoxName.ItemsSelected
strWhere = strWhere & Chr(34) &
Me.MyListBoxName.ItemData(varItem) & _
Chr(34) & ","
Next varItem
'Take off the last comma
strWhere = Left(strWhere, Len(strWhere) -1)
'Add the closing paren
strWhere = strWhere & ")"

Now you have a string you can use in your SQL that
will filter based on the
selections maked in the list box.

:

Hello,

I want to be able to make multiple selections on
my
form from the same field
in the same table but allow specific records to
be
selected, as well as a
range of records and certain records (comma
delimited?). After the fields
are selected, I want to pass this value into a
SQL
statement to perform the
query. How do I do this?
 
B

BethHill

Yes, thank you, Klatuu!!!

SusanV said:
Glad you got it working - mostly with Klatuu's excellent help!

;-D

SusanV

BethHill said:
Thank you, thank you, thank you! That worked great! :)

SusanV said:
Is the field numeric? If so, drop the quotes
"WHERE [tbl Event].[Event ID] IN (1, 3);"
--
hth,
SusanV


I am using the IN() instead of all of those OR statements in SQL. I am
getting an error message that says "Data type mismatch in criteria
expression." What does this mean? Here is the "stWhere" string when
the
user selects "1" and "3" in the list box.
"WHERE [tbl Event].[Event ID] IN ("1", "3");"

Thank you for your help! I have never used the IN part of SQL before.

:

Why?

The IN() is a whole lot easier than a string or ORs.

To change it, you will have to recode where strWhere is created.
Even if you choose to do that, all those useless (((((((((((((((((
that
Access puts in can be omitted.

WHERE [tbl Event].[Event ID])=1 OR [tbl Event].[Event ID]=3;




:

I have a new problem. I am using the multi-select list box and it
is
correctly allowing the user to make multiple selections. I am using
the code
you gave me to store the selected records in a string. (I put
msgbox
in my
code to see the string and it is correctly storing the selected
records.)
The problem I am now having is using the IN statement of SQL.

For example, I want my "stWhere" to look like the following if the
user
selects Event ID "1" and Event ID "2". How would I achieve this
using
your
For Each....statement?


WHERE ((([tbl Event].[Event ID])=1)) OR ((([tbl Event].[Event
ID])=3));


:

I guess you could, but I really think that would take more time
and
be more
prone to errors. Also, remember, you want to be able to select a
range or
use <ctl>Click to select multiple noncontiguous items. You would
not
be able
to do that easily.

I really believe the multi select list box is the easiest to
develop
and
would be the most user friendly.

:

Could I use something else, like a text box and have the user to
sepearte the
choices with commas or do a range with a hyphen? Similar to
print
options
when selecting the pages to be printed. If so, do you have the
code to do
this?

:

There is not a better solution that I can think of. I tried
using a combo
with auto expand to position the list box, but anything
previously selected
became unselected.

:

The [Event ID] text is very meaningful to the user and is
unique. (It is the
primary key in the "tbl Event" table.) There are currently
about 100+
records or [Event ID]s that the user could choose from but
could be more very
soon. If columns is not a good alternative, do you know of
another option?

:

If you are talking about putting multiple [Event ID]s in
the
list box item (1
id per column), it is possible, but not easy, and would be
difficult to
select. How would the user know which column to select?
How
would you, as
the developer, know which column to use? Is the [Event
ID]
text that is
meaningful to a user? Is it unique? How many are we
talking
about?

:

Let me give you my scenario:

In the form, there is a multi-select list box with the
control source as
"SELECT [tbl Event].[Event ID] FROM [tbl Event]; " This
correctly displays
all the records for the Event ID field.

In this list box, there are many records that I have to
scroll through in
order to select the ones that I want. I was wondering
if
there is a way to
display more records in the list box rather than scroll
through each of them.



:

You can't use a multi select list box control as a
bound
control. It will
always return a Null value.
I'm not sure I understand the issue.

:

Thank you! I am now able to make multiple
selections
in my list box. Now
the problem lies with the text that is in the list
box.
There is just field
for the control source on the list box but multiple
of
records. The field is
just an AutoNumber in the list box. I don't want to
have to scroll down all
the way through the list to choose the records I
want
to send to SQL
statement. Is there a way to make it wrap the text
by
using columns in the
list box?

:

You will need a List Box control with the Multi
Select property set to
Extended. This will allow you to use the
familiar,
Click to get one, <Ctl>
Click to add another, or <Shift> Click to select a
range.

Now, you need a Row Source for the field. It
should
look something like:

SELECT DISTINCT SomeFieldName FROM SomeTableName

This will return one occurance of every value in
the
field.

Once the user has selected a field, you can use
the
ItemsSelected collection
of the List Box control to put together a where
condition for your SQL
statement:

Dim varItem As Variant
Dim strWhere as String

strWhere = "IN("
For Each varItem In
Me.MyListBoxName.ItemsSelected
strWhere = strWhere & Chr(34) &
Me.MyListBoxName.ItemData(varItem) & _
Chr(34) & ","
Next varItem
'Take off the last comma
strWhere = Left(strWhere, Len(strWhere) -1)
'Add the closing paren
strWhere = strWhere & ")"

Now you have a string you can use in your SQL that
will filter based on the
selections maked in the list box.

:

Hello,

I want to be able to make multiple selections on
my
form from the same field
in the same table but allow specific records to
be
selected, as well as a
range of records and certain records (comma
delimited?). After the fields
are selected, I want to pass this value into a
SQL
statement to perform the
query. How do I do this?
 
E

EileenD

Hello,

I am also trying to create a multi-selection list box whereby the user can
select one or more of the items in the list and have the items selected saved
in the table. I have followed you code example but am not sure I clearly
understand what's involved.

I have a table called tblTest where I have a listbox controlled field called
"Category" where the data will be stored.
The items for the listbox will come from a lookup table called
"tblCategory". The field cantaining the values is called "CategoryName" and
the related values are:
1 Overarching
2 Risk Assessment
3 Risk Management
4 Regulations/Instruments
5 Compliance & Enforcement
14 Other
15 Don't Know

The form I am using for the listbox is called "frmTest".

First off, I don't understand what you mean by the "ItemsSelected collection
of the List Box control" nor do I know where to input the information from
my tables.
Could you help me out by inserting the info for my data into your code
(below) so I can follow the logic? This would be a great help!

Also, can you use the Multi-Select option with a ComboBox?

Dim varItem As Variant
Dim strWhere as String

strWhere = "IN("
For Each varItem In Me.MyListBoxName.ItemsSelected
strWhere = strWhere & Chr(34) & Me.MyListBoxName.ItemData(varItem) & _
Chr(34) & ","
Next varItem
'Take off the last comma
strWhere = Left(strWhere, Len(strWhere) -1)
'Add the closing paren
strWhere = strWhere & ")"
Thank's for your advice!!

Eileen
 
D

Denver

just want to ask if it is possible to synchronize a combo box and a list box?
inorder to limit my selection after the combo box selection

thanks
 
R

Roger Carlson

Yes it is. It's essentially the same process as cascading combo boxes. On
my website (www.rogersaccesslibrary.com), is a small Access database sample
called "CascadingComboBoxes.mdb" which illustrates how to do this. The
Simple Listbox example would be the closest. Just replace the first listbox
with a combo box. You can find the sample here:
http://www.rogersaccesslibrary.com/forum/forum_posts.asp?TID=389.

--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top