ShawnGray
asked on
rst.MoveNext
I'm trying to learn loops and movenext stuff.
Found this code on EE and modified it to see how it works.
I have three records in the table all marked "yes".
But the code sends the first record three times.
Can anyone show me why and how to correct?
Thank you,
Shawn
Found this code on EE and modified it to see how it works.
I have three records in the table all marked "yes".
But the code sends the first record three times.
Can anyone show me why and how to correct?
Thank you,
Shawn
Private Sub TESTcmd_Click()
Dim dbthisdb As DAO.Database
Dim rst As DAO.Recordset
Dim strSql As String
Dim strSendTo As String
strSql = "SELECT [name] FROM [xxxTESTxxx]" _
& "WHERE [F2] = Yes" _
Set dbthisdb = DBEngine.Workspaces(0).Databases(0)
Set rst = dbthisdb.OpenRecordset(strSql, dbOpenForwardOnly)
Do While Not rst.EOF()
DoCmd.SendObject , , , EmailAddress, , , "Subject Test line" & [F1], "test message text", False
rst.MoveNext
Loop
Set rst = Nothing
Set dbthisdb = Nothing
End Sub
Set dbthisdb = DBEngine.Workspaces(0).Dat abases(0)
Set rst = dbthisdb.OpenRecordset(str Sql, dbOpenForwardOnly)
rst.movefirst
Do Until rst.EOF
DoCmd.SendObject , , , EmailAddress, , , "Subject Test line" & [F1], "test message text", False
rst.MoveNext
Loop
Set rst = dbthisdb.OpenRecordset(str
rst.movefirst
Do Until rst.EOF
DoCmd.SendObject , , , EmailAddress, , , "Subject Test line" & [F1], "test message text", False
rst.MoveNext
Loop
Set dbthisdb = DBEngine.Workspaces(0).Dat abases(0)
Set rst = dbthisdb.OpenRecordset(str Sql, dbOpenForwardOnly)
rst.movefirst
Do Until rst.EOF
DoCmd.SendObject , , , EmailAddress, , , "Subject Test line " & rst(0) & ", "test message text", False
rst.MoveNext
Loop
Set rst = dbthisdb.OpenRecordset(str
rst.movefirst
Do Until rst.EOF
DoCmd.SendObject , , , EmailAddress, , , "Subject Test line " & rst(0) & ", "test message text", False
rst.MoveNext
Loop
oop missing " &
DoCmd.SendObject , , , EmailAddress, , , "Subject Test line " & rst(0) & "," & "test message text", False
DoCmd.SendObject , , , EmailAddress, , , "Subject Test line " & rst(0) & "," & "test message text", False
ASKER
Thank you. I'm getting "invalid operation" at
rst.MoveFirst
rst.MoveFirst
the strSql is wrong too.
Private Sub TESTcmd_Click()
Dim dbthisdb As DAO.Database
Dim rst As DAO.Recordset
Dim strSql As String
Dim strSendTo As String
strSql = "SELECT [name] FROM [xxxTESTxxx]"
strSql =strSql & " WHERE [F2] = Yes"
Set dbthisdb = DBEngine.Workspaces(0).Databases(0)
Set rst = dbthisdb.OpenRecordset(strSql)
if rst.eof then
msgbox "No records"
exit sub
end if
rst.movefirst
Do Until rst.EOF
DoCmd.SendObject , , , EmailAddress, , , "Subject Test line " & rst(0) & ", "test message text", False
rst.MoveNext
Loop
Set rst = Nothing
Set dbthisdb = Nothing
End Sub
you have to correct this line too
DoCmd.SendObject , , , EmailAddress, , , "Subject Test line " & rst(0) & ", "test message text", False
changing EmailAddress to a real email address
DoCmd.SendObject , , , EmailAddress, , , "Subject Test line " & rst(0) & ", "test message text", False
changing EmailAddress to a real email address
ASKER
see the msgbox after clicking the button
emailloopRev.mdb
emailloopRev.mdb
ASKER
Hey Cap,
Sorry, I must have some mental block on this topic.
I tweaked the code below, it now sends to the correct address but it ignores my "where" clause and seems to email record one to all three email addresses.
(changed the field [Name] to [NameField]. Learned that it inserts the name of the table when I use "Name".)
Sorry, I must have some mental block on this topic.
I tweaked the code below, it now sends to the correct address but it ignores my "where" clause and seems to email record one to all three email addresses.
(changed the field [Name] to [NameField]. Learned that it inserts the name of the table when I use "Name".)
Private Sub TESTcmd_Click()
Dim dbthisdb As DAO.Database
Dim rst As DAO.Recordset
Dim strSql As String
Dim strSendTo As String
strSql = "SELECT [emailaddress] FROM [xxxTESTxxx]"
strSql = strSql & " WHERE [F2] = Yes"
Set dbthisdb = DBEngine.Workspaces(0).Databases(0)
Set rst = dbthisdb.OpenRecordset(strSql)
If rst.EOF Then
MsgBox "No records"
Exit Sub
End If
rst.MoveFirst
Do Until rst.EOF
DoCmd.SendObject , , , rst(0), , , "-F1=" & [F1] & " -NameField=" & [NameField] & " -F2=" & [F2], "test message text " & [emailaddress], False
'Debug.Print rst(0)
'MsgBox rst(0)
rst.MoveNext
Loop
Set rst = Nothing
Set dbthisdb = Nothing
End Sub
can you explain what you are trying to do in this statement
DoCmd.SendObject , , , rst(0), , , "-F1=" & [F1] & " -NameField=" & [NameField] & " -F2=" & [F2], "test message text " & [emailaddress], False
DoCmd.SendObject , , , rst(0), , , "-F1=" & [F1] & " -NameField=" & [NameField] & " -F2=" & [F2], "test message text " & [emailaddress], False
ASKER
The rst(0) is new to me but it seems to grab the email address. :/
The rest of the stuff is supposed take a few fields from each record and email it to a client. Ulitmately these fields will be [clientaccountnumber] and [accountbalance]. Very specific to each email address.
The rest of the stuff is supposed take a few fields from each record and email it to a client. Ulitmately these fields will be [clientaccountnumber] and [accountbalance]. Very specific to each email address.
change this
strSql = "SELECT [emailaddress] FROM [xxxTESTxxx]"
strSql = strSql & " WHERE [F2] = Yes"
to
strSql = "SELECT * FROM [xxxTESTxxx]"
strSql = strSql & " WHERE [F2] = Yes"
to refer to the fields in the recordset, use this
rs!emailaddress
rs!clientaccountnumber
rs!accountbalance
rs!F2
rs!F1
strSql = "SELECT [emailaddress] FROM [xxxTESTxxx]"
strSql = strSql & " WHERE [F2] = Yes"
to
strSql = "SELECT * FROM [xxxTESTxxx]"
strSql = strSql & " WHERE [F2] = Yes"
to refer to the fields in the recordset, use this
rs!emailaddress
rs!clientaccountnumber
rs!accountbalance
rs!F2
rs!F1
ASKER
Like this?
DoCmd.SendObject , , , rst(0), , , rs!f1, "test message text ", False
says "object required"
DoCmd.SendObject , , , rst(0), , , rs!f1, "test message text ", False
says "object required"
DoCmd.SendObject , , , rst!emailaddress, , , rs!f1, "test message text ", False
ASKER
still get an error on that line "object required".
Dim dbthisdb As DAO.Database
Dim rst As DAO.Recordset
Dim strSql As String
Dim strSendTo As String
strSql = "SELECT * FROM [xxxTESTxxx]"
strSql = strSql & " WHERE [F2] = Yes"
Set dbthisdb = DBEngine.Workspaces(0).Databases(0)
Set rst = dbthisdb.OpenRecordset(strSql)
If rst.EOF Then
MsgBox "No records"
Exit Sub
End If
rst.MoveFirst
Do Until rst.EOF
DoCmd.SendObject , , , rst!emailaddress, , , rs!F1, "test message text ", False
rst.MoveNext
Loop
Set rst = Nothing
Set dbthisdb = Nothing
use this
DoCmd.SendObject acSendNoObject, , , rst!emailaddress, , , rs!F1, "test message text ", False
ASKER
same error
emailloopRev.mdb
emailloopRev.mdb
oh...nooo... ( rs!F1 ) '<<< we are not using rs
use this
DoCmd.SendObject acSendNoObject, , , rst!emailaddress, , , rst!F1, "test message text ", False
use this
DoCmd.SendObject acSendNoObject, , , rst!emailaddress, , , rst!F1, "test message text ", False
ASKER
Ok. Cool. The fields are working right. But it still ignores WHERE.
All records are sent even if [f2]=No
All records are sent even if [f2]=No
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Cap,
Thank you. I really appreciate your help.
You're awesome.
Shawn
Thank you. I really appreciate your help.
You're awesome.
Shawn
I can't see why you're even sending the first record.
There doesn't seem to be any reference to your recordset in the sendobject.