2

我在 MS ACCESS 中有一个数据库,其中我有一个包含一些计数的 ECount 字段有什么方法可以在 VBA 代码中拉出该调用,我正在生成一封新邮件,该邮件应该拉出邮件中的计数。

Private Sub Command_Click()
Dim objOutlook As Outlook.Application
Dim objEmail As Outlook.MailItem
Dim ThisDay As Date

ThisDay = Format(Now, "mm/dd/yy")


Set objOutlook = CreateObject("Outlook.application")
Set objEmail = objOutlook.CreateItem(olMailItem)

With objEmail
    .To = ""
    .CC = ""
    .Subject = "Daily Email Processed " *(Thisday)
    .Body = "Hi," + vbNewLine + vbNewLine + vbNewLine + "Please find below the number of Emails processed for the  " + vbNewLine + vbNewLine + "Email Count = " (**ECOUNT**) + vbNewLine + "O Count = " (**OCount**)
    .Attachments.Add ""
    .Attachments.Add ""
    .Display
    '.ReadReceiptRequested

End With
4

3 回答 3

1

从表/查询中检索单个字段的最简单方法是使用DLOOKUPAccess 的功能。

所以你可以写:

    .Body = "嗨" + vbNewLine + vbNewLine + vbNewLine + _
        "请在下面找到为 " + vbNewLine + vbNewLine + _ 处理的电子邮件数量
        "Email Count = " + DLookup("ECount", "SourceTableOrQuery", "Criteria1 = 5 and Criteria2 = ""Test""") + vbNewLine + _
        "O 计数 = " + DLookup("OCount", "SourceTableOrQuery")

或者,如果您还没有计数,但想计算项目数,您可以使用“DCount”函数。

于 2013-02-12T08:46:02.690 回答
1
Dim db As DAO.Database
Dim rs1 As DAO.Recordset
Dim sCount As String

Set db = CurrentDb()
Set rs1 = db.OpenRecordset("SELECT TOP 1 ECounter FROM [Email Query]")

If rs1.RecordCount > 0 Then
    rs1.MoveFirst
    sCount = rs1.Fields("ECount")
End If
rs1.Close
set rs1 = Nothing
set db = Nothing

sCount将保持价值

于 2013-02-12T08:46:07.267 回答
0
Dim db As DAO.Database
Dim rs1 As DAO.Recordset
Dim sCount As String

Set db = CurrentDb()
Set rs1 = db.OpenRecordset("SELECT TOP 1 ECounter FROM [Email Query]")

If rs1.RecordCount > 0 Then
    rs1.MoveFirst
    sCount = rs1.Fields("ECount")
End If
rs1.Close
set rs1 = Nothing
set db = Nothing
于 2013-02-12T10:42:16.840 回答