473,408 Members | 2,813 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,408 software developers and data experts.

query from 2 tables list of items just once

i have this code below which pull the data from tbl_bookings for 1 event. each event has a room(s) allocated to it stored in tbl_roombookings. But they can have more than one room allocated say room 1 2 and 3

currenly this is shown in a window at the bottom as a quick view for the events on a certain day. i would like for them to be show next to them as well but when i try and add anything it then has the event listed multipul times as there are rooms.

Expand|Select|Wrap|Line Numbers
  1. strSQL2 = "SELECT tbl_Bookings.EventID, tbl_Bookings.EventTitle As [Course Title], tbl_Bookings.Status " & _
  2.         ", tbl_Bookings.StartTime As [Start Time], tbl_Bookings.EndTime As [End Time] " & _
  3.         ", tbl_Bookings.Contact, Security, IIF(Count(CateringRequests.EventID)>0, 'Yes', 'No') As Catering, tbl_Bookings.Int_Ext As [Booking] " & _
  4.         "FROM tbl_Bookings LEFT JOIN CateringRequests " & _
  5.         "ON tbl_Bookings.EventID = CateringRequests.EventID " & _
  6.         "WHERE MeetingDate = " & ctlDayBlock.Tag & " " & _
  7.         "GROUP BY tbl_Bookings.EventID, EventTitle, Status, StartTime, EndTime, Contact, Security, Int_Ext " & _
  8.         "ORDER BY tbl_Bookings.EventID"
  9.  
attached is also a picture showing what i mean, in this example APLS has about 5 rooms which link to this one event and the other 2 in a normal report that i can do it would APLS would show 5 times one for each room. which is not what i want. I would like it to show room in column and then just have room1, room2, room3, etc...

hope this makes sence?
Attached Images
File Type: jpg example1.jpg (40.3 KB, 241 views)
Mar 2 '17 #1

✓ answered by jforbes

This might be closer:
Expand|Select|Wrap|Line Numbers
  1. strSQL2 = "SELECT tbl_Bookings.EventID, tbl_Bookings.EventTitle As [Course Title], tbl_Bookings.Status " & _
  2.         ", tbl_Bookings.StartTime As [Start Time], tbl_Bookings.EndTime As [End Time] " & _
  3.         ", tbl_Bookings.Contact, Security, IIF(Count(CateringRequests.EventID)>0, 'Yes', 'No') As Catering, tbl_Bookings.Int_Ext As [Booking] " & _
  4.         ", concatRelated('Room', 'tbl_RoomBookings', 'EventID=' & [EventID] & ') AS Rooms " & _
  5.         "FROM tbl_Bookings LEFT JOIN CateringRequests " & _
  6.         "ON tbl_Bookings.EventID = CateringRequests.EventID " & _
  7.         "WHERE MeetingDate = " & ctlDayBlock.Tag & " " & _
  8.         "GROUP BY tbl_Bookings.EventID, EventTitle, Status, StartTime, EndTime, Contact, Security, Int_Ext " & _
  9.         "ORDER BY tbl_Bookings.EventID"
I haven't tested it or anything, so you may have to tweak it.

14 1388
jforbes
1,107 Expert 1GB
I think what your looking for is Concatenate values from related records

It can be used to take a Column over Multiple rows and make one Value out of it.
Mar 3 '17 #2
something like this? i'm getting a compile error so guessing that i put something in the wrong order.Still new with the whole VB scripting and SQL statements.

Expand|Select|Wrap|Line Numbers
  1. strSQL2 = "SELECT tbl_Bookings.EventID, tbl_Bookings.EventTitle As [Course Title], tbl_Bookings.Status " & _
  2.         ", tbl_Bookings.StartTime As [Start Time], tbl_Bookings.EndTime As [End Time] " & _
  3.         ", tbl_Bookings.Contact, Security, IIF(Count(CateringRequests.EventID)>0, 'Yes', 'No') As Catering, tbl_Bookings.Int_Ext As [Booking] " & _
  4.         "FROM tbl_Bookings LEFT JOIN CateringRequests " & _
  5.         "ON tbl_Bookings.EventID = CateringRequests.EventID " & _
  6.         "WHERE MeetingDate = " & ctlDayBlock.Tag & " " & _
  7.         "SELECT Room, concatRelated("Room", "tbl_RoomBookings", "EventID = " & [EventID])" " & _
  8.         "GROUP BY tbl_Bookings.EventID, EventTitle, Status, StartTime, EndTime, Contact, Security, Int_Ext " & _
  9.         "ORDER BY tbl_Bookings.EventID"
