query based email

I am trying to set up an email notification that feeds off of a query in my access db.  Since it is a many result I need to have it go through a loop.  All the code below seems to work except the loop area.  I comment that out and it works fine.  When not commented, my computer just sits there and looks at me.  I hope another set of eyes can find the problem.  I'll give more points if it turns out to be harder than I predicted.

Code below:

Public Sub ReqEmail()
On Error GoTo ErrorHandler

Dim RecordCounter As Integer
Dim ReturnValue As Variant
Dim MaxRecords As Integer
Dim Line_1 As String, Line_2 As String, Line_3 As String, Line_4 As String, Line_5 As String, Line_6 As String, Line_7 As String, strParagraph As String, strBody As String
Dim strEmail As String, rs As DAO.Recordset

DoCmd.Hourglass True

strEmail = "SELECT T_ORDER_INFO.FOLDER_NUM, T_REQ_LINE_ITEM.LINE_ITEM, T_REQ_LINE_ITEM.PART_NUM, T_RECEIVING.RECVD_DATE, " & _
        "T_RECEIVING.QTY, T_RECEIVING.COMMENTS, T_ORDER_INFO.REQUESTOR_CDSID " & _
        "FROM ((T_ORDER_INFO INNER JOIN T_REQUISITION_INFO ON T_ORDER_INFO.ID = T_REQUISITION_INFO.REF_ID) INNER JOIN " & _
        "T_REQ_LINE_ITEM ON T_REQUISITION_INFO.ID = T_REQ_LINE_ITEM.REF_ID) INNER JOIN T_RECEIVING ON " & _
        "T_REQ_LINE_ITEM.ID = T_RECEIVING.REF_ID " & _
        "WHERE (((T_ORDER_INFO.FOLDER_NUM)='" & [Forms]![F_REC_IMPOUND_MAIN]![FOLDER_NUM] & "') AND ((T_RECEIVING.RECVD_DATE) Between #" & _
        [Forms]![F_REC_IMPOUND_MAIN]![F_LINE_ITEM-SUB].[Form]![START_DATE] & "# And #" & [Forms]![F_REC_IMPOUND_MAIN]![F_LINE_ITEM-SUB].[Form]![END_DATE] & "#));"

Set rs = CurrentDb.OpenRecordset(strEmail, dbOpenSnapshot)
strParagraph = ""

RecordCounter = 1
MaxRecords = 29
MaxRecords = rs.RecordCount

'Initialize display message in status bar.
ReturnValue = SysCmd(SYSCMD_INITMETER, "Attaching Records to Email", MaxRecords)

If rs.RecordCount > 0 Then
    Line_1 = "Folder Number:  " & rs!FOLDER_NUM
    Line_2 = "Requestor Cdsid:  " & rs!REQUESTOR_CDSID
    'If I comment this loop out the code works fine except I don't get the data I am looking for...line_3-line_7.
    Do While Not rs.EOF
        Line_3 = "Line Item:  " & rs!LINE_ITEM
        Line_4 = "Part Number:  " & rs!PART_NUM
        Line_5 = "Received Date:  " & rs!RECVD_DATE
        Line_6 = "Qty Received:  " & rs!QTY
        Line_7 = "Comments:  " & rs!COMMENTS
        strParagraph = strParagraph & Line_3 & vbCr & Line_4 & vbCr & Line_5 & vbCr & Line_6 & vbCr & Line_7 & vbCr
    Loop
   
    DoCmd.Hourglass False
    DoCmd.SendObject , , , rs!REQUESTOR_CDSID & "@abc.com", Forms!F_LOGIN!CDSID_LOGIN & "@abc.com", , _
        "Receiving Data...", _
        Line_1 & vbCr & _
        Line_2 & vbCr & vbCr & strParagraph, True
       
Else
    MsgBox "No receiving data for the dates selected." & vbCr & vbCr & "Please try another date range.", vbExclamation, "No Receiving Data Available"
   
End If

'Remove the meter at the bottom of screen
   ReturnValue = SysCmd(SYSCMD_REMOVEMETER)
   
DoCmd.Hourglass False

Exit_ReqEmail:
    Exit Sub

ErrorHandler:
    DoCmd.Hourglass False
    'Remove the meter at the bottom of screen
    ReturnValue = SysCmd(SYSCMD_REMOVEMETER)
    MsgBox Err.Description
    Resume Exit_ReqEmail
   
End Sub
woodan1Asked:
Who is Participating?
 
will_scarlet7Connect With a Mentor Commented:
If you want to send one email with the data from all the records in the loop try it like this:
**************************************
Option Compare Database

Public Sub ReqEmail()
On Error GoTo ErrorHandler

Dim RecordCounter As Integer
Dim ReturnValue As Variant
Dim MaxRecords As Integer
Dim myCDSID As String
Dim Line_1 As String, Line_2 As String, Line_3 As String, Line_4 As String, Line_5 As String, Line_6 As String, Line_7 As String, strParagraph As String
Dim strEmail As String, rs As DAO.Recordset

