Results 1 to 7 of 7
  1. #1
    Newby is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Nov 2012
    Posts
    60

    Updating a date field in a record with the current date

    I am setting up a "to-do" table and I want a button that will update the "date last completed" with the current date. Also, I was watching a u-tube video show how a macro can be converted to code, but I do not see that function in Access 2007. Is that a feature of Access 2010?

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    typical format for an SQL UPDATE query

    Code:
    UPDATE table SET column = expression WHERE predicates;
    You must be careful with UPDATE query, if you do not constrain
    the request with a proper WHERE clause, you could UPDATE all records in your table
    .

    Here is a typical Update query in vba in a button click event. Hope it helps.

    Code:
    Private Sub Command4_Click()
    
    Dim DB As DAO.Database
    Dim sql As String
       On Error GoTo Command4_Click_Error
    
    sql = " UPDATE tblCabinet  " _
        & " SET  MyDate = Date " _
        & " WHERE RepairOrderNo = 12345"  
    Set DB = CurrentDb
    DB.Execute sql, dbFailOnError
    
       On Error GoTo 0
       Exit Sub
    
    Command4_Click_Error:
    
        MsgBox "Error " & Err.number & " (" & Err.Description  _ 
             & ") in procedure Command4_Click of VBA Document Form_Form5"
    End Sub
    Do some research on SQL queries. There is a tutorial at
    http://www.w3schools.com/sql/default.asp
    Last edited by orange; 12-10-2012 at 11:02 AM. Reason: added example after seeing Allan had given Macro sample

  4. #4
    Newby is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Nov 2012
    Posts
    60

    Thanks for your help, but I am still having issues

    Quote Originally Posted by orange View Post
    typical format for an SQL UPDATE query

    Code:
    UPDATE table SET column = expression WHERE predicates;
    You must be careful with UPDATE query, if you do not constrain
    the request with a proper WHERE clause, you could UPDATE all records in your table
    .

    Here is a typical Update query in vba in a button click event. Hope it helps.

    Code:
    Private Sub Command4_Click()
    
    Dim DB As DAO.Database
    Dim sql As String
       On Error GoTo Command4_Click_Error
    
    sql = " UPDATE tblCabinet  " _
        & " SET  MyDate = Date " _
        & " WHERE RepairOrderNo = 12345"  
    Set DB = CurrentDb
    DB.Execute sql, dbFailOnError
    
       On Error GoTo 0
       Exit Sub
    
    Command4_Click_Error:
    
        MsgBox "Error " & Err.number & " (" & Err.Description  _ 
             & ") in procedure Command4_Click of VBA Document Form_Form5"
    End Sub
    Do some research on SQL queries. There is a tutorial at
    http://www.w3schools.com/sql/default.asp
    Private Sub Cmd_Update_Click()

    Dim DB As DAO.Database
    Dim sql As String
    On Error GoTo Command4_Click_Error

    sql = " UPDATE to_dos " _
    & " SET DateCompleted = Date " _ DateCompleted is my field Name
    & " WHERE ID = 1"
    Set DB = CurrentDb
    DB.Execute sql, dbFailOnError

    On Error GoTo 0
    Exit Sub

    Command4_Click_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description _
    & ") in procedure Command4_Click of VBA Document Form_Form5"
    End Sub


    Note: I need this to update the DateCompleted field with the current date for whatever record that I am view in a form. I hard coded the Where command above to “ID = 1” to see if I could get the program to update record 1, but got this message instead.

    Error 3061 (Too Few paramenters. Expected 1.) in procedure Command4_Click of VBA Document form _Form5


  5. #5
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Code:
    Private Sub Cmd_Update_Click()
    Code:
    Dim DB As DAO.Database
    Dim sql As String
       On Error GoTo Command4_Click_Error
    
    sql = " UPDATE to_dos  " _
        & " SET  DateCompleted = Date " _  '''''''DateCompleted is my field Name
        & " WHERE ID = 1"
    Set DB = CurrentDb
    DB.Execute sql, dbFailOnError
    
       On Error GoTo 0
       Exit Sub
    
    Command4_Click_Error:
    
        MsgBox "Error " & Err.Number & " (" & Err.Description _
             & ") in procedure Command4_Click of VBA Document Form_Form5"
    End Sub
    Ok so you do have a button on a form called Cmd_Update and
    you do have a numeric Id field in your record?

    You need to separate a comment from code with at least one ' (I used several to highlight where this is in your code)

  6. #6
    Newby is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Nov 2012
    Posts
    60
    Quote Originally Posted by orange View Post
    Code:
    Private Sub Cmd_Update_Click()
    Code:
    Dim DB As DAO.Database
    Dim sql As String
       On Error GoTo Command4_Click_Error
    
    sql = " UPDATE to_dos  " _
        & " SET  DateCompleted = Date " _  '''''''DateCompleted is my field Name
        & " WHERE ID = 1"
    Set DB = CurrentDb
    DB.Execute sql, dbFailOnError
    
       On Error GoTo 0
       Exit Sub
    
    Command4_Click_Error:
    
        MsgBox "Error " & Err.Number & " (" & Err.Description _
             & ") in procedure Command4_Click of VBA Document Form_Form5"
    End Sub
    Ok so you do have a button on a form called Cmd_Update and
    you do have a numeric Id field in your record?

    You need to separate a comment from code with at least one ' (I used several to highlight where this is in your code)
    That comment was not in my code . I added that comment to this thread only. Some where in this code I need to identify that the current record is the record whose date field I want to update. This is where I am stuggling.

  7. #7
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    Regarding your error 3061 - you are getting that because either DateCompleted or ID (or both) is not the name of a field in your table to_dos.

    John

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

Similar Threads

  1. Replies: 25
    Last Post: 11-16-2012, 12:47 PM
  2. Replies: 3
    Last Post: 10-25-2012, 10:04 PM
  3. Replies: 11
    Last Post: 03-29-2012, 11:23 PM
  4. Replies: 1
    Last Post: 12-07-2011, 01:02 PM
  5. Set textbox field to depend on current date
    By lilanngel in forum Queries
    Replies: 6
    Last Post: 03-10-2011, 08:16 AM

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