Mar 3 '17 #3
jforbes
1,107 Expert 1GB
This might be closer:
Expand|Select|Wrap|Line Numbers
  1. strSQL2 = "SELECT tbl_Bookings.EventID, tbl_Bookings.EventTitle As [Course Title], tbl_Bookings.Status " & _
  2.         ", tbl_Bookings.StartTime As [Start Time], tbl_Bookings.EndTime As [End Time] " & _
  3.         ", tbl_Bookings.Contact, Security, IIF(Count(CateringRequests.EventID)>0, 'Yes', 'No') As Catering, tbl_Bookings.Int_Ext As [Booking] " & _
  4.         ", concatRelated('Room', 'tbl_RoomBookings', 'EventID=' & [EventID] & ') AS Rooms " & _
  5.         "FROM tbl_Bookings LEFT JOIN CateringRequests " & _
  6.         "ON tbl_Bookings.EventID = CateringRequests.EventID " & _
  7.         "WHERE MeetingDate = " & ctlDayBlock.Tag & " " & _
  8.         "GROUP BY tbl_Bookings.EventID, EventTitle, Status, StartTime, EndTime, Contact, Security, Int_Ext " & _
  9.         "ORDER BY tbl_Bookings.EventID"
I haven't tested it or anything, so you may have to tweak it.
Mar 3 '17 #4
everything just goes blank in the box no headers no data

but also no errors??
Mar 6 '17 #5
jforbes
1,107 Expert 1GB
A guess from the picture you provided is that the SELECT statement isn't returning any records.

Typically, when I'm not getting records when I think I should, I put a break point in the Code, debug.print the SQL string (strSQL2), copy the SQL onto the Clipboard, open a new Query, flip over to SQL Mode, paste the SQL into the new query, attempt to run it and see what it does. This might be helpful: How to Debug SQL String
Mar 6 '17 #6
tried putting the debug.print in the VBA code in a couple of places but all with the same results.

put it right in front of the strSQL2 and in front of the lstevents.rowsource = strSQL2 and a few other places.

i either get blanks appearing or false i don't get the SQL code to copy into a query.

i have done that before when i was expanding the SQL last time, but cant get it to work this time to work on it.
Mar 6 '17 #7
looking thought some other information that would make sence for it being false, as thats the overall one saying false to no information, doesnt make it into a SQL to paste into the query and work that way. is that correct in my assumptions?
Mar 6 '17 #8
found it.

Expand|Select|Wrap|Line Numbers
  1. lstEvents.RowSource = strSQL2
  2. Debug.Print strSQL2
  3.  
