Results 1 to 10 of 10

Thread: joining fields from 3 tables

  1. #1

    Thread Starter
    Lively Member nutstretch's Avatar
    Join Date
    May 2000
    Location
    UK
    Posts
    85

    Question

    I am trying to retrieve data from 3 different tables. one has customer name, one has booking date and status of booking and the third has whether a kennel has been allocated to a booking. table one is linked to table 2 by cust_no and two linked to 3 by booking_no I am trying to find the surname, date of booking of bookings where a kennel has not been allocated to a pet. my code is:

    Set rs1 = db.OpenRecordset("select tblCustomer.surname, tblbooking.booking_no, tblbooking.date_in from tblCustomer, tblBooking, tblBooking_details where tblBooking.cust_no = tblCustomer.cust_no and tblbooking.booking_no = tblBooking_details.booking_no and tblBooking_details.kennel_no = ' " & stat & " ' order by surname ")

    Please someone tell me what i have domne wrong as i am pulling my hair out.

    I am not sure if I need extra quotes or & in the code.

    if at first you don't succeed, drink the rest of the bottle and loop to the same place tomorrow

  2. #2
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105
    The query gets a little easier to read if you use an alias for the table names, as below.

    Set rs1 = db.OpenRecordset("select a.surname, b.booking_no, b.date_in from tblCustomer a, tblBooking b, tblBooking_details c where a.cust_no = b.cust_no and b.booking_no = c.booking_no and c.kennel_no = ' " & stat & " ' order by surname ")

    is kennel-no a numeric or character based field?

  3. #3
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Leavenworth KS USA
    Posts
    482
    When is a record assigned to the tblBooking_details table (i.e., are there default null/other-value records for every booking OR no records until the entry of the kennel_no assignment)? This determines whether you want to use a NOT EXISTS or standard search argument. For example:
    Code:
    ' Route A
    SELECT a.surname, b.booking_no, b.date_in 
    FROM tblCustomer a, tblBooking b, tblBooking_details c
    WHERE a.cust_no = b.cust_no 
      AND b.booking_no = c.booking_no 
      AND c.kennel_no = [default null/other-value]
    ORDER BY a.surname
    
    ' Route B
    SELECT a.surname, b.booking_no, b.date_in 
    FROM tblCustomer a, tblBooking b
    WHERE a.cust_no = b.cust_no 
      AND NOT EXISTS(SELECT c.booking_no 
        FROM tblBooking_details c
        WHERE c.booking_no = b.booking_no)  
    ORDER BY a.surname

  4. #4
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105
    It looks like (from the query) that the only reason booking_details is even in the query is to provide selection criteria, which would tend to indicate it's presence is a requirement... Of course, I could be wrong...

    I'd be real interested in the plan on this query, just to see how the parser decides how to build the joins...


  5. #5

    Thread Starter
    Lively Member nutstretch's Avatar
    Join Date
    May 2000
    Location
    UK
    Posts
    85
    many thanks for Replies. Mongo : I wasn't sure about not exists so added character to field on addittion of the record.

    JHausmann : I tried the code which seems the same as already had but using alias' Nothing appeared in my listview. The field kennel_no is character based as there is calculation need to be done on this and there for takes up less memory Am i right?

    my listview code is as follows as works on all my other listviews.

    Do While Not rs1.EOF

    Set itemX1 = ListView2.ListItems.Add
    itemX1.Text = rs1("booking_no")
    itemX1.SubItems(1) = rs1("surname")
    itemX1.SubItems(2) = rs1("date_in")

    rs1.MoveNext
    Loop

    Do I need & before each and in the code.

    If i put the query into access then i get what i want so i am sure it has something to do with the syntax of my statement.

    many thanks again for you comments
    if at first you don't succeed, drink the rest of the bottle and loop to the same place tomorrow

  6. #6
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105
    Character fields use 1 byte per length. Integer fields use 1, 2 or 4 bytes (in SQL Server) depending on the type of integer specified, Tiny, Small or regular.

    Put a breakpoint on your set command and see what the SQL looks like before you try to run it. My guess is that the variable "stat" has an unexpected value in your program

  7. #7

    Thread Starter
    Lively Member nutstretch's Avatar
    Join Date
    May 2000
    Location
    UK
    Posts
    85
    the variable stat is a data type string

    It is assigned to the same character which is given to the kennel_no at booking

    if at first you don't succeed, drink the rest of the bottle and loop to the same place tomorrow

  8. #8
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105
    do this:

    1) change the code

    dim sSQL as string
    sSQL="select a.surname, b.booking_no, b.date_in from tblCustomer a, tblBooking b, tblBooking_details c where a.cust_no = b.cust_no and b.booking_no = c.booking_no and c.kennel_no = ' " & stat & " ' order by surname "
    Set rs1 = db.OpenRecordset(sSQl)

    2) set a breakpoint on the set rs1 line
    3) run your prog from VB
    4) when it stops at the breakpoint, type (without the quotes) in the immediate window:

    "?sSql"
    5) copy the entire contents of the immediate window into a response to this post

  9. #9

    Thread Starter
    Lively Member nutstretch's Avatar
    Join Date
    May 2000
    Location
    UK
    Posts
    85
    Hi Thanks for helping me. This is what arrived in the immediate window.


    select a.surname, b.booking_no, b.date_in from tblCustomer a, tblBooking b, tblBooking_details c where a.cust_no = b.cust_no and b.booking_no = c.booking_no and c.kennel_no = ' ' order by surname

    if at first you don't succeed, drink the rest of the bottle and loop to the same place tomorrow

  10. #10
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105
    You have no value for stat when the recordset is set. I'd guess because it's a local variable.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width