如果那么其他用于在VBA中循环 [英] If then else for looping in VBA

查看:46
本文介绍了如果那么其他用于在VBA中循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我需要这个VBA的帮助。

Hello I need help with this VBA.

以下是我正在尝试的内容,根据名为"InStock"的查询和名为"Location"的字段,此字段中只有两种可能的选择,其中"演示"或"Intl"。

Here’s what I am trying, based on a query called "InStock" and a field named "Location" there are only two possible choices in this field, where are either "Demo" or "Intl".

我想要做的是如果Location ="Demo"然后发送电子邮件到一个地方,否则如果Location ="Intl"发送到另一封电子邮件,或者说"没有记录"

What I am trying to do is if Location = "Demo" then send an email to one place, else if Location = "Intl" send to another email, or say "No Records"

我正在做 rs 的原因是如果查询"InStock"中同时存在"Demo"或"Intl",它将向两者发送电子邮件。

The reason why I am doing rs is if there are both a "Demo" or "Intl" in the query "InStock" it will send an email to both.

我已将查询设置为只有0,1或2条记录。这意味着可以返回零记录,或者只有1个Demo,或者只有1个Intl,或者只有1个Demo和1个Intl。

I have set the query to only have 0, 1 or 2 records. Meaning there can be zero records returned, or only 1 Demo, or only 1 Intl, or both just 1 Demo and just 1 Intl.

Private Sub

Private Sub

Command33_Click()

Command33_Click()

设置rs = CurrentDb.OpenRecordset(" ;选择位置FROM InStock")

Set rs = CurrentDb.OpenRecordset("SELECT location FROM InStock")

如果不是(rs.EOF和rs.BOF)那么

If Not (rs.EOF And rs.BOF) Then

ElseIf Me.Location =" Demo"那么

ElseIf Me.Location = "Demo" Then

DoCmd.SendObject _

DoCmd.SendObject _

,_

,_

,_

" email@email.com"

"email@email.com"

,_

,_

,_

"主题1到达此处",_

"Subject 1 goes here", _

"电子邮件的第1条去这里" _

"Message 1 of email goes here " _

,_

错误

否则

DoCmd .SendObject _

DoCmd.SendObject _

,_

,_

,_

" email@email.com"

"email@email.com"

,_

,_

,_

"主题2到达此处",_

"Subject 2 goes here", _

"消息2到达此处" _

"Message 2 goes here " _

,_

错误

结束如果

直到rs.EOF = True

Do Until rs.EOF = True

rs.MoveNext

rs.MoveNext

循环

MsgBox" NO Records"

MsgBox "NO Records"

rs.Close Set

rs.Close Set

rs = Nothing

rs = Nothing

End Sub

推荐答案

循环逻辑存在缺陷 - 在开始循环之前发送消息(Do Until ......循环)。试试这个:

The logic of your loop is flawed - you send the message before starting the loop (Do Until ... Loop). Try this:

Private Sub Command33_Click()
    Dim rs As DAO.Recordset
    Dim strSQL As String
    Dim strAddr As String
    Dim strSubj As String
    Dim strBody As String
    strSQL = "SELECT location FROM InStock"
    Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenForwardOnly)
    Do While Not rs.EOF
        If rs!Location = "Demo" Then
            strAddr = "email1@email.com"
            strSubj = "Subject 1 goes here"
            strBody = "Message 1 of email goes here"
        Else
            strAddr = "email2@email.com"
            strSubj = "Subject 2 goes here"
            strBody = "Message 2 of email goes here"
        End If
        DoCmd.SendObject To:=strAddr, Subject:=strSubj, _
            MessageText:=strBody, EditMessage:=False
        rs.MoveNext
    Loop
    rs.Close
    Set rs = Nothing
End Sub


这篇关于如果那么其他用于在VBA中循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