So code for SQL comes out at
Expand|Select|Wrap|Line Numbers
  1. SELECT tbl_Bookings.EventID, tbl_Bookings.EventTitle As [Course Title], tbl_Bookings.Status , tbl_Bookings.StartTime As [Start Time], tbl_Bookings.EndTime As [End Time] , tbl_Bookings.Contact, Security, IIF(Count(CateringRequests.EventID)>0, 'Yes', 'No') As Catering, tbl_Bookings.Int_Ext As [Booking] , concatRelated('Room', 'tbl_RoomBookings', 'EventID=' & [EventID] & ') AS Rooms FROM tbl_Bookings LEFT JOIN CateringRequests ON tbl_Bookings.EventID = CateringRequests.EventID WHERE MeetingDate = 42801 GROUP BY tbl_Bookings.EventID, EventTitle, Status, StartTime, EndTime, Contact, Security, Int_Ext ORDER BY tbl_Bookings.EventID
  2.  
Mar 6 '17 #9
getting a syntax error in sting

is this something do with the extra table now being called on, does this also need to be in the select, or left join like the other table that is also being called?

or is this an concatrelated, not transferring from VBA to SQL?

getting a litte lost to be fair
Mar 6 '17 #10
jforbes
1,107 Expert 1GB
I did notice a typo. The Highlighted portion in the following should be removed:
Expand|Select|Wrap|Line Numbers
  1. ", concatRelated('Room', 'tbl_RoomBookings', 'EventID=' & [EventID] & ') AS Rooms " & _
  • Your code is using VBA to Create SQL String.
  • It then applies the SQL String to RowSource of the ListBox.
  • The ListBox recognizes that it has a newly defined RowSource and Queries the database using the SQL String.
  • Access, while performing the Query, will call the ConcatRelated() function, (which is a VBA call) for each row.

So there really isn't another table being added to the SELECT Statement. The table is being included in the Function call which will perform a small query and return a string with the Concatenated fields.

Again, usually the best way to troubleshoot something like this is to get the SQL String on to the Clipboard, create a Query and Paste the SQL into the new Query and let Access tell you what is wrong with it.
Mar 6 '17 #11
cool manage to get that into a query, had the error of: the '[EventID]' could refer to more than one table listed in the from clause of your SQL

so added in which table it was looking from which changed the error:

undefined function 'concatRelated' in expression

Expand|Select|Wrap|Line Numbers
  1. strSQL2 = "SELECT tbl_Bookings.EventID, tbl_Bookings.EventTitle As [Course Title], tbl_Bookings.Status " & _
  2.         ", tbl_Bookings.StartTime As [Start], tbl_Bookings.EndTime As [End] " & _
  3.         ", tbl_Bookings.Contact, Security, IIF(Count(CateringRequests.EventID)>0, 'Yes', 'No') As Catering, tbl_Bookings.Int_Ext As [Booking] " & _
  4.         ", concatRelated('Room', 'tbl_RoomBookings', 'EventID=' & [tbl_Bookings.EventID] ) AS Rooms " & _
  5.         "FROM tbl_Bookings LEFT JOIN CateringRequests " & _
  6.         "ON tbl_Bookings.EventID = CateringRequests.EventID " & _
  7.         "WHERE MeetingDate = " & ctlDayBlock.Tag & " " & _
  8.         "GROUP BY tbl_Bookings.EventID, EventTitle, Status, StartTime, EndTime, Contact, Security, Int_Ext, Rooms " & _
  9.         "ORDER BY tbl_Bookings.EventID"
  10.  
do i need to define this somewhere in the VBA script for it to call it? or is it already a predefined script built into access?
Mar 7 '17 #12
a development, try the expression builder in access to see if ConcatRelated existed. it didn't but did find ConcatChr changed that and started to get some results. This then showed up an error of spelling which i corrected and then started populating one room in the table/Query

Expand|Select|Wrap|Line Numbers
  1. strSQL2 = "SELECT tbl_Bookings.EventID, tbl_Bookings.EventTitle As [Course Title], tbl_Bookings.Status " & _
  2.         ", tbl_Bookings.StartTime As [Start], tbl_Bookings.EndTime As [End] " & _
  3.         ", tbl_Bookings.Contact, Security, IIF(Count(CateringRequests.EventID)>0, 'Yes', 'No') As Catering, tbl_Bookings.Int_Ext As [Booking] " & _
  4.         ", ConcatChr('Room', 'tbl_RoomBooking', 'EventID=' & [tbl_Bookings].[EventID] ) AS [Rooms] " & _
  5.         "FROM tbl_Bookings LEFT JOIN CateringRequests " & _
  6.         "ON tbl_Bookings.EventID = CateringRequests.EventID " & _
  7.         "WHERE MeetingDate = " & ctlDayBlock.Tag & " " & _
  8.         "GROUP BY tbl_Bookings.EventID, EventTitle, Status, StartTime, EndTime, Contact, Security, Int_Ext " & _
  9.         "ORDER BY tbl_Bookings.EventID"
  10.  
  11.  
Mar 7 '17 #13
jforbes
1,107 Expert 1GB
You get the ConcatRelated() function from Allen Browne's website:
Concatenate values from related records
He has some instructions on how to put it into a module for you. When you have the function available, Access will stop complaining about it. I'm not sure what ConcatChr() is, but it sounds like a function to build one string out of many, which isn't exactly what your trying to do.
Mar 7 '17 #14
Think i have cracked it i was looking at the link before you sent, doesn't say what to name the module, i had named it the same as the function so it was confusing it and i assume stopping the function from working.
Mar 7 '17 #15

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

Similar topics

5
by: Peter Collinson | last post by:
Hi... Is there any way to style a List Item a different color and size than the <LI> in an Ordered List? I'd like a red super-script number and a dark blue text in a page's footnotes. And...
2
by: Unknown User | last post by:
How can I separate list items displayed inline by vertical bars | ? If I use a background image that simulate a vertical bar, and I put this bg image behind each list item, the last list item will...
1
by: Sakharam Phapale | last post by:
Hi All, How to show dropdown list of menu items just like click on Parent menu. For example, Edit (Parent menu) Cut (child menu) Copy (child menu) Paste (child menu)
2
by: George | last post by:
Using VS.NET 2002\Web Form\VB I have a dropdown list that will contain letters of the alphabet. At startup, it has only the letter A in the list. I add letters to the items list, as needed....
2
by: Sakharam Phapale | last post by:
Hi All, How to show dropdown list of menu items just like click on Parent menu. For example, Edit (Parent menu) Cut (child menu) Copy (child menu) Paste (child menu)
7
by: David | last post by:
Hello All, I am trying to split a long list of list items ( li ) into 3 groups and append them to a div on the page. Below is the test page that I have created. It's fairly simple but I can't...
4
by: Keith Hughitt | last post by:
For example, If you have a list: <ul> <li>item 1 is short.</li> <li>item 2 is a little bit longer</li> </ul> regardless of the size of the contents of each list item, the element
0
by: c0mrade | last post by:
I have a problem with getting the list items, below is my hibernate code, after that code there is my method ..and below that is my junit test. How can I make sure that query is executing properly,...
16
Frinavale
by: Frinavale | last post by:
What I'd like to do is take a list and display it's items in a circle. For example (the order of the in the circle doesn't matter..except for Item1, it has to be at the top): Item1 ...
1
by: oanaramonaelena | last post by:
Hi, I want to create a combo box to transfer some items from o list to another list items in the same form and in the same time to actualize two tables: first, from I transfer items and the second,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.