Results 1 to 11 of 11
  1. #1
    cbrsix is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    215

    Send Email

    I have the following code and it is telling me that an item is not found is this collection. this is coming up on the recipient = rst![Business Email Address] line


    Code:
    Private Sub EmailRRCOA_Click()
    On Error GoTo SendEmail_Err
        Dim myOlApp As Object
        Dim myNameSpace As Object
        Dim myFolder As Object
        Dim myItem As Object
        Dim myAttachments, myRecipient As Object
        Dim recipient As String
        Dim file_name As String
        Dim mySubject As Object
        Dim dbs As Object
        Dim rst As Object
        Dim strSQL As String
        
        'Select the Query where you want your information to be drawn from
        strSQL = "ClientChangeOfAddress"
        Set dbs = CurrentDb
        Set rst = dbs.OpenRecordset(strSQL)
        rst.MoveFirst
        While Not rst.EOF
            'This is the email address that you corresponds to your recipient
            recipient = rst![Business Email Address]
            Set myOlApp = CreateObject("Outlook.Application")
            Set myItem = myOlApp.CreateItem(olMailItem)
            Set myAttachments = myItem.Attachments
            Set myRecipient = myItem.Recipients.Add(recipient)
            Set myRecipient = myItem.Recipients.Add("example@example.com")
            myRecipient.Type = olCC
            '"Message Subject String Here"
            myItem.Subject = "Client Change of Address"
            '"Put Message Body Text Here"
            myItem.Body = rst![Rep FName] & "," & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "Below is an address change we received for your client, " & rst!ClientName & "." & Chr(10) & Chr(13) & "Old Address:" & Chr(10) & "OldAddressHere" & Chr(10) & Chr(13) & "New Address:" & Chr(10) & "NewAddressHere"
            myItem.Display
            rst.MoveNext
        Wend
        DoCmd.Close acForm, "ClientChangeOfAddress" 'Closes the form
        Set myRecipient = Nothing
        Set myAttachments = Nothing
        Set myItem = Nothing
        Set myOlApp = Nothing
        Set rst = Nothing
    SendEmail_Exit:
        Exit Sub
    here is a copy of the queries recordsource
    Click image for larger version. 

Name:	Capture.jpg 
Views:	33 
Size:	38.8 KB 
ID:	12285

    I can't figure out how to fix it and/or why it's giving me this error.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,632
    Maybe:

    Set rst = dbs.OpenRecordset(strSQL, dbOpenDynaset)

    Otherwise, looks fine to me but can't remember if I have never used an aggregate query as datasource for a recordset. Will the code read any of the fields of the recordset? Try a Debug.Print before the problem line.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    I'm going to suggest:

    Set rst = dbs.OpenRecordset("ClientChangeOfAddress", dbOpenDynaset)

  4. #4
    cbrsix is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    215
    Adding the dbOpenDynaset didn't work. It came back with the same error on the [Business Email Address] line.

  5. #5
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Did you try my suggestion?

  6. #6
    cbrsix is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    215
    Yes. I tried both.

  7. #7
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Care to post your db so we can look at it? Remove any sensitive data of course. Compact and Repair and then ZIP it up before posting.

  8. #8
    cbrsix is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    215
    Let me elaborate a little further about this project of mine. I realized when looking at the code to email that it will email to everyone in the query (ClientChangeOfAddress). This is not actually what I want. I have a client address change form and the button (EmailRRCOA) on the form. What I need to happen is to send the email to the Rep for the specific client that is on the current form. I know how to open a report or form based on information using me.dirty (see below) but do not know how to add that aspect to the current code or if it's even possible with the current code. I'm thinking that I won't need the above mentioned query anymore. Any suggestions?

    Dim strWhere As String
    If Me.Dirty Then 'Save any edits
    Me.Dirty = False
    End If
    If Me.NewRecord Then 'Check these is a record to print
    MsgBox "Select a record to print"
    Else
    strWhere = "ID = " & Me.ID
    DoCmd.OpenReport "NewCOAEnvelope", acViewPreview, , strWhere
    End If

    This is for a button I have on the same form to open a report.

  9. #9
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    We're trying to get past the error you are having first. You've defined almost everything as Objects where I would have written:
    Dim dbs As DAO.Database
    Dim rst As DAO.Recordset

    I do not know if that will make any difference.

  10. #10
    cbrsix is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    215
    I tried your suggestion and it didn't make a difference. It still comes back with the same error. I mentioned the second code structure because I didn't know if that would change the thinking on my current email code.

  11. #11
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    I'm not figuring out what is causing the Collection error. All I can think of now is to play with the actual file. Sorry.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. How to send an email?
    By George in forum Access
    Replies: 6
    Last Post: 06-08-2012, 07:48 AM
  2. How can I send an email from access???
    By Asma in forum Programming
    Replies: 2
    Last Post: 12-07-2011, 07:49 AM
  3. Send a PDF via email.
    By tcheck in forum Access
    Replies: 3
    Last Post: 08-30-2011, 02:31 PM
  4. Replies: 4
    Last Post: 04-13-2011, 10:11 AM
  5. send email to email addresses in database?
    By cnstarz in forum Access
    Replies: 5
    Last Post: 03-02-2011, 09:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums