Results 1 to 5 of 5
  1. #1
    markcrobinson is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Location
    New Hampshire, US
    Posts
    31

    Relationships in a SQL statement

    I have 4 tables with a common field, ItemNumber.


    Set db = CurrentDb
    Set rs1 = db.OpenRecordset("select * from Items;")
    Set rs2 = db.OpenRecordset("select * from Stock;")
    Set rs3 = db.OpenRecordset("select * from OpenPOMARS;")
    Set rs4 = db.OpenRecordset("select * from RequireMARS;")

    I want to pull the first ItemNumber from Items and look up and combine data from each of the other three databases.
    There are a lot of calculations I'm performing on the data -- too many for a query. So I'm writing a vb program.
    The sloppy way is to open up all 4 databases and do Lookups.

    The joins are all Left joins. I need ALL the records in Items and whatever records exist in the other tables. When I create a query to cheat of figuring out the SQL statement I get:

    SELECT Items.itemId, Items.use, Stock.qStk, OpenPOMARS.*, RequireMARS.*" & _
    "FROM ((Items LEFT JOIN Stock ON Items.itemId = Stock.itemId)" & _
    "LEFT JOIN OpenPOMARS ON Items.itemId = OpenPOMARS.ItemNumber)" & _
    " LEFT JOIN RequireMARS ON Items.itemId = RequireMARS.ItemNumber;"

    but when I set it to a variable and do
    docmd.runsql variable I get
    "A RunSQL action requires a argument consisting of an SQL statement."

    My Questions:
    1 - Whats wrong with my SQL statement? It works in a Query but not in a program.
    2 - And I going about this the right way?

    Thank You!

  2. #2
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Please try:
    Code:
    Dim sSQL as String
    sSQL="SELECT Items.itemId, Items.use, Stock.qStk, OpenPOMARS.*, RequireMARS.*" & _         " FROM ((Items LEFT JOIN Stock ON Items.itemId = Stock.itemId)" & _
             " LEFT JOIN OpenPOMARS ON Items.itemId = OpenPOMARS.ItemNumber)" & _
             " LEFT JOIN RequireMARS ON Items.itemId = RequireMARS.ItemNumber;
    Set db = CurrentDb
    Set rs1 = db.OpenRecordset(sSQL)   'default is dbOpenDynaset
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    The problem is that RunSQL only works with action queries (append, update, delete). You could open a recordset on the SQL or set a record source or row source to, or...
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    markcrobinson is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Location
    New Hampshire, US
    Posts
    31

    Thank you!

    Quote Originally Posted by Gicu View Post
    Please try:
    Code:
    Dim sSQL as String
    sSQL="SELECT Items.itemId, Items.use, Stock.qStk, OpenPOMARS.*, RequireMARS.*" & _         " FROM ((Items LEFT JOIN Stock ON Items.itemId = Stock.itemId)" & _
             " LEFT JOIN OpenPOMARS ON Items.itemId = OpenPOMARS.ItemNumber)" & _
             " LEFT JOIN RequireMARS ON Items.itemId = RequireMARS.ItemNumber;
    Set db = CurrentDb
    Set rs1 = db.OpenRecordset(sSQL)   'default is dbOpenDynaset
    You make it look soooo easy!

  5. #5
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    I just wanted to point out that a problem with your query in the original post was that you didn't have any spacing between word in your string concatenation. When I'm pragmatically generating a query like this the first thing I do after building the string is print it out to the debug window to make sure the SQL I generated looks valid.

    Another easy way to generate SQL would be to just build your query in the query designer and copy the SQL it generates. The Allen Browne website has an example form and code that will format SQL to a VBA string. http://allenbrowne.com/ser-71.html

    Oh, and I've been using this formatter a lot lately, it has an output mode "VB" that will build a string for you. http://www.dpriver.com/pp/sqlformat.htm

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 7
    Last Post: 08-05-2016, 11:53 AM
  2. Replies: 2
    Last Post: 07-27-2016, 10:25 AM
  3. Replies: 11
    Last Post: 04-29-2015, 01:38 PM
  4. Explicit Relationships and Implicit Relationships
    By Dazza666 in forum Database Design
    Replies: 2
    Last Post: 07-17-2013, 02:11 AM
  5. Replies: 7
    Last Post: 08-17-2011, 01:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums