Results 1 to 13 of 13
  1. #1
    usertest is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Oct 2023
    Posts
    35

    Question Inserting data using PTQ into local access table

    Hi,


    Below is the code

    Code:
    Public Sub GET_LIST()
    Dim DC As New DataConnection
    Dim r As New ADODB.Recordset
    Dim r1 As New ADODB.Recordset
    Dim strSQL As String
    
    
        DoCmd.SetWarnings False
        DoCmd.RunSQL "Delete * From LISTTABLE;"
        DoCmd.SetWarnings True
    
    
    
    
    strSQL = "SELECT * FROM LISTTABLE;"
    r1.Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
    
    
    strSQL = "SELECT DISTINCT A.MGR_ID, B.STATUS, B.MASTER, B.NAME AS REP_NAME, C.CODE, B.STATE FROM SQLTABLE AS A LEFT JOIN " & _
             "SQLTABLE AS B on A.MGR_ID = B.ID LEFT JOIN SQLTABLE AS C ON B.MASTER = C.ID WHERE C.SUBSIDIARY = '001' AND C.STATUS <> '99';"
          
    r.Open strSQL, DC.REP, adOpenKeyset, adLockOptimistic
    
    
    Do While r.EOF = False
        r1.AddNew
            r1("ID") = r("MGR_ID")
            r1("MGR_ID") = r("MGR_ID")
            r1("STATUS") = r("STATUS")
            r1("REP_NAME") = r("REP_NAME")
            r1("CODE") = r("CODE")
            r1("MASTER") = r("MASTER")
            r1("STATE") = r("STATE")
            r1.Update
        r.MoveNext
        Loop
        r.Close
    
    
        Set DC = Nothing
        Set r = Nothing
        Set r1 = Nothing
    
    
    End Sub

    In Summary, I delete all the records from the local access table (r1), and then I add the data from SQL table(r) to Access table(r1) based on conditions.
    Now, I would like to convert the below sql code into a PTQ and I think I can achieve that easily.

    Code:
    strSQL = "SELECT DISTINCT A.MGR_ID, B.STATUS, B.MASTER, B.NAME AS REP_NAME, C.CODE, B.STATE FROM SQLTABLE AS A LEFT JOIN " & _
             "SQLTABLE AS B on A.MGR_ID = B.ID LEFT JOIN SQLTABLE AS C ON B.MASTER = C.ID WHERE C.SUBSIDIARY = '001' AND C.STATUS <> '99';"

    Once I do that, How do I code so that the condition of r1(id) = r(mgr_id) and r1(mgr_id) = r(mgr_id) that is part of the logic?

    I'm stuck as to how do I code that part of the logic?

    The code is working fine and no issues in any part of it. I just want to improve the performance and optimize it.

    Thanks!

  2. #2
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    You don't need the loop or the recordsets you could simply run an update query?
    This was written on the hoof so test it for syntax first:

    Something like
    Code:
    UPDATE LISTTABLE 
    SET 
            r1.ID         = r.MGR_ID
            r1.MGR_ID     = r.MGR_ID
            r1.STATUS     = r.STATUS
            r1.REP_NAME     = r.REP_NAME
            r1.CODE        = r.CODE
            r1.MASTER     = r.MASTER
            r1.STATE       = r.STATE
    FROM Listtable r1
    Join 
    (    SELECT DISTINCT A.MGR_ID, B.STATUS, B.MASTER, B.NAME AS REP_NAME, C.CODE, B.STATE 
        FROM SQLTABLE AS A LEFT JOIN  _
        SQLTABLE AS B on A.MGR_ID = B.ID LEFT JOIN SQLTABLE AS C ON B.MASTER = C.ID 
        WHERE C.SUBSIDIARY = '001' AND C.STATUS <> '99'
    ) AS r 
    ON r.mgr_ID On r1.id ANd r1.MGR_ID = r.mgr_ID
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    usertest is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Oct 2023
    Posts
    35
    Where do I put that query?
    Cause there is access table and SQL table being used in the query.
    I would have to reference the database connection for my SQL table?


    Quote Originally Posted by Minty View Post
    You don't need the loop or the recordsets you could simply run an update query?
    This was written on the hoof so test it for syntax first:

    Something like
    Code:
    UPDATE LISTTABLE 
    SET 
            r1.ID         = r.MGR_ID
            r1.MGR_ID     = r.MGR_ID
            r1.STATUS     = r.STATUS
            r1.REP_NAME     = r.REP_NAME
            r1.CODE        = r.CODE
            r1.MASTER     = r.MASTER
            r1.STATE       = r.STATE
    FROM Listtable r1
    Join 
    (    SELECT DISTINCT A.MGR_ID, B.STATUS, B.MASTER, B.NAME AS REP_NAME, C.CODE, B.STATE 
        FROM SQLTABLE AS A LEFT JOIN  _
        SQLTABLE AS B on A.MGR_ID = B.ID LEFT JOIN SQLTABLE AS C ON B.MASTER = C.ID 
        WHERE C.SUBSIDIARY = '001' AND C.STATUS <> '99'
    ) AS r 
    ON r.mgr_ID On r1.id ANd r1.MGR_ID = r.mgr_ID

  4. #4
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Just link to the SQL table, then use it in your query.
    You can't pass through the Access table it will have to be done in Access, so you may as well use a update query which will be 100 times faster than a Loop

    Avoid RBAR (Row By Agonising Row) at all costs.

    Actually, thinking about it, if your local table is pulled from a SQL Table then just run the whole thing as a Pass through.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  5. #5
    usertest is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Oct 2023
    Posts
    35
    Hi,
    When I tried to put the query as PTQ, it shows an error ODBC error - call failed. Invalid object name LISTTABLE.



    Quote Originally Posted by Minty View Post
    Just link to the SQL table, then use it in your query.
    You can't pass through the Access table it will have to be done in Access, so you may as well use a update query which will be 100 times faster than a Loop

    Avoid RBAR (Row By Agonising Row) at all costs.

    Actually, thinking about it, if your local table is pulled from a SQL Table then just run the whole thing as a Pass through.

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  7. #7
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    You aren't reading the answers very well.
    Quote Originally Posted by Minty View Post
    Just link to the SQL table, then use it in your query.
    You can't pass through the Access table it will have to be done in Access, so you may as well use a update query which will be 100 times faster than a Loop
    .
    If ListTable doesn't exist in your SQL database of course you can't pass it through to the server.

    If it does you can.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  8. #8
    zpy2 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2023
    Posts
    4
    update test1
    set test1.name=test2.name,test1.age=test2.age
    from test1
    inner join test2
    on test1.id=test2.id

    ms sql server ?

  9. #9
    usertest is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Oct 2023
    Posts
    35
    sorry for the late reply,
    But LISTTABLE is just a local access table and does not exist on SQL database. So agreed, I cant pass that as PTQ.


    Quote Originally Posted by Minty View Post
    You aren't reading the answers very well.


    If ListTable doesn't exist in your SQL database of course you can't pass it through to the server.

    If it does you can.

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I feel like this has already been given in one of your various threads, but your query would be:

    INSERT INTO LISTTABLE(Field1, Field2, ...)
    SELECT Field1, Field2, ...
    FROM PTQ
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,044
    Quote Originally Posted by Minty View Post
    You can't pass through the Access table it will have to be done in Access,
    actually, it can be done in SQL if you use SSIS

  12. #12
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Quote Originally Posted by NoellaG View Post
    actually, it can be done in SQL if you use SSIS
    Well, yes it can, but I doubt the OP would want to that in the middle of their normal form operations.

    He's trying to make an efficiency improvement, when a simple query based solution locally in Access would be massively faster than looping through a recordset.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  13. #13
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,044
    You're right, and as a SQL DBA I love you for taking the OP away from RBAR operations. Thank you

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

Similar Threads

  1. Copy data from SQL to local Access Table
    By usertest in forum Programming
    Replies: 4
    Last Post: 10-16-2023, 01:33 PM
  2. Replies: 16
    Last Post: 04-02-2020, 04:49 PM
  3. Replies: 3
    Last Post: 06-07-2018, 07:08 AM
  4. Replies: 2
    Last Post: 03-30-2018, 11:33 AM
  5. Inserting Data to Access Table (Local) from MySQL table (Net)
    By gambit1430 in forum Import/Export Data
    Replies: 2
    Last Post: 09-26-2014, 01:34 AM

Tags for this Thread

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