日期和时间格式自动更改为文本

时间:2018-02-10 07:47:20

标签: access-vba

我需要您的帮助,将Outlook邮件数据下载到时间格式为hh:mm的特定访问表字段中。下面的代码效果很好,我已经格式化了表格字段,它是以hh:mm格式的estml。 问题1)此代码由计时器触发。当计时器运行时,数据被导入,但是estml字段属性自动变为文本,估计值存储为文本。 问题2)主要问题是当我运行一个应该显示估计>的查询时7小时由于数据存储为文本,因此查询无法获得正确的结果。 问题3)估计可能超过24小时,比如75小时。该领域仍然应该捕获75小时。

Private Sub getml()
Dim rst As DAO.Recordset
Dim OlApp As Outlook.Application

Dim inbox As Outlook.MAPIFolder
Dim inboxItems As Outlook.Items
Dim Mailobject As Object
Dim db As DAO.Database
Dim dealer As Integer
Set db = CurrentDb

Set OlApp = CreateObject("Outlook.Application")
Set inbox = OlApp.GetNamespace("Mapi").GetDefaultFolder(olFold erInbox)
Set rst= CurrentDb.OpenRecordset("mls")
Set inboxItems = inbox.Items
For Each Mailobject In inboxItems

With rst
.AddNew
!task= Mailobject.UserProperties.Find("taskID")
!estml= Mailobject.UserProperties.Find("estimate")
.Update

Mailobject.UnRead = False
End With
End If
Next
Set OlApp = Nothing
Set inbox = Nothing
Set inboxItems = Nothing
Set Mailobject = Nothing
End Sub

2 个答案:

答案 0 :(得分:2)

这里有许多误解。

  1. \ testml 的格式对于值并不重要。它仅供展示。
  2. 如果 \ testml 的数据类型为日期,则 \ testml字段属性会自动更改为文本,而估算值将以文本的形式存储。这两个都不能:查询没有得到正确的结果,因为数据存储为文本
  3. 如果Mailobject.UserProperties.Find("estimate")返回文字,请将其转换为日期值。
  4. 估计可以超过24小时,比如说75小时。这是可能的,但它不能像75小时一样显示 - 23:59:59是最大值。
  5. 广告。 3:

    !estml= TimeValue(Mailobject.UserProperties.Find("estimate"))
    

    广告。 4:

    您可以使用此功能显示小时(和分钟):

    Public Function FormatHourMinute( _
      ByVal datTime As Date, _
      Optional ByVal strSeparator As String = ":") _
      As String
    
    ' Returns count of days, hours and minutes of datTime
    ' converted to hours and minutes as a formatted string
    ' with an optional choice of time separator.
    '
    ' Example:
    '   datTime: #10:03# + #20:01#
    '   returns: 30:04
    '
    ' 2005-02-05. Cactus Data ApS, CPH.
    
      Dim strHour       As String
      Dim strMinute     As String
      Dim strHourMinute As String
    
      strHour = CStr(Fix(datTime) * 24 + Hour(datTime))
      ' Add leading zero to minute count when needed.
      strMinute = Right("0" & CStr(Minute(datTime)), 2)
      strHourMinute = strHour & strSeparator & strMinute
    
      FormatHourMinute = strHourMinute
    
    End Function
    

答案 1 :(得分:1)

跟进我的意见(上文)......

您说,在表mls中,字段estml具有:

  • 数据类型设置为Date/Time

  • 格式设置为hh:mm

...但你说:

  

“由于数据存储为文本,因此查询无法获得正确的结果。”

这没有意义。如果数据类型明确设置为Date/Time,则不会自动更改为文本。

显示 以其他方式,因为 格式 适用于 table ,不一定是整个数据库,但数据类型不会改变。

要确认该字段是否存储为文本(并且您正在查看实际未格式化的数据),请确保正确设置表格,例如双击数据类型 和< / em>删除任何现有的格式,如下所示:

example reset data type

如果您的问题仍未解决,则需要分享更多信息,例如您的问题 表格设计视图的屏幕截图。