如何格式化日期和时间内部创建表功能时的时间戳?

时间:2016-10-31 18:51:08

标签: datetime properties formatting access-vba

在我的(新手)DAO数据库中,我的表已成功创建,但是,没有数据输入到日期和时间戳中。我的目标是让本地用户的当前日期戳和时间戳在创建后自动进入Datestamp和Timestamp字段并进行格式化。任何人都可以帮我弄清楚我哪里出错了,并解决这个问题?谢谢!

Function UALCreate()
   Dim db As DAO.Database
   Dim tdf As DAO.TableDef
   Dim fld As Field
   Dim p As Property
   Dim pr As Property

   Set db = CurrentDb()
   Set tdf = db.CreateTableDef("UserActivityLog")

   With tdf
      Set fld = .CreateField("UserActivityLog_ID", dbGUID)
      fld.Attributes = dbFixedField
      fld.DefaultValue = "genGUID()"
      .Fields.Append fld

      Set fld = .CreateField("Owner_ID", dbLong)
      .Fields.Append fld

      .Fields.Append .CreateField("Activity", dbText, 50)
      .Fields.Append .CreateField("CurrentForm", dbText, 50)
      .Fields.Append .CreateField("LastForm", dbText, 50)
      .Fields.Append .CreateField("Datestamp", dbDate)

      .Fields.Append .CreateField("Timestamp", dbDate)

      .Fields.Append .CreateField("UserName", dbText, 50)
      .Fields.Append .CreateField("User_ID", dbLong)
      .Fields.Append .CreateField("UserTypeID", dbLong)
   End With

   db.TableDefs.Append tdf

   Set fld = tdf.Fields("Datestamp")
   Set p = fld.CreateProperty("Format", dbText, "mm/dd/yyyy")
   Set fld = tdf.Fields("Timestamp")
   Set pr = fld.CreateProperty("Format", dbText, "hh:mm:ss")
   fld.Properties.Append p
   fld.Properties.Append pr
   Set p = Nothing
   Set pr = Nothing

   Set fld = Nothing
   Set tdf = Nothing
   Set db = Nothing
   Debug.Print "UserActivityLog"
End Function

2 个答案:

答案 0 :(得分:0)

  

..创建后的格式化

忘记这一点。相反,在显示或打印数据时始终应用格式。

您不需要两个字段来保存日期和时间。组合它们并使用Now设置值更容易。

答案 1 :(得分:0)

Function UALCreate()
   Dim db As DAO.Database
   Dim tdf As DAO.TableDef
   Dim fld As Field
   Dim p As Property
   Dim pr As Property
   Dim rs As Recordset

   Set db = CurrentDb()
   Set tdf = db.CreateTableDef("UserActivityLog")

   With tdf
      Set fld = .CreateField("UserActivityLog_ID", dbGUID)
      fld.Attributes = dbFixedField
      fld.DefaultValue = "genGUID()"
      .Fields.Append fld

      Set fld = .CreateField("Owner_ID", dbLong)
      .Fields.Append fld

      .Fields.Append .CreateField("Activity", dbText, 50)
      .Fields.Append .CreateField("CurrentForm", dbText, 50)
      .Fields.Append .CreateField("LastForm", dbText, 50)
      .Fields.Append .CreateField("Datestamp", dbDate)

      .Fields.Append .CreateField("Timestamp", dbDate)

      .Fields.Append .CreateField("UserName", dbText, 50)
      .Fields.Append .CreateField("User_ID", dbLong)
      .Fields.Append .CreateField("UserTypeID", dbLong)
   End With

   db.TableDefs.Append tdf
   CurrentDb.TableDefs.Refresh

   Set fld = Nothing
   Set p = fld
   Set fld = tdf.Fields("Datestamp")
   Set p = fld.CreateProperty("Format", dbText, "mm/dd/yyyy")
   fld.Properties.Append p
   Set p = Nothing

   Set fld = Nothing
   Set p = fld
   Set fld = tdf.Fields("Timestamp")
   Set p = fld.CreateProperty("Format", dbText, "hh:mm:ss")
   fld.Properties.Append p
   Set p = Nothing

   Set fld = Nothing
   Set tdf = Nothing

   CurrentDb.TableDefs.Refresh

   Set rs = CurrentDb.OpenRecordset("UserActivityLog", dbOpenDynaset, dbSeeChanges)

   rs.AddNew
   rs!Owner_ID = 0
   rs!Activity = "Logon"
   rs!CurrentForm = "Logon"
   rs!LastForm = "None"
   rs!Datestamp = #1/1/1900#
   rs!Timestamp = #1/1/1900#
   rs!UserName = "None"
   rs!User_ID = 0
   rs!UserTypeID = 0
   rs.Update
   rs.Requery

   DoCmd.Close acTable, "UserActivityLog", acSaveYes

   Set rs = Nothing
   Set db = Nothing
End Function