Solved

query based email

Posted on 2004-08-18
6
407 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

821 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