Results 1 to 13 of 13
  1. #1
    VAer is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Sep 2016
    Location
    USA
    Posts
    163

    Inner Join Statement (For VBA Code) (A table & a query)

    I have two tables: Table_abc Table_xyz
    XyzQuery is a query from Table_xyz
    I would like to set up AbcQuery which needs inner join XyzQuery


    How to correct my Inner Joint statement?

    Thanks.





    Code:
    XyzQuery = "Select Xyz_field1, Xyz_field2 from Table_xyz"
    
    
    AbcQuery = "Select Abc_field1, Abc_field2 From Table_abc Where Abc_field1 = " & _
            TextBoxfield1.Value & " Inner Join " & XyzQuery & " On Table_abc.Abc_field1 = XyzQuery.Xyz_field1 ORDER BY Abc_field1 ASC, Abc_field2 DESC"
    Attached Thumbnails Attached Thumbnails Capture.JPG  

  2. #2
    kagoodwin13 is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Feb 2012
    Posts
    181
    The way this is written is confusing WHERE with INNER JOIN. They are more mutually exclusive, not meant to be combined together.

    Also, you need an alias (AS) for your XyzQuery to use it.

    This should be debugged as SQL first, and VBA second.

    Right now it is reading as:
    Code:
    SELECT Abcfield1, Abcfield2
    FROM Table_abc
    WHERE Abc_field1 = Textboxfield1.Value
    INNER JOIN
      SELECT Xyzfield1, Xyzfield2
      FROM Table_xyz
      ON Table_abc.Abcfield1 = Xyzquery.Xyz_field1
    ORDER BY Abc_field1 ASC, Abc_field2 DESC
    It would be a valid SQL query as:
    Code:
    SELECT Abcfield1, Abcfield2
    FROM Table_abc
    INNER JOIN
      (SELECT Xyzfield1, Xyzfield2
      FROM Table_xyz) AS XyzQuery
      ON Table_abc.Abcfield1 = Xyzquery.Xyz_field1
    WHERE Abc_field1 = Textboxfield1.Value
    ORDER BY Abc_field1 ASC, Abc_field2 DESC

  3. #3
    VAer is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Sep 2016
    Location
    USA
    Posts
    163
    Quote Originally Posted by kagoodwin13 View Post
    The w
    [/CODE]

    It would be a vASC, Abc_field2 DESC
    [/CODE]
    So I should also put WHERE after INNER JOIN ?

    Thanks.

  4. #4
    kagoodwin13 is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Feb 2012
    Posts
    181
    Quote Originally Posted by VAer View Post
    So I should also put WHERE after INNER JOIN ?

    Thanks.
    Yes. INNER JOIN always follows a FROM statement. https://www.w3schools.com/sql/sql_join_inner.asp

  5. #5
    VAer is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Sep 2016
    Location
    USA
    Posts
    163
    Quote Originally Posted by kagoodwin13 View Post
    Yes. INNER JOIN always follows a FROM statement. https://www.w3schools.com/sql/sql_join_inner.asp
    How to write (SELECT Xyzfield1, Xyzfield2 FROM Table_xyz) AS XyzQuery in VBA? The code can understand one SELECT inside another SELECT? The code can understand left parenthesis and right parenthesis?

    Thanks.

  6. #6
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,405
    Code:
    How to write (SELECT Xyzfield1, Xyzfield2 FROM Table_xyz) AS XyzQuery in VBA?
    Are you asking how to create a named query from an SQL statement?
    Or
    Asking about SELECT statements within SELECT queries? (Nested queries or subqueries)

  7. #7
    VAer is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Sep 2016
    Location
    USA
    Posts
    163
    Quote Originally Posted by davegri View Post
    Code:
    How to write (SELECT Xyzfield1, Xyzfield2 FROM Table_xyz) AS XyzQuery in VBA?
    Are you asking how to create a named query)
    I am asking how to correctly write VBA for AbcQuery? How to correctly write SQL in the AbcQuery statement (Initial post)?

  8. #8
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    I have two tables: Table_abc Table_xyz
    XyzQuery is a query from Table_xyz
    I would like to set up AbcQuery which needs inner join XyzQuery
    What exactly are you trying to accomplish - in plain English?
    It would help if you provide
    a) some sample data and
    b) a sample of the output you expect from that data.

    You have 2 tables that are related. Where does Textboxfield1 fit in this?

  9. #9
    VAer is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Sep 2016
    Location
    USA
    Posts
    163
    Quote Originally Posted by orange View Post
    What exactly are you trying to accomplish - in plain English?
    Textboxfield1 fit in this?
    1) Copy XyzQuery to one Excel Sheet
    2) Copy AbcQuery to another Excel Sheet
    3) TextBoxfield1.Value is an input value from Userform textbox. It is not really important for this question. The only purpose is to make code dynamic for users. You may ignore this part.

    Maybe I can modify the sample code as below:

    Code:
    XyzQuery = "Select Xyz_field1, Xyz_field2 from Table_xyz"
    
    AbcQuery = "Select Abc_field1, Abc_field2 From Table_abc Inner Join " & XyzQuery & " On Table_abc.Abc_field1 = XyzQuery.Xyz_field1 Where Abc_field1 = " & TextBoxfield1.Value & " ORDER BY Abc_field1 ASC, Abc_field2 DESC"
    



  10. #10
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    ??? We're 9 posts in and this is the first mention of Excel???

    I don't understand what you're trying to do. Your topic/subject Inner Join... has nothing to do with Excel.
    Perhaps someone else will understand your request and respond.

  11. #11
    VAer is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Sep 2016
    Location
    USA
    Posts
    163
    Quote Originally Posted by orange View Post
    Wha?
    I made up my question, which is similar to my issue. Maybe I can make up sample data too.

    Actually, this is NOT good example, I should make another new field for Textbox value, maybe TextBoxField6

    Never mind, thanks for reading my post.
    Attached Thumbnails Attached Thumbnails Capture.JPG  

  12. #12
    VAer is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Sep 2016
    Location
    USA
    Posts
    163
    Quote Originally Posted by orange View Post
    ??? We're 9 posts in and this is the first mention of Excel???
    Title use the word "For VBA code", write SQL statement inside VBA program.

  13. #13
    VAer is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Sep 2016
    Location
    USA
    Posts
    163
    Never mind. I can just ignore Inner Join statement. I was trying to ask a question, but the example is not good enough.

    I can just ignore Inner Statement, and write a VBA loop to delete any unwanted records.

    Let me mark the post as solved.

    Thanks.

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

Similar Threads

  1. Join Statement with two field relationship
    By dhogan444 in forum Queries
    Replies: 2
    Last Post: 08-09-2012, 08:30 AM
  2. when (select statement within inner join should be used)?
    By learning_graccess in forum Programming
    Replies: 1
    Last Post: 05-17-2012, 01:54 PM
  3. Query Join, Expression, or Code
    By catguy in forum Queries
    Replies: 1
    Last Post: 08-17-2011, 10:52 PM
  4. Update statement with linked table join
    By Guigui in forum Queries
    Replies: 6
    Last Post: 09-17-2010, 04:47 AM
  5. SQL JOIN statement
    By seen in forum Access
    Replies: 10
    Last Post: 01-15-2010, 05:34 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