Results 1 to 9 of 9
  1. #1
    yrstruly is offline Competent Performer
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2024
    Posts
    122

    Add =Now() to VBA

    This VBA adds a modified date/time column to my tables, which is correct. It does not set the auto value, anyway i can do this with a code instead of going through all my tables and manually do it?


    Code:
    Sub AddModifiedDateToAllUserTables()    Dim db As Object
        Dim tdf As Object
        Dim fld As Object
        Dim fieldExists As Boolean
        Dim tableName As String
    
    
        Set db = CurrentDb
    
    
        For Each tdf In db.TableDefs
            tableName = tdf.Name
    
    
            ' ✅ Skip system and temporary tables
            If Not (Left(tableName, 4) = "MSys" Or Left(tableName, 1) = "~") Then
                
                fieldExists = False
                
                ' ✅ Check if ModifiedDate column already exists
                For Each fld In tdf.Fields
                    If fld.Name = "ModifiedDate" Then
                        fieldExists = True
                        Exit For
                    End If
                Next fld
                
                ' ✅ Add column only if not already present
                If Not fieldExists Then
                    ' Add field with DEFAULT Now()
                    db.Execute "ALTER TABLE [" & tableName & "] ADD COLUMN ModifiedDate DATETIME DEFAULT Now();", 128
                    Debug.Print "✅ Added ModifiedDate with DEFAULT Now() to: " & tableName
                Else
                    Debug.Print "ℹ️ Already has ModifiedDate: " & tableName
                End If
    
    
            Else
                Debug.Print "⛔ Skipped system/temporary table: " & tableName
            End If
        Next tdf
    
    
        MsgBox "ModifiedDate update complete.", vbInformation
    End Sub

  2. #2
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,927
    What is the question? To update existing records with now()?

  3. #3
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    I believe so...

    Add the following line after fieldExists = True and before Exit For

    db.Execute "UPDATE [
    " & tableName & "] SET ModifiedDate = Now();

    This will update the field for all records in that table. Is that what you want?
    If not add a WHERE filter to the update query to limit the records affected.
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  4. #4
    yrstruly is offline Competent Performer
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2024
    Posts
    122
    The purpose is to have a time date olumn, whih capture time stamp every time this row is modified.

  5. #5
    Minty is offline VIP
    Windows 11 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    The correct syntax for adding the Default value is

    Code:
    CurrentDb.TableDefs("YourTableName").Fields("ModifiedDate").DefaultValue = "Now()"
    From here: https://stackoverflow.com/questions/27608708/change-default-value-of-field-through-vba
    That assumes the field is already in place.

    Another set of descriptions is here : https://stackoverflow.com/questions/...g-in-ms-access
    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 ↓↓

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,550
    Quote Originally Posted by Minty View Post
    The correct syntax for adding the Default value is

    Code:
    CurrentDb.TableDefs("YourTableName").Fields("ModifiedDate").DefaultValue = "Now()"
    From here: https://stackoverflow.com/questions/27608708/change-default-value-of-field-through-vba
    That assumes the field is already in place.

    Another set of descriptions is here : https://stackoverflow.com/questions/...g-in-ms-access
    TBF I did look on how it could be done, and I found that you had to use two statements.
    One to add the field. Then the next to set the default value.

    Yours seems more intuitive to use.

    Of course that setting however created will only work for new records subsequent to it being set.
    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
    yrstruly is offline Competent Performer
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2024
    Posts
    122
    Will it set = "Now()" in the table, not only the code but actucally alter that. So when i click to view the table in desgn mode i should see it for that column.

  8. #8
    Minty is offline VIP
    Windows 11 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    I'm not sure I understand your question.
    Create a copy of a single table, then run the code on that test table to see what happens?
    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 ↓↓

  9. #9
    Micron is online now Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,421
    Not sure I see the point of Now() as a default value instead of Date(). When you begin a record you will get a value for Now(). If it takes 5 minutes to finish the record (or worse, I take a coffee break first), then the value at the point of record completion isn't accurate. If time matters, it should be time stamped via code. If the real time doesn't matter, then the value should be Date(). Otherwise, what's the point of time stamping something that is 5, 10 or even 20 minutes off?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. Replies: 6
    Last Post: 04-26-2022, 10:16 AM
  2. Replies: 8
    Last Post: 07-25-2019, 04:25 PM
  3. Replies: 6
    Last Post: 09-11-2015, 05:05 AM
  4. VBA code used to work now it does not
    By rachello89 in forum Programming
    Replies: 9
    Last Post: 06-15-2012, 08:48 AM
  5. Replies: 8
    Last Post: 01-28-2012, 11:05 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