Link to home
Start Free TrialLog in
Avatar of ShawnGray
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

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

Open in new window

Avatar of peter57r
peter57r
Flag of United Kingdom of Great Britain and Northern Ireland image

You've got me beat..
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.
Set dbthisdb = DBEngine.Workspaces(0).Databases(0)
Set rst = dbthisdb.OpenRecordset(strSql, dbOpenForwardOnly)
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).Databases(0)
Set rst = dbthisdb.OpenRecordset(strSql, dbOpenForwardOnly)
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
Avatar of ShawnGray
ShawnGray

ASKER

Thank you.  I'm getting "invalid operation" at

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

Open in new window

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
I've attached a sample.
Would you look at this?
Thank you,
Shawn
emailloop.mdb
see the msgbox after clicking the button
emailloopRev.mdb
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".)
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

Open in new window

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


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.
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
Like this?
DoCmd.SendObject , , , rst(0), , , rs!f1, "test message text ", False
says "object required"


DoCmd.SendObject , , , rst!emailaddress, , , rs!f1, "test message text ", False
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

Open in new window



use this

    DoCmd.SendObject acSendNoObject, , , rst!emailaddress, , , rs!F1, "test message text ", False
same error
emailloopRev.mdb
oh...nooo...   (  rs!F1 ) '<<< we are not using rs

use this


DoCmd.SendObject acSendNoObject, , , rst!emailaddress, , , rst!F1, "test message text ", False
Ok.  Cool. The fields are working right.  But it still ignores WHERE.
All records are sent even if [f2]=No
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Cap,
Thank you.  I really appreciate your help.
You're awesome.
Shawn