DoCmd.Hourglass True

strEmail = "SELECT T_ORDER_INFO.FOLDER_NUM, T_REQ_LINE_ITEM.LINE_ITEM, T_REQ_LINE_ITEM.PART_NUM, T_RECEIVING.RECVD_DATE, " & _
        "T_RECEIVING.QTY, T_RECEIVING.COMMENTS, T_ORDER_INFO.REQUESTOR_CDSID " & _
        "FROM ((T_ORDER_INFO INNER JOIN T_REQUISITION_INFO ON T_ORDER_INFO.ID = T_REQUISITION_INFO.REF_ID) INNER JOIN " & _
        "T_REQ_LINE_ITEM ON T_REQUISITION_INFO.ID = T_REQ_LINE_ITEM.REF_ID) INNER JOIN T_RECEIVING ON " & _
        "T_REQ_LINE_ITEM.ID = T_RECEIVING.REF_ID " & _
        "WHERE (((T_ORDER_INFO.FOLDER_NUM)='" & [Forms]![F_REC_IMPOUND_MAIN]![FOLDER_NUM] & "') AND ((T_RECEIVING.RECVD_DATE) Between #" & _
        [Forms]![F_REC_IMPOUND_MAIN]![F_LINE_ITEM-SUB].[Form]![START_DATE] & "# And #" & [Forms]![F_REC_IMPOUND_MAIN]![F_LINE_ITEM-SUB].[Form]![END_DATE] & "#));"

Set rs = CurrentDb.OpenRecordset(strEmail, dbOpenSnapshot)
strParagraph = ""

RecordCounter = 1
MaxRecords = 29
MaxRecords = rs.RecordCount

'Initialize display message in status bar.
ReturnValue = SysCmd(SYSCMD_INITMETER, "Attaching Records to Email", MaxRecords)

If rs.RecordCount > 0 Then
    Do While Not rs.EOF
        myCDSID = rs!REQUESTOR_CDSID
        Line_1 = "Folder Number:  " & rs!FOLDER_NUM
        Line_2 = "Requestor Cdsid:  " & rs!REQUESTOR_CDSID
        Line_3 = "Line Item:  " & rs!LINE_ITEM
        Line_4 = "Part Number:  " & rs!PART_NUM
        Line_5 = "Received Date:  " & rs!RECVD_DATE
        Line_6 = "Qty Received:  " & rs!QTY
        Line_7 = "Comments:  " & rs!COMMENTS
        strParagraph = strParagraph & Line_3 & vbCr & Line_4 & vbCr & Line_5 & vbCr & Line_6 & vbCr & Line_7 & vbCr
        rs.MoveNext
        RecordCounter = RecordCounter + 1
        ReturnValue = SysCmd(SYSCMD_UPDATEMETER, RecordCounter)   ' Update meter.
    Loop
   
    DoCmd.Hourglass False
    DoCmd.SendObject , , , myCDSID & "@abc.com", Forms!F_LOGIN!CDSID_LOGIN & "@abc.com", , _
        "Receiving Data...", Line_1 & vbCr & Line_2 & vbCr & vbCr & strParagraph, True
       
Else
    MsgBox "No receiving data for the dates selected." & vbCr & vbCr & "Please try another date range.", vbExclamation, "No Receiving Data Available"
   
End If

'Remove the meter at the bottom of screen
   ReturnValue = SysCmd(SYSCMD_REMOVEMETER)
   
DoCmd.Hourglass False

Exit_ReqEmail:
    Exit Sub

ErrorHandler:
    DoCmd.Hourglass False
    'Remove the meter at the bottom of screen
    ReturnValue = SysCmd(SYSCMD_REMOVEMETER)
    MsgBox Err.Description
    Resume Exit_ReqEmail
   
End Sub

**************************************
Notice I left the DoCmd.SendObject outside the loop, but changed it's refeence to a variable that was being populated inside the loop. If it still gives you the "No Current Record" error please note in your next post  what line gives the error.

God bless!
0
 
