1

在 Access 2003 中单击表单中的命令按钮时,我试图弹出一个 msgbox
。与数据库中表中引用的日期相比,msgbox 应该由当前日期触发。它看起来像这样:

If Date() is < [Date in table?], THEN "Msgbox" = "It is now Quarter 2"

一旦超过第 3 季度的日期,消息框将显示“现在是第 3 季度”

谢谢如果你能帮忙

4

2 回答 2

1

Access 有一组称为域函数的函数,用于查找存储在表中的单个信息。一些最常见的是 DCount()、DLookup()、DSum()、DAvg()、DMax() 和 DMin()。

为此,您需要使用 DLookup 函数。基本上,它需要一个字段名和一个表名来查找一个值。在许多情况下,您希望包含一个条件语句(或 WHERE 子句)作为第三个参数,以确保 DLookup 函数实际上是从正确的行中检索值。如果您不传递条件语句,域函数将简单地返回第一个匹配项。

If Date() <= DLookup("SomeDateField", "tblYourTableName") Then
    MsgBox "The date in stored in the table is today or else is in the future."
Else
    MsgBox "The date stored in the table is in the past."
End If

这是另一种写法:

If Date() < DLookup("SomeDateField", "tblYourTableName") Then
    MsgBox "The date in stored in the table is in the future."
Else
    MsgBox "The date stored in the table is today or is in the past."
End If

如果表中有多个记录/行,这就是您的操作方法。然后,您需要某种条件语句来缩小范围,以便从您想要的行中检索您想要的值。

If Date() < DLookup("SomeDateField", "tblYourTableName", "UserID = 1") Then
    MsgBox "The date in stored in the table is in the future."
Else
    MsgBox "The date stored in the table is today or is in the past."
End If

虽然这并不是您真正要问的,但我认为了解此功能(和其他域功能)幕后真正发生的事情很重要。基本上,您选择从一个表中检索一个值,并可以选择使用条件语句(在 SQL 中称为 WHERE 子句)指定要从哪个记录/行中检索值。因此,让我们看看您将如何编写这样的函数,以及 Microsoft 可能如何编写他们的 DLookup 函数。

Public Function MyLookup(ByVal strField As String, _
                         ByVal strTable As String, _
                         Optional ByVal strCriteria As String) As Variant
    'Error handling intentionally omitted
    'Proper error handling is very critical in
    'production code in a function such as this 
    If strField <> "" And strTable <> "" Then
        Dim sSQL as string
        sSQL = "SELECT TOP 1 " & strField & " FROM " & strTable
        If strCriteria <> "" Then
            sSQL = sSQL & " WHERE " & strCriteria
        End If
        Dim rst As DAO.Recordset
        Set rst = CurrentDb.OpenRecordset(sSQL, dbOpenSnapshot)
        If Not (rst.EOF and rst.BOF) Then
            MyLookup = rst(strField).Value
        End If
        rst.Close
        Set rst = Nothing
    End If
End Function

现在假设您想在联系人表中查找某人的生日:

Dim dteBirthDate as Date
dteBirthDate = MyLookup("BirthDate", "tblContacts", "ContactID = " & 12345)

如果您没有 DLookup 函数(或者如果您没有编写自己的函数),那么每次您需要在一张桌子。

于 2012-05-25T18:34:14.803 回答
0

我认为您正在寻找的是以下内容:

'Dates to be retrieved from the db (quarter start dates)
Dim q1 As Date
Dim q2 As Date
Dim q3 As Date
Dim q4 As Date
'Today's date
Dim today As Date
Dim quarter As Integer

Set today = Date()
Set q1 = DLookup("FieldContainingDate", "tableContainingDates", "quarter=1")
Set q2 = DLookup("FieldContainingDate", "tableContainingDates", "quarter=2")
Set q3 = DLookup("FieldContainingDate", "tableContainingDates", "quarter=3")
Set q4 = DLookup("FieldContainingDate", "tableContainingDates", "quarter=4")

Set quarter = 1 'Base case.
If (today > q1) Then quarter = 2
If (today > q2) Then quarter = 3
If (today > q3) Then quarter = 4

MsgBox "It is quarter " & quarter 'Display which quarter it is in a msgbox

您可能不得不根据您将日期格式存储在数据库中的方式等来摆弄日期格式。以另一种方式编写它也会更有效(例如删除中间 q# 变量)但我把它写出来了用很长的方式使它更清楚。

于 2012-05-25T19:42:03.817 回答