472,354 Members | 1,450 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 software developers and data experts.

Populate multiple query columns with multiple listbox selections

Hi,

I have a list box that displays 2 columns. Behind it sits a query with
five columns. These are Column1 (DOB), column2 (a concatenated string
of Surname Forname, Title), Column3 (Surname), column4 (Forename) and
column5 (title). Columns 3,4 and 5 are not shown in the list box only
the first two.

DOB Name: [Surname]&" "&[Forename]&", "&[Title]

This would show for example:

10/10/1980 Barrett Carl, Mr
11/11/1981 Smith Robert, Mr

etc..

What I want is for the selected items in the listbox to have the
corresponding data in columns 1,3,4 and 5 insterted into the query
criteria section. For example if Barrett Carl, Mr is selected I want
the 10/10/1980 entered as DOB query criteria (query column1), Barrett
entered as Surname query criteria (query column3), forename into
column4 and the title into column5, so that it displays this along one
row over the criteria section of the query. There will be multiple
entries in the criteria section depending on how many items are
selected from the listbox.

This will form the where part of an SQL statement that I will add to
the select part of the statement to create a qdf(query) from which a
report will be displayed to the user.

Hope this makes sense.

Any help would be appreciated.

Cheers.

Nov 13 '05 #1
4 5263

<ca**********@newcastle.gov.uk> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
Hi,

I have a list box that displays 2 columns. Behind it sits a query with
five columns. These are Column1 (DOB), column2 (a concatenated string
of Surname Forname, Title), Column3 (Surname), column4 (Forename) and
column5 (title). Columns 3,4 and 5 are not shown in the list box only
the first two.

DOB Name: [Surname]&" "&[Forename]&", "&[Title]

This would show for example:

10/10/1980 Barrett Carl, Mr
11/11/1981 Smith Robert, Mr

etc..

What I want is for the selected items in the listbox to have the
corresponding data in columns 1,3,4 and 5 insterted into the query
criteria section. For example if Barrett Carl, Mr is selected I want
the 10/10/1980 entered as DOB query criteria (query column1), Barrett
entered as Surname query criteria (query column3), forename into
column4 and the title into column5, so that it displays this along one
row over the criteria section of the query. There will be multiple
entries in the criteria section depending on how many items are
selected from the listbox.

This will form the where part of an SQL statement that I will add to
the select part of the statement to create a qdf(query) from which a
report will be displayed to the user.

Hope this makes sense.

Any help would be appreciated.

Cheers.


Well it does all make sense, but it's not clear what bit you need help with.
As I understand it, you have a multi-select listbox which should enable the
user to select one or more records to display. You intend to build up an
SQL string and then either create or overwrite a querydef object.
The only part I don't understand is why you put multiple criteria in for
your query. Presumably there is a table of individuals with a primary key,
so why not have a hidden column in the listbox which contains the ID? You
then loop through the listbox to build up the SQL, eg:

SELECT tblPerson.* FROM tblPerson
WHERE tblPerson.ID IN (1023,84,1396,205)

As another adjustment, you could consider whether a querydef is the way to
go. If you are opening a report to view, you could supply the WHERE clause
in the DoCmd.OpenReport bit. This is quite standard and perhaps not as
messy as fiddling with querydefs. But is there any specific bit which you
can't do?
Nov 13 '05 #2
Hi Justin,

Thanks for the hint. I've used the ID field and and it does what I
want. I don't know why I was trying to make things more difficult for
myself.

Could you elaborate a bit more on how to use the WHERE clause as the
source of the report instead of using a qdf.

Heres some of the code I have in my form that builds the SQL that is
used for the qdf that creates the query used as the source for my
report.

If I can just use the sql as the report source then that would be
great.

Cheers.

StartDate = [Forms]![frmReportPerpetratorCases]![txtFromDate]
EndDate = [Forms]![frmReportPerpetratorCases]![txtToDate]

For Each varItem In Me![lstPerpetrator].ItemsSelected
strSQLWherePerpetrator = strSQLWherePerpetrator &
"tblIncidentPerpetrator.PerpetratorID = " &
Me![lstPerpetrator].ItemData(varItem) & " OR "
Next varItem

