Vba

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • GANA1234
    New Member
    • Mar 2008
    • 5

    Vba

    Hello All,

    I am trying to start a clock when I add a record to the table ( like a timestamp) how can I do it using VBA can any one give inputs on the VBA code.

    Thanks
  • orangeCat
    New Member
    • Dec 2007
    • 83

    #2
    Originally posted by GANA1234
    Hello All,

    I am trying to start a clock when I add a record to the table ( like a timestamp) how can I do it using VBA can any one give inputs on the VBA code.

    Thanks
    If your intent is to put a Date_TimeStamp on a record, then you must have a field on each record to hold the DateTime value.
    Suppose you have a Table -- MyTable
    with fields Fld1, Fld2 and DateStamp.
    When you create a record, you set the value of DateStamp to Now.
    Now is a built-in function that returns the system current Date and time.

    vba code may look like this

    .
    Code:
    .......
    Dim dbs as dao.database
    Dim rst as dao.recordset
    Set dbs = CurrentDb
          Set rst = dbs.OpenRecordset("MyTable")
          With rst
             .AddNew
             
             ![DateStamp] = Now   ' <--This would add datestamp to the record
             ![fld1] = "Tom Brown"
             ![fld2] = "Microsoft XP"
            
             .Update
             .Close
          End With
    ............

    Comment

    • missinglinq
      Recognized Expert Specialist
      • Nov 2006
      • 3533

      #3
      A number of ways to do this, of course, but he simplest is, in Design View for the table, select the field that holds the timestamp, go down to Default Value and enter =Now().

      Or, to add it in the form when the record is saved:
      Code:
      Private Sub Form_BeforeUpdate(Cancel As Integer)
         If Me.NewRecord Then Me.MyTimeStamp = Now()
      End Sub
      Or, to add it immediately when a record is created:
      Code:
      Private Sub Form_Current()
       If Me.NewRecord Then Me.MyTimeStamp = Now()
      End Sub
      Welcome to TheScripts!

      Linq ;0)>

      Comment

      • GANA1234
        New Member
        • Mar 2008
        • 5

        #4
        Thanks Orangecat and Missinglinq

        Comment

        Working...