|
-
Apr 11th, 2005, 08:28 AM
#1
Thread Starter
PowerPoster
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.
-
Apr 11th, 2005, 08:36 AM
#2
Re: Joining tables
What field links the two tables? Is there an Id field in each?
-
Apr 11th, 2005, 08:42 AM
#3
Thread Starter
PowerPoster
Re: Joining tables
 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?
-
Apr 11th, 2005, 08:46 AM
#4
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.
-
Apr 11th, 2005, 08:47 AM
#5
Frenzied Member
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...)
-
Apr 11th, 2005, 08:52 AM
#6
Thread Starter
PowerPoster
Re: Joining tables
Ok. I have AuID now in both tables. Any code snippet pls?
-
Apr 11th, 2005, 08:54 AM
#7
Frenzied Member
Re: Joining tables
Code:
SELECT AuLName, AUFName, Title, Subject, ISBN
FROM Authors a
INNER JOIN Titles t
ON a.AuID = t.AuID
-
Apr 11th, 2005, 09:06 AM
#8
Thread Starter
PowerPoster
Re: Joining tables
 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?
-
Apr 11th, 2005, 09:11 AM
#9
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:
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.
-
Apr 11th, 2005, 09:11 AM
#10
Frenzied Member
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.
-
Apr 11th, 2005, 09:18 AM
#11
Thread Starter
PowerPoster
Re: Joining tables
Thanks to both of you. I'll try it now.
-
Apr 11th, 2005, 09:36 AM
#12
Thread Starter
PowerPoster
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?
-
Apr 11th, 2005, 09:38 AM
#13
Re: Joining tables
 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')
-
Apr 11th, 2005, 09:41 AM
#14
Thread Starter
PowerPoster
Re: Joining tables
 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?
-
Apr 11th, 2005, 09:54 AM
#15
Re: Joining tables
 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.
-
Apr 12th, 2005, 09:20 AM
#16
Thread Starter
PowerPoster
Re: Joining tables
why am i getting syntax error here? the code is generated by Access.
VB 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"
-
Apr 12th, 2005, 09:24 AM
#17
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"
-
Apr 12th, 2005, 09:33 AM
#18
Thread Starter
PowerPoster
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!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|