strSQLWherePerpetrator = Left(strSQLWherePerpetrator,
Len(strSQLWherePerpetrator) - 4)

strSQLSelect = "SELECT tblCase.CaseReferenceNumber, tblCase.CaseNumber,
tblIncident.IncidentDate, tblIncident.IncidentType, " _
& "tblIncidentPerpetrator.PerpetratorID,
tblIncidentPerpetrator.PerpetratorTenRefNo,
tblIncidentPerpetrator.PerpetratorTitle,
tblIncidentPerpetrator.PerpetratorForename, " _
& "tblIncidentPerpetrator.PerpetratorSurname,
tblIncidentPerpetrator.PerpetratorDOB,
tblIncidentPerpetrator.PerpetratorAddress1, " _
& "tblIncidentPerpetrator.PerpetratorAddress2,
tblIncidentPerpetrator.PerpetratorAddress3,
tblIncidentPerpetrator.PerpetratorAddress4, " _
& "tblIncidentPerpetrator.PerpetratorPostCode,
tblIncidentPerpetrator.PerpetratorTenure,
tblIncidentPerpetrator.PerpetratorTenancyType " _
& "FROM (tblCase INNER JOIN tblIncident ON tblCase.CaseReferenceNumber
= tblIncident.CaseReferenceNumber) " _
& "INNER JOIN tblIncidentPerpetrator ON tblIncident.IncidentID =
tblIncidentPerpetrator.IncidentID"

strSQLWhere = " WHERE (((tblIncident.IncidentDate) >= #" &
Format$(StartDate, "dd/mmm/yyyy") & "# AND " _
& "(tblIncident.IncidentDate) <= #" & Format$(EndDate, "dd/mmm/yyyy") &
"# AND (" & strSQLWherePerpetrator & ")))"

If Len(strSQLWherePerpetrator) = 0 Then
MsgBox "You must select an Incident Type from the list. Please
try again."
lstPerpetrator.SetFocus
Exit Sub
End If

strSQL = strSQLSelect + strSQLWhere

Set qdf = dbs.CreateQueryDef("qryReportPerpetratorCases", strSQL)

Me.Visible = False
[Forms]![frmMainMenu].Visible = False

DoCmd.OpenReport "repPerpetratorCases", acPreview, "", ""

Nov 13 '05 #3

<ca**********@newcastle.gov.uk> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Hi Justin,

Thanks for the hint. I've used the ID field and and it does what I
want. I don't know why I was trying to make things more difficult for
myself.

Could you elaborate a bit more on how to use the WHERE clause as the
source of the report instead of using a qdf.

Heres some of the code I have in my form that builds the SQL that is
used for the qdf that creates the query used as the source for my
report.

If I can just use the sql as the report source then that would be
great.

Cheers.

StartDate = [Forms]![frmReportPerpetratorCases]![txtFromDate]
EndDate = [Forms]![frmReportPerpetratorCases]![txtToDate]

For Each varItem In Me![lstPerpetrator].ItemsSelected
strSQLWherePerpetrator = strSQLWherePerpetrator &
"tblIncidentPerpetrator.PerpetratorID = " &
Me![lstPerpetrator].ItemData(varItem) & " OR "
Next varItem

strSQLWherePerpetrator = Left(strSQLWherePerpetrator,
Len(strSQLWherePerpetrator) - 4)

