简体   繁体   English

在访问中循环查询

[英]Looping through query in 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

I am trying to loop through the query "Staff Query" to send a text to everyone in it. 我正在尝试遍历查询“职员查询”,以向其中的每个人发送文本。 I have everything working except the fact that it wont loop through the query, it loops through the first record on the form I open it on and it wont move. 除了它不会遍历查询,它遍历我打开它的表单上的第一条记录并且它不会移动之外,其他所有工作都在进行。 Anyone have any suggestions for this? 有人对此有任何建议吗? Thank you in advance. 先感谢您。

You are not reading the values from the recordset into your local variables. 您没有将记录集中的值读入本地变量。 What you do right now, is reading the values from form controls (I guess). 您现在正在做的是从表单控件中读取值(我想)。 So these will not change if you move to the next record in the recordset. 因此,如果您移至记录集中的下一个记录,这些内容将不会更改。

Change the beginning of the loop like this: 像这样更改循环的开始:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM