Wrox Programmer Forums
|
BOOK: Access 2010 VBA Programmer's Reference
This is the forum to discuss the Wrox book Access 2010 Programmer's Reference by Teresa Hennig, Rob Cooper, Geoffrey L. Griffith, Jerry Dennison; ISBN: 978-0-470-59166-6
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Access 2010 VBA Programmer's Reference section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 20th, 2012, 07:43 PM
Registered User
 
Join Date: Apr 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Issue with syntax in FROM Clause

Hello all,

I have created a SQL statement and have changed it a bit to work with VBA in word via a macro. I've been having difficulty finding a good reference which explains how the syntax should be set in a FROM clause inner joining data from more than 2 tables. I have the following code so far:

strStudents = "SELECT tblStudentNo, fldLastName, fldDepartmentName, fldTelephone, fldClassName "

strStudents = strStudents & "FROM [tblDepartments] INNER JOIN " & "[tblStudents] INNER JOIN tblClasses INNER JOIN tblStudentsAndClasses ON "

strStudents = strStudents & "[tblStudents].fldStudentNo = tblStudentsAndClasses.fldStudentNo ON "

strStudents = strStudents & "[tblDepartments].fldDepartmentNo = tblStudents.fldDeptNo "

strStudents = strStudents & "WHERE fldClassName= '" & strVar & "' ORBER BY fldLastName"


I have been unable to find a good site that shows what the syntax should typically look like in this case. Can someone please point me in the right direction?

Cheers,
Heisswa
 
Old April 21st, 2012, 05:59 AM
gjgriffith's Avatar
Wrox Author
 
Join Date: Jul 2009
Posts: 110
Thanks: 5
Thanked 14 Times in 14 Posts
Default Have you tried using the Access Query Builder?

Hello Heisswa,

Thanks you for posting in the "Microsoft Access 2010 Programmer's Reference" book forum. I had a chance to look over your question here and even though it is not really about this book, I'll see if I can help ! Specifically you asked:

Quote:
I've been having difficulty finding a good reference which explains how the syntax should be set in a FROM clause inner joining data from more than 2 tables.
...
I have been unable to find a good site that shows what the syntax should typically look like in this case. Can someone please point me in the right direction?
Hmm, I had a look at your query here...it seems very complex and looks like you have at least 4 tables with joins, even though they aren't all listed here, which is probably why you are having a problem! In this case, what I like to do when I have a really complex query, is just create the query in Access using Query Design view mode and get it just the way I want it to work. Then, once I have the query correct, I switch to SQL view mode, then copy, paste and the SQL code generated by Access, and fix up for VBA. Does that make sense?

Otherwise, I'm not sure about a website to provide exactly what you are looking for. One book I might recommend would be "Beginning SQL", which you can see here:
http://www.wrox.com/WileyCDA/WroxTit...764577328.html

And one site I would recommend would be W3Schools, they have a GREAT SQL tutorial at:
http://www.w3schools.com/sql/default.asp

But really, in this case, it would probably just be simplest to design the Query in Access using Design view mode and then switch to SQL view mode to get the specific SQL that you need! Anyway, I hope this helps and hopefully you get this problem all worked out using Access!

Regards,
__________________
Geoffrey L. Griffith
http://www.ImagineThought.com

Wrox Author of:
Microsoft Access 2010 24-Hour Trainer
Access 2010 Programmer's Reference
Access 2007 VBA Programmer's Reference

*** Please click the THANKS button (to the right) if this post helped you! *** ---------------------------------------------------------------------------------------------------------->

Last edited by gjgriffith; April 21st, 2012 at 06:04 AM..
 
Old April 21st, 2012, 07:15 AM
Authorized User
 
Join Date: Oct 2010
Posts: 64
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Hi Heisswa,

You have the syntax wrong for each INNER JOIN as it should be followed by the ON clause. Also, you only have 2 ON clauses when there should be 3 as you have 4 tables! I think you missed the join condition from tblStudentsAndClasses to tblClasses.

Using alternative JOIN syntax you could have your query similar to the following:
SELECT s.tblStudentNo, s.fldLastName, d.fldDepartmentName, d.fldTelephone, c.fldClassName
FROM tblDepartments d, tblStudents s, tblClasses c, tblStudentsAndClasses sc
WHERE c.fldClassName = ?
AND s.fldStudentNo = sc.fldStudentNo
AND sc.fldClassId = c.fldClassId
AND s.fldDeptNo = d.fldDepartmentNo
ORDER BY fldLastName

The ? is just the parameter, which you would replace with your variable strVar when building your SQL string. I have also assumed that the primary key for the tblClasses table is fldClassId and the foreign key is likewise in tblStudentsAndClasses. Also, I have used aliases to simplify the SQL. I have also assumed the attribute fldTelephone is from the tblDepartments table.

HTH a little bit!

Malc.
 
Old April 21st, 2012, 07:11 PM
Registered User
 
Join Date: Apr 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey guys, thanks for the reply. I already used Access to build a query in design mode. I got the following from Access 2010:

Code:
SELECT tblStudents.fldStudentNo, tblStudents.fldFirstName, tblStudents.fldLastName, tblDepartments.fldDepartmentName, tblStudents.fldTelephone, tblClasses.fldClassName

FROM tblDepartments INNER JOIN (tblStudents INNER JOIN (tblClasses INNER JOIN tblStudentsAndClasses ON tblClasses.fldClassNo = tblStudentsAndClasses.fldClassNo) ON tblStudents.fldStudentNo = tblStudentsAndClasses.fldStudentNo) ON tblDepartments.fldDepartmentNo = tblStudents.fldDeptNo

WHERE (((tblClasses.fldClassName) = "Windows XP Level 1"))

ORDER BY tblStudents.fldLastName;
I will take another look at what I already have and make a couple adjustments per your suggestion, Malcolm.

Heisswa





Similar Threads
Thread Thread Starter Forum Replies Last Post
Syntax error in FROM clause. jaz061459 Pro VB Databases 1 May 15th, 2010 12:32 AM
Syntax error in FROM clause. jaz061459 VB Databases Basics 1 May 15th, 2010 12:30 AM
Syntax error in FROM clause: asp jpindi ASP.NET 1.0 and 1.1 Basics 6 July 16th, 2008 12:30 AM
syntax for where clause with like operator Yasho VB.NET 2002/2003 Basics 2 June 11th, 2007 08:34 AM
Syntax error in query. Incomplete query clause. dispickle ADO.NET 3 April 16th, 2004 01:04 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.