Results 1 to 8 of 8
  1. #1
    diegomarino is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    434

    Update query all fields

    Hi
    i know i can't use "*" in and update query, but is there a way to update every field with an update query?
    i have a code that iterate through every table so the query structure must be the same for every table even if they have different field number

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,283
    You cannot.?
    How is Access meant to know what updates what?
    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
    diegomarino is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    434
    i thought it was smarter. However in append query i can, is there a workaroud? or maybe i should run a piece of code for every field in a table counting the number of fields?

  4. #4
    RayMilhon is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,085

    ???

    Quote Originally Posted by diegomarino View Post
    i thought it was smarter. However in append query i can, is there a workaroud? or maybe i should run a piece of code for every field in a table counting the number of fields?
    So you're updating every field to contain the same data???? You need to explain your data structure and more detail on what you're trying to do. There's too much room for misinterpretation here.

  5. #5
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,165
    Agreed with Ray. It would probably be best explain what the end goal is.

    As for a work around you could build a vba function that loops through the fields in a given table and dynamically builds your query.

    https://stackoverflow.com/questions/...t-column-names

    Here is an example that you will need to edit to suit your needs.
    Code:
    Public Sub example()
    On Error GoTo ErrHandler
        Dim db As DAO.Database
        Dim rs As DAO.Recordset
        Dim fld As DAO.Field
        Dim qry As String
        Dim tbl As String
        Dim new_value As String
        
        'this is the name of the table in question
        tbl = "[TABLE_NAME_HERE]"
        
        Set db = CurrentDb
        Set rs = db.OpenRecordset(tbl)
        
        'build an update query string
        qry = "UPDATE " & tbl & " SET "
        For Each fld In rs.Fields
            new_value = "0" '<--- what ever you're trying to set the field value to. may need to consider data types, eg dates need to be surrounded by # or strings need quotation marks
            qry = qry & "[" & fld.Name & "] = " & new_value & ", "
        Next
        
        'remove the last comma and space from last loop
        qry = Left(qry, Len(qry) - 2)
        
        
        'close the recordset. we don't need it anymore
        rs.Close
        
        'print the string to the immediate debug window and make sure it looks correct (CTRL-G)
        Debug.Print qry
        
        'execute the qry (uncomment the following line when output from debug.print line above is built correctly)
        'db.Execute qry, dbFailOnError
        
        
    ExitHandler:
        'clean up
        Set fld = Nothing
        Set rs = Nothing
        Set db = Nothing
        Exit Sub
        
    ErrHandler:
        Debug.Print "*** Error #" & Err.Number & " - " & Err.Description
        MsgBox Err.Description, , "Error #" & Err.Number
        Resume ExitHandler
    End Sub

  6. #6
    diegomarino is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    434
    yes that's the one, i was building something similar but this will save me some time. thx

  7. #7
    diegomarino is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    434
    that's my solution, it append and update from excel files, maybe i'll add delete too but it's easy. I made just one test but it seems to work. Naturally if you have autonumber field there will be changes to make

    Code:
    Public Function ImportFromExcel() ' controlla se i file excel prodotti da powerapps sono pił aggiornati delle tabelle access e le aggiorna   Dim db As DAO.Database, Rst As DAO.Recordset, RstApp As DAO.Recordset, Tbf As DAO.TableDef, FldNum As Integer
       Dim TbEx As String, FldName As String, ColName As String
       
       Set db = CurrentDb
       'DoCmd.SetWarnings False
       For Each Tbf In db.TableDefs
          TbEx = Tbf.Name & "ex"
          'Debug.Print (Tbf.Name)
          If ifTableExists(TbEx) Then
             FldName = GetIndex(Tbf)
             DoCmd.RunSQL ("INSERT INTO [" & Tbf.Name & "] SELECT [" & TbEx & "].* FROM [" & TbEx & "] LEFT JOIN " & vbCrLf & _
                          "[" & Tbf.Name & "] ON [" & TbEx & "].[" & FldName & "] = [" & Tbf.Name & "].[" & FldName & "] " & vbCrLf & _
                          "WHERE ((([" & Tbf.Name & "].[" & FldName & "]) Is Null) AND (([" & TbEx & "].app)=""pw""));")
             DoCmd.RunSQL (Updatequery(Tbf))
          End If
       
       Next Tbf
       'DoCmd.SetWarnings True
    End Function
    Code:
    Public Function Updatequery(Tb As DAO.TableDef) As StringOn Error GoTo ErrHandler
        Dim db As DAO.Database
        Dim rs As DAO.Recordset
        Dim fld As DAO.Field
        Dim qry As String
        Dim Tbl As String
        Dim new_value As String
        Dim IdxName As String
    
    
        'this is the name of the table in question
        Tbl = Tb.Name
        Set db = CurrentDb
        
        Set rs = db.OpenRecordset(Tbl)
        IdxName = GetIndex(Tb)
        'build an update query string
        qry = "UPDATE [" & Tbl & "ex] INNER JOIN [" & Tbl & "] ON [" & Tbl & "ex].[" & IdxName & "] = [" & Tbl & "].[" & IdxName & "] SET"
        For Each fld In rs.Fields
            new_value = Tbl & "Ex" & ".[" & fld.Name & "]"
            qry = qry & "[" & Tbl & "].[" & fld.Name & "] = " & new_value & ", "
        Next
        
        'remove the last comma and space from last loop
        qry = Left(qry, Len(qry) - 2)
        qry = qry & " WHERE ((([" & Tbl & "ex].[Data/ora modifica])>[" & Tbl & "].[Data/ora modifica]));"
    
    
        Debug.Print (qry)
        Updatequery = qry
        
        'close the recordset. we don't need it anymore
        rs.Close
        
        'print the string to the immediate debug window and make sure it looks correct (CTRL-G)
    
    
        
        'execute the qry (uncomment the following line when output from debug.print line above is built correctly)
        'db.Execute qry, dbFailOnError
        
        
    ExitHandler:
        'clean up
        Set fld = Nothing
        Set rs = Nothing
        Set db = Nothing
        Exit Function
        
    ErrHandler:
        Debug.Print "*** Error #" & Err.Number & " - " & Err.Description
        MsgBox Err.Description, , "Error #" & Err.Number
        Resume ExitHandler
    End Function
    Code:
    Public Function GetIndex(Tbf As DAO.TableDef) As String   
       Dim Idx As Index
       For Each Idx In Tbf.Indexes
          On Error Resume Next
          If Idx.Primary Then GetIndex = Replace(Idx.Fields, "+", "")
       Next Idx
    End Function

  8. #8
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,165
    Thank you for sharing!

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

Similar Threads

  1. Replies: 2
    Last Post: 11-27-2017, 08:59 PM
  2. Replies: 1
    Last Post: 06-11-2016, 05:46 AM
  3. Replies: 3
    Last Post: 05-27-2014, 09:31 AM
  4. Replies: 2
    Last Post: 08-30-2012, 07:59 AM
  5. UPDATE query with many fields
    By Deutz in forum Queries
    Replies: 7
    Last Post: 08-23-2011, 05:53 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