knollbertConnect With a Mentor Commented:
.
    Do While Not rs.EOF
        Line_3 = "Line Item:  " & rs!LINE_ITEM
        Line_4 = "Part Number:  " & rs!PART_NUM
        Line_5 = "Received Date:  " & rs!RECVD_DATE
        Line_6 = "Qty Received:  " & rs!QTY
        Line_7 = "Comments:  " & rs!COMMENTS
        strParagraph = strParagraph & Line_3 & vbCr & Line_4 & vbCr & Line_5 & vbCr & Line_6 & vbCr & Line_7 & vbCr
        rs.movenext   ''''''''''''''''''''''''''''''''''''''''''''''
    Loop
0
 
woodan1Author Commented:
I added the rs.movenext, but now I am getting a "No current record" message.  I comment the do.. and loop lines and it works for record 1, so it is not that I don't have any records.

Here's the updated code:

Public Sub ReqEmail()
On Error GoTo ErrorHandler

Dim RecordCounter As Integer
Dim ReturnValue As Variant
Dim MaxRecords As Integer
Dim Line_1 As String, Line_2 As String, Line_3 As String, Line_4 As String, Line_5 As String, Line_6 As String, Line_7 As String, strParagraph As String
Dim strEmail As String, rs As DAO.Recordset

DoCmd.Hourglass True

strEmail = "SELECT T_ORDER_INFO.FOLDER_NUM, T_REQ_LINE_ITEM.LINE_ITEM, T_REQ_LINE_ITEM.PART_NUM, T_RECEIVING.RECVD_DATE, " & _
        "T_RECEIVING.QTY, T_RECEIVING.COMMENTS, T_ORDER_INFO.REQUESTOR_CDSID " & _
        "FROM ((T_ORDER_INFO INNER JOIN T_REQUISITION_INFO ON T_ORDER_INFO.ID = T_REQUISITION_INFO.REF_ID) INNER JOIN " & _
        "T_REQ_LINE_ITEM ON T_REQUISITION_INFO.ID = T_REQ_LINE_ITEM.REF_ID) INNER JOIN T_RECEIVING ON " & _
        "T_REQ_LINE_ITEM.ID = T_RECEIVING.REF_ID " & _
        "WHERE (((T_ORDER_INFO.FOLDER_NUM)='" & [Forms]![F_REC_IMPOUND_MAIN]![FOLDER_NUM] & "') AND ((T_RECEIVING.RECVD_DATE) Between #" & _
        [Forms]![F_REC_IMPOUND_MAIN]![F_LINE_ITEM-SUB].[Form]![START_DATE] & "# And #" & [Forms]![F_REC_IMPOUND_MAIN]![F_LINE_ITEM-SUB].[Form]![END_DATE] & "#));"

Set rs = CurrentDb.OpenRecordset(strEmail, dbOpenSnapshot)
strParagraph = ""

RecordCounter = 1
MaxRecords = 29
MaxRecords = rs.RecordCount

'Initialize display message in status bar.
ReturnValue = SysCmd(SYSCMD_INITMETER, "Attaching Records to Email", MaxRecords)

If rs.RecordCount > 0 Then
    Do While Not rs.EOF
        Line_1 = "Folder Number:  " & rs!FOLDER_NUM
        Line_2 = "Requestor Cdsid:  " & rs!REQUESTOR_CDSID
        Line_3 = "Line Item:  " & rs!LINE_ITEM
        Line_4 = "Part Number:  " & rs!PART_NUM
        Line_5 = "Received Date:  " & rs!RECVD_DATE
        Line_6 = "Qty Received:  " & rs!QTY
        Line_7 = "Comments:  " & rs!COMMENTS
        strParagraph = strParagraph & Line_3 & vbCr & Line_4 & vbCr & Line_5 & vbCr & Line_6 & vbCr & Line_7 & vbCr
        rs.MoveNext
        RecordCounter = RecordCounter + 1
        ReturnValue = SysCmd(SYSCMD_UPDATEMETER, RecordCounter)   ' Update meter.
    Loop
   
    DoCmd.Hourglass False
    DoCmd.SendObject , , , rs!REQUESTOR_CDSID & "@abc.com", Forms!F_LOGIN!CDSID_LOGIN & "@abc.com", , _
        "Receiving Data...", Line_1 & vbCr & Line_2 & vbCr & vbCr & strParagraph, True
       
Else
    MsgBox "No receiving data for the dates selected." & vbCr & vbCr & "Please try another date range.", vbExclamation, "No Receiving Data Available"
   
End If

'Remove the meter at the bottom of screen
   ReturnValue = SysCmd(SYSCMD_REMOVEMETER)
   
DoCmd.Hourglass False

Exit_ReqEmail:
    Exit Sub

ErrorHandler:
    DoCmd.Hourglass False
    'Remove the meter at the bottom of screen
    ReturnValue = SysCmd(SYSCMD_REMOVEMETER)
    MsgBox Err.Description
    Resume Exit_ReqEmail
   
End Sub
0
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

 
will_scarlet7Commented:
It could be because of this line:

  DoCmd.SendObject , , , rs!REQUESTOR_CDSID & "@abc.com", Forms!F_LOGIN!CDSID_LOGIN & "@abc.com", , _
        "Receiving Data...", Line_1 & vbCr & Line_2 & vbCr & vbCr & strParagraph, True

You have this placed after the "Do While Not rs.EOF" loop so when it gets there rs does not have a current record to get the rs!REQUESTOR_CDSID from.
0
 
woodan1Author Commented:
Sort of...I moved it into the loop and now it creates an email on the first loop (with just one record listed) and then "No current record" message.  For the test case I am running, I know there are two records.
0
 
woodan1Author Commented:
Makes sense now.  Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.