Results 1 to 13 of 13
  1. #1
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371

    Append Statement Fields

    Hi Guy's, i am trying to run an Append statement, this is teh easy part

    Some Field names don't match in the destination table, is there an easy method to change my code here ?



    I can of course use rs update or list go through all field names in the destination table, just would be rather la lenghy stsment in stead of kept short and sweet

    the error i am getting is can't find field name hence the above information

    Code:
    sSQL4 = "INSERT INTO tblHold ( Status ) " _        
    & "SELECT tblShipmentIn.*, tblShipmentIn.Status " _
            & "From tblShipmentIn " _
            & "WHERE (((tblShipmentIn.Status)= '" & sHold & "'));"
                  
             DoCmd.SetWarnings False
                DoCmd.RunSQL sSQL4
            DoCmd.SetWarnings True

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,574
    I tell you this all the time...
    DEbug.Print your sql and see what you have , NOT what you think you have.

    How can you insert into one field from all fields in another table?
    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

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    Dave,

    WHAT exactly are you trying to achieve in plain English? As Welshgasman said - your SQL syntax is wrong.
    It says
    Code:
    INSERT tblShipmentIn.*, tblShipmentIn.Status into field Status
    
     a bunch of values into 1 field?????
    You might want to show readers the structure of tblHold and a few records from tblShipmentIn

  4. #4
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371
    Thanks guys. select (append) all to another table where status has a criteria (sHold), the destination table some fields are not the same name.

    @WGM, i did debug.print everything, commented out because after testing some statements

    Code:
    'Debug.Print strDay & " " & dtShp & vbCrLf & "New Records In: " & iRecsAdded & vbCrLf & "Records To Update: "; iRecsUpdate & _            vbCrLf & "Delete " & iRecsAdded & vbCrLf & _
                "Append " & iRecsUpdate & " From Shipment In"
    i didnt think debug.Print was relevant as it wasn't record check i was doing, it's field name differences i was trying to rule out in the destination table

    @Orange, thank you, i was trying to update field names that match source fields and destination fields instead of the command finding field names that don't match

    Hope i have put this across correctly |

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,574
    Fieldnames do not have to match, but MUST be in the same order.
    I meant show the debug.print of the sql statement.
    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

  6. #6
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371
    Thanks WGM, yes understood, maybe better for me to update via looping through recordset 1 with a criteria to add data the the relevant fields in recordset 2

    I guess this way, im only updating the required fields.

    Thanks though for both yours and orange input

  7. #7
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371
    Thanks WGM unserstood, maybe if i ceate a query on sorce table, add dest table then change field source, i would think i get expression where fields don't exist then remove them from the query, once i have the correct fields in the select query then use the SQL as an append

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,574
    Why do you feel you need to automate this?
    We have one member who wants to reame first field in tables??

    All stuff that begs the question .....WHY?
    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

  9. #9
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    Dave,

    I don't understand your requirement. Can you mock up an example showing:
    -this is before
    and
    -this I what I need in the output (tblHold).

  10. #10
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371
    Hello Oragne, thank you, i have worked out a solujtion, instead of selecting * from Source Table and trying to append to Dest table, just created an append query.

    pulled in all fields

    removed fields i didn't need to update to minimize

    copeid the SQL and run rhe SQL code

    sorry i haven't replied, we have a family bereavment that we are sorting out so responses slow

    I will makr as solved and ratehr than posting the statement, the fix was:

    instead of Select *, added all fields then removed the fields not reuired for append

    Thanks again to you and WGM

  11. #11
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,574
    Sorry to hear of your loss.
    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

  12. #12
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371
    Thank you WGM, totally aprpeciate your kind comment

    yeh as above, created query, removed field that don't require update and just copied SQL and run it in the code, works fine, as per you suggest though run the sql into immerdiate window, use that a lot now then exit sub to halt and see results.

    ideal to stop me messing real data up

  13. #13
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,574
    I tend to run any query as a select, to see what I get is what I want, then convert to update/append/delete query in qbe window.
    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

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

Similar Threads

  1. Append error statement workaround
    By Roncc in forum Access
    Replies: 6
    Last Post: 01-17-2018, 01:20 PM
  2. Replies: 7
    Last Post: 06-28-2017, 04:33 PM
  3. Append SQL Statement expression
    By Jgk in forum Queries
    Replies: 5
    Last Post: 05-16-2014, 05:56 PM
  4. Run Append Query From If Statement
    By scoe in forum Queries
    Replies: 2
    Last Post: 04-02-2014, 07:41 AM
  5. Append Query with IIF statement
    By Cheshire101 in forum Queries
    Replies: 5
    Last Post: 09-16-2010, 12:51 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