strSQLSelect = "SELECT tblCase.CaseReferenceNumber, tblCase.CaseNumber,
tblIncident.IncidentDate, tblIncident.IncidentType, " _
& "tblIncidentPerpetrator.PerpetratorID,
tblIncidentPerpetrator.PerpetratorTenRefNo,
tblIncidentPerpetrator.PerpetratorTitle,
tblIncidentPerpetrator.PerpetratorForename, " _
& "tblIncidentPerpetrator.PerpetratorSurname,
tblIncidentPerpetrator.PerpetratorDOB,
tblIncidentPerpetrator.PerpetratorAddress1, " _
& "tblIncidentPerpetrator.PerpetratorAddress2,
tblIncidentPerpetrator.PerpetratorAddress3,
tblIncidentPerpetrator.PerpetratorAddress4, " _
& "tblIncidentPerpetrator.PerpetratorPostCode,
tblIncidentPerpetrator.PerpetratorTenure,
tblIncidentPerpetrator.PerpetratorTenancyType " _
& "FROM (tblCase INNER JOIN tblIncident ON tblCase.CaseReferenceNumber
= tblIncident.CaseReferenceNumber) " _
& "INNER JOIN tblIncidentPerpetrator ON tblIncident.IncidentID =
tblIncidentPerpetrator.IncidentID"

strSQLWhere = " WHERE (((tblIncident.IncidentDate) >= #" &
Format$(StartDate, "dd/mmm/yyyy") & "# AND " _
& "(tblIncident.IncidentDate) <= #" & Format$(EndDate, "dd/mmm/yyyy") &
"# AND (" & strSQLWherePerpetrator & ")))"

If Len(strSQLWherePerpetrator) = 0 Then
MsgBox "You must select an Incident Type from the list. Please
try again."
lstPerpetrator.SetFocus
Exit Sub
End If

strSQL = strSQLSelect + strSQLWhere

Set qdf = dbs.CreateQueryDef("qryReportPerpetratorCases", strSQL)

Me.Visible = False
[Forms]![frmMainMenu].Visible = False

DoCmd.OpenReport "repPerpetratorCases", acPreview, "", ""

Set the report's datasource as above, but with no where clause at all.
Then, field names not accurate, but try something like:

strWhere = "PerpID IN (104,109,207,1325) AND PerpDate BETWEEN #01/01/2005#
AND #01/31/2005#"

DoCmd.OpenReport "repPerpetratorCases", acViewPreview, , strWhere

Just make sure, they cannot simply open the report without specifiying this
otherwise they could inadvertently print out all records. If this is a
danger, there are ways around it depending on which version of Access.

PS Although I see you took my hint for using the ID, you decided to use the
OR syntax instead of an IN. OR clauses can really slow things down and
although you would think that the query ... WHERE PerpID=1 OR PerpID=2 OR
PerpID=3 might be the same as WHERE PerpID IN (1,2,3) the speed can differ
greatly.


Nov 13 '05 #4

<ca**********@newcastle.gov.uk> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Hi Justin,

Thanks for the hint. I've used the ID field and and it does what I
want. I don't know why I was trying to make things more difficult for
myself.

Could you elaborate a bit more on how to use the WHERE clause as the
source of the report instead of using a qdf.

Heres some of the code I have in my form that builds the SQL that is
used for the qdf that creates the query used as the source for my
report.

If I can just use the sql as the report source then that would be
great.

Cheers.

StartDate = [Forms]![frmReportPerpetratorCases]![txtFromDate]
EndDate = [Forms]![frmReportPerpetratorCases]![txtToDate]

For Each varItem In Me![lstPerpetrator].ItemsSelected
strSQLWherePerpetrator = strSQLWherePerpetrator &
"tblIncidentPerpetrator.PerpetratorID = " &
Me![lstPerpetrator].ItemData(varItem) & " OR "
Next varItem

strSQLWherePerpetrator = Left(strSQLWherePerpetrator,
Len(strSQLWherePerpetrator) - 4)

strSQLSelect = "SELECT tblCase.CaseReferenceNumber, tblCase.CaseNumber,
tblIncident.IncidentDate, tblIncident.IncidentType, " _
& "tblIncidentPerpetrator.PerpetratorID,
tblIncidentPerpetrator.PerpetratorTenRefNo,
tblIncidentPerpetrator.PerpetratorTitle,
tblIncidentPerpetrator.PerpetratorForename, " _
& "tblIncidentPerpetrator.PerpetratorSurname,
tblIncidentPerpetrator.PerpetratorDOB,
tblIncidentPerpetrator.PerpetratorAddress1, " _
& "tblIncidentPerpetrator.PerpetratorAddress2,
tblIncidentPerpetrator.PerpetratorAddress3,
tblIncidentPerpetrator.PerpetratorAddress4, " _
& "tblIncidentPerpetrator.PerpetratorPostCode,
tblIncidentPerpetrator.PerpetratorTenure,
tblIncidentPerpetrator.PerpetratorTenancyType " _
& "FROM (tblCase INNER JOIN tblIncident ON tblCase.CaseReferenceNumber
= tblIncident.CaseReferenceNumber) " _
& "INNER JOIN tblIncidentPerpetrator ON tblIncident.IncidentID =
tblIncidentPerpetrator.IncidentID"

