在访问中循环查询

时间:2016-03-23 12:50:59

标签: vba ms-access

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

我正在尝试循环查询&#34;员工查询&#34;向其中的每个人发送文本。我有一切工作,除了它不会循环查询的事实,它循环通过我打开它的表单上的第一个记录,它不会移动。 有人对此有什么建议吗? 先感谢您。

1 个答案:

答案 0 :(得分:2)

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

像这样改变循环的开头:

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