Results 1 to 18 of 18

Thread: Joining tables[Resolved]

  1. #1

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Resolved Joining tables[Resolved]

    HI! I have two tables namely, Author and Titles. I want to display on flexgrid.
    I have AuID, AuLName and AUFName for Author table and Title, Subject and ISBN for Titles. I would like to display the AuID, AuLName, AUFName, Title, Subject and ISBN in flexgrid.

    sample code is very much appreciated. Thank you in advance.
    Last edited by Simply Me; Apr 12th, 2005 at 09:41 AM.
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Joining tables

    What field links the two tables? Is there an Id field in each?

  3. #3

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Joining tables

    Quote Originally Posted by Hack
    What field links the two tables? Is there an Id field in each?
    Actually i'm planning to change the AuID with ISBN so that i can relate the two tables using ISBN. Any suggestion what should i use as link?
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Joining tables

    There has to be something in both tables that relate the records in one table to records in another table. In this case, and Author Id would make the most sense. If you had have that kind of relationship between the two tables, then getting the information out would be a simple but, without some type of logical relationship between the two, SQL isn't going to know what you are after.

  5. #5
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Joining tables

    I don't think that changing AuID for ISBN is necessarily a good idea. Won't each author have (potentially) many Titles?
    Why not add AuID to Titles, and then you can link on that column.
    (Or if you want to support multiple authors of one title, then have a 'junction table', comprising simply of AuID and ISBN, then you can JOIN though this table...)

  6. #6

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Joining tables

    Ok. I have AuID now in both tables. Any code snippet pls?
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  7. #7
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Joining tables

    Code:
    SELECT AuLName, AUFName, Title, Subject, ISBN
    FROM Authors a
    INNER JOIN Titles t
     ON a.AuID = t.AuID

  8. #8

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Joining tables

    Quote Originally Posted by PilgrimPete
    Code:
    SELECT AuLName, AUFName, Title, Subject, ISBN
    FROM Authors a
    INNER JOIN Titles t
     ON a.AuID = t.AuID
    That's it? a and t are aliases? will the code above display the fields in the flexgrid?
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Joining tables

    That bugger PilgramPete beat me to again!

    I had (almost precisely) the same code snippet he had, so I won't bother repeating it. To dump it to your MSHFlexgrid (if you are using an MSFlexgrid, swap it for an MSHFlexgrid), create a recordset from the query, then use this
    VB Code:
    1. Set MSHFlexgrid1.Recordset = adoRs
    Where adoRs is the name of the recordset you created by running PilgramPete's query.
    Last edited by Hack; Apr 11th, 2005 at 09:24 AM.

  10. #10
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Joining tables

    Yep. I'm afraid that's it. There's nothing to this programming malarkey...
    Yes, a and t are aliases. I'm too lazy to be typing table names.
    Yes, this should populate a flexgrid - if you bind it to the recordset, or loop through the recordset adding the records.

  11. #11

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Joining tables

    Thanks to both of you. I'll try it now.
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  12. #12

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Joining tables

    It's working! Following up question. if I am going to add a record. How will the INSERT statement will look like?
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  13. #13
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Joining tables

    Quote Originally Posted by Simply Me
    It's working! Following up question. if I am going to add a record. How will the INSERT statement will look like?
    Something like this line.
    Code:
    INSERT INTO authors (id, lname, fname) VALUES (xx, 'Hack', 'Mr')

  14. #14

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Joining tables

    Quote Originally Posted by Hack
    Something like this line.
    Code:
    INSERT INTO authors (id, lname, fname) VALUES (xx, 'Hack', 'Mr')
    You mean i have to have two insert statement, one for the authors and the other for the titles? or there is a way to make is one insert only?
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  15. #15
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Joining tables

    Quote Originally Posted by Simply Me
    You mean i have to have two insert statement, one for the authors and the other for the titles?
    Yes. One for each.

  16. #16

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Joining tables

    why am i getting syntax error here? the code is generated by Access.
    VB Code:
    1. strSQL = "SELECT tblAuthors.AuthorID, tblAuthors.AuthorLName, tblAuthors.AuthorFName, tblBooks.ISBN, tblBooks.Title, tblBooks.Subject, tblBooks.AccessionNum, tblBooks.CallNum, tblBooks.Edition, tblBooks.Copyright, tblBooks.Copies, tblBooks.Available" & _
    2.     "FROM tblBooks INNER JOIN (tblAuthors INNER JOIN tblBookAuthor ON tblAuthors.AuthorID = tblBookAuthor.AuthorID) ON tblBooks.ISBN = tblBookAuthor.ISBN"
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  17. #17
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Joining tables[with follow up]

    Because you are missing a space.

    In front of the FROM clause.

    Code:
    strSQL = "SELECT tblAuthors.AuthorID, tblAuthors.AuthorLName, tblAuthors.AuthorFName, tblBooks.ISBN, tblBooks.Title, tblBooks.Subject, tblBooks.AccessionNum, tblBooks.CallNum, tblBooks.Edition, tblBooks.Copyright, tblBooks.Copies, tblBooks.Available" & _
        " FROM tblBooks INNER JOIN (tblAuthors INNER JOIN tblBookAuthor ON tblAuthors.AuthorID = tblBookAuthor.AuthorID) ON tblBooks.ISBN = tblBookAuthor.ISBN"

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  18. #18

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Joining tables[Resolved]

    [QUOTE=szlamany]Because you are missing a space.

    In front of the FROM clause.

    oh my... just that space and i've been trying to figure it out for about 20 mins. now.... anyway thanks!
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

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