strSQLWhere = " WHERE (((tblIncident.IncidentDate) >= #" &
Format$(StartDate, "dd/mmm/yyyy") & "# AND " _
& "(tblIncident.IncidentDate) <= #" & Format$(EndDate, "dd/mmm/yyyy") &
"# AND (" & strSQLWherePerpetrator & ")))"

If Len(strSQLWherePerpetrator) = 0 Then
MsgBox "You must select an Incident Type from the list. Please
try again."
lstPerpetrator.SetFocus
Exit Sub
End If

strSQL = strSQLSelect + strSQLWhere

Set qdf = dbs.CreateQueryDef("qryReportPerpetratorCases", strSQL)

Me.Visible = False
[Forms]![frmMainMenu].Visible = False

DoCmd.OpenReport "repPerpetratorCases", acPreview, "", ""


Possible repost:

Set the report's datasource as above, but with no where clause at all.
Then, field names not accurate, but try something like:

strWhere = "PerpID IN (104,109,207,1325) AND PerpDate BETWEEN #01/01/2005#
AND #01/31/2005#"

DoCmd.OpenReport "repPerpetratorCases", acViewPreview, , strWhere

Just make sure, they cannot simply open the report without specifiying this
otherwise they could inadvertently print out all records. If this is a
danger, there are ways around it depending on which version of Access.

PS Although I see you took my hint for using the ID, you decided to use the
OR syntax instead of an IN. OR clauses can really slow things down and
although you would think that the query ... WHERE PerpID=1 OR PerpID=2 OR
PerpID=3 might be the same as WHERE PerpID IN (1,2,3) the speed can differ
greatly.

Nov 13 '05 #5

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

Similar topics

1
by: theoryboy | last post by:
I'm trying to implement multiple selection functionality in a Tix Hlist using a control-click. I've bound control-click to a function that uses selection_set to add to the selection, but it doesn't...
2
by: Jen F. | last post by:
I have inherited a medical database in which there are multiple values stored in a single field (ie. "Current Conditions" field might contain 1-20 different conditions, separated by comma (ie....
4
by: Alan Lane | last post by:
Hello world: I'm including both code and examples of query output. I appologize if that makes this message longer than it should be. Anyway, I need to change the query below into a pivot table...
0
by: starace | last post by:
I have designed a form that has 5 different list boxes where the selections within each are used as criteria in building a dynamic query. Some boxes are set for multiple selections but these list...
6
by: Joe | last post by:
I have 2 multi-list boxes, 1 displays course categories based on a table called CATEGORIES. This table has 2 fields CATEGORY_ID, CATEGORY_NAME The other multi-list box displays courses based on...
0
by: bill yeager | last post by:
Duray, it helps in regards to knowing how to get the items that were selected in the lisbox, but I'm going in reverse --- after I get the items from the db that were selected, I'd like to be able...
1
by: Marisa | last post by:
Hi. I am new to C#, and I am having trouble getting the multiple selection of listbox to work. I have an asp.net web form, and I need two listboxes which are filled with an access table. I have...
0
by: Gunnar Hurtig | last post by:
Hi All I am relatively new to Tkinter and am putting a wraparound to the ATNF ASAP program. In one part I present several long lists in list boxes for selection. My code will remember the multiple...
12
by: micarl | last post by:
How would i print a report based on criteria selected from several Combo Boxes as well as multiple Multi Select List Boxes, that are located on the same form? I can get one Multi List Box, just...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made but the http to https rule only works for...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...

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.