Results 1 to 4 of 4
  1. #1
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    365

    Update to another DB

    I have two databases each with a table of identical structure.
    From DB1 tblSource I want to update or add a record in DB2 Table1
    Code:
      sql = "SELECT * FROM [;DATABASE=" & ExtDB & "].Table1 Where Prefix = " & Chr$(34) & ThisPrefix & Chr$(34) & ";"
        Set r = CurrentDb.OpenRecordset(sql)
        Set e = CurrentDb.OpenRecordset("Select * from tblSource;")
        If r.RecordCount = 0 Then
                r.AddNew
            Else
                r.Edit
        End If
        For i = 1 To e.Fields.Count - 1  'start at 1 to skip idfield
            r(i) = e(i)
         Next
         r.Update
         r.Close
         e.Close
    But it seems overkill to For next loop thru all fields? Might it be possible to use an Update query ?

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    You understand an UPDATE SQL uses SET clause where you have to specify field pairs? How many fields? Loop may be simpler code.

    If you set a link to Table1, could possibly build an UPSERT SQL to accomplish both INSERT and UPDATE - again, uses SET clause. Review https://stackoverflow.com/questions/...crosoft-access
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    365
    Thank you June, I have something working but it's big having to specify each of the 20 fieldnames. You can't use * ? That may fail anyway if then can't exclude field0 (id).

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    No, cannot use wildcard in UPDATE. Exactly how would you envision anyway?

    So why not stick with the loop?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Replies: 9
    Last Post: 08-08-2023, 09:48 PM
  2. Replies: 4
    Last Post: 03-09-2022, 11:28 AM
  3. Replies: 2
    Last Post: 06-21-2018, 03:16 AM
  4. Replies: 7
    Last Post: 08-26-2013, 06:05 AM
  5. Replies: 1
    Last Post: 01-22-2011, 12:23 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