【问题标题】:Looping through query in access在访问中循环查询
【发布时间】:2016-03-23 21:06:17
【问题描述】:
Dim strCell As String
Dim strProv As String
Dim strTextEmail As String
Dim oApp As Outlook.Application
Dim oMail As MailItem
Set oApp = CreateObject("Outlook.application")


Dim StrBCC As String
Dim rst As DAO.Recordset
Dim qdf As DAO.QueryDef
Dim db As DAO.Database

Set db = CurrentDb
Set qdf = db.QueryDefs("Staff Query")

Set rst = qdf.OpenRecordset()

With rst
Do Until .EOF

strCell = Nz([CellPhone], "0")
strProv = Nz([CellProvider], "0")
MsgBox [CellPhone]
MsgBox [CellProvider]

If strCell = "0" Or strProv = "0" Then
    MsgBox "MISSING DATA: Please double check the agent name, cell phone number and provider are entered in the staff table."
Else
         Dim Clean As String
         Dim Pos, A_Char
         Pos = 1
         For Pos = 1 To Len(strCell)
            A_Char = Mid(strCell, Pos, 1)
            If A_Char >= "0" And A_Char <= "9" Then
              Clean = Clean + A_Char
            End If
         Next Pos
         strCell = Clean
         strProv = DLookup("[EmailExt]", "CellProviders", "[CellProvider]='" & strProv & "'")
strTextEmail = strCell & strProv
End If


StrBCC = StrBCC & strTextEmail & ";"

rst.MoveNext
Loop

End With
StrBCC = Left(StrBCC, Len(StrBCC) - 1)

Set oMail = oApp.CreateItem(olMailItem)
oMail.BCC = StrBCC
oMail.Display

Set oMail = Nothing
Set oApp = Nothing
Set rst = Nothing

我正在尝试遍历查询“员工查询”以向其中的每个人发送文本。除了它不会循环通过查询,它会循环通过我打开它的表单上的第一条记录并且它不会移动之外,我一切正常。 有人对此有什么建议吗? 先感谢您。

【问题讨论】:

  • 在开始循环之前尝试 rst.MoveFirst
  • 以及MoveFirst先检查是否有记录-If Not .BOF AND Not .EOF THEN: DO UNTIL .EOF.....

标签: vba ms-access


【解决方案1】:

您没有将记录集中的值读入本地变量。您现在所做的是从表单控件中读取值(我猜)。因此,如果您移动到记录集中的下一条记录,这些都不会改变。

像这样改变循环的开始:

With rst
  Do Until .EOF
    strCell = Nz(rst.Fields("CellPhone").Value, "0")
    strProv = Nz(rst.Fields("CellProvider").Value, "0")

【讨论】:

  • 非常感谢!如此简单却忽略了这个事实。
  • 最后一个问题,当电话号码出现在密件抄送字段中时,它们会不断被添加到下一条记录中。我不确定是要重置字段还是我附加错误?
  • 好吧,我看不到您将邮件发送到哪里,但我没有质疑它,因为它不是问题的一部分。 - 无论如何,您将当前数字附加到您的变量 strBCC。如果你不想这样,只需将其设置为新值:strBCC = strTextEmail
  • 我想通了,但感谢您的回复。必须在循环的每次迭代之前将 clean 变量设置为 ""。
猜你喜欢
  • 1970-01-01
  • 2011-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多