Solved

query based email

Posted on 2004-08-18
6
403 Views
Last Modified: 2008-10-15
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
0
Comment
Question by:woodan1
  • 3
  • 2
6 Comments
 
LVL 6

Assisted Solution

by:knollbert
knollbert earned 25 total points
ID: 11835181
.
    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
 

Author Comment

by:woodan1
ID: 11835472
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
 
LVL 15

Expert Comment

by:will_scarlet7
ID: 11835561
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:woodan1
ID: 11835749
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
 
LVL 15

Accepted Solution

by:
will_scarlet7 earned 75 total points
ID: 11836040
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
 

Author Comment

by:woodan1
ID: 11836501
Makes sense now.  Thanks.
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now