从访问表中查找并显示日期值

时间:2012-05-25 17:23:56

标签: ms-access vba ms-access-2003

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

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

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

谢谢你能提供帮助

2 个答案:

答案 0 :(得分: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功能,(或者如果您没有编写自己的功能),那么每次需要查找时,您最终都会在上面的“MyLookup”功能中编写所有代码。表中的单个值。

答案 1 :(得分: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#变量)但是我用冗长的方式写出来让它更清晰。

相关问题