CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2012
    Location
    Lubbock, TX
    Posts
    15

    [RESOLVED] Attaching Sub Form to SMTP Message

    I'm having a problem attaching a subform to an e-mail message. Before, I was sending the e-mail through outlook using DoCmd, but recently re-wrote the code to send through an SMTP server in order to avoid the annoying warning message. I want to attach a subform located with in the same database, called "5-Day Ticket Notification Subform", but I keep getting Runtime Error 438. I'm not sure if using .Attachments.Add is causing the error, or what. I've searched forums trying to find a solution, but have had no luck. Any help y'all can give will be much appreciated!
    Code:
     ' Sends the e-mail
        With Msg
            Set .Configuration = Conf
            .To = Address
            .CC = ""
            .BCC = ""
            .From = """Name"" <e-mail address>"
            .Subject = "5-Day Notification"
            .Attachments.Add Forms![5-Day Ticket Notification Subform]
            .TextBody = StringBody
            .Send
        End With

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Attaching Sub Form to SMTP Message

    Format the .From address, and then pass it to MSG class/form BEFORE you call it.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Aug 2012
    Location
    Lubbock, TX
    Posts
    15

    Re: Attaching Sub Form to SMTP Message

    Ok, so I've done that, but now it's telling me that it can't find the form. I've checked the name many times, and copy and pasted the name of the form and subform into my vba code, so I am SURE there is no typo.
    Code:
     ' Declare module-level variables
        Dim Msg As Object
        Dim Conf As Object
        Dim StringBody As String
        Dim Flds As Variant
        Dim Address As Field
        Dim Name As Field
        Dim a As Control
        
        ' Assign object references
        Set Msg = CreateObject("CDO.Message")
        Set Conf = CreateObject("CDO.Configuration")
        Set Address = r.Fields("E-Mail Address")
        Set Name = r.Fields("First Name")
        Set Flds = Conf.Fields
        Set a = Forms![5-Day Ticket Notification]![5-Day Ticket Notification Subform]
    I'm getting "Run Time error '2465': Field Ticket Tracking Database can't find the field '5-Day Ticket Notification Subform' referred to in your expression."
    Notice, it says "can't find the FIELD...", so I'm wondering if I haven't referred to the form/subform properly? If I put quotes around the form and subform name in the vba code, it returns the same error, but says that it "can't find the form..."

    Either way, I'm getting the same error code.

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Attaching Sub Form to SMTP Message

    Shoudln't that be a period between the form and sub form rather than a ! ?
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Aug 2012
    Location
    Lubbock, TX
    Posts
    15

    Re: Attaching Sub Form to SMTP Message

    Tried that, and I'm still getting the same error code, only it says "can't find the field 'l'..."

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Attaching Sub Form to SMTP Message

    Can't really call an ACCESS form in a VB app. Should be accessing the DATA from the database directly.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    Aug 2012
    Location
    Lubbock, TX
    Posts
    15

    Re: Attaching Sub Form to SMTP Message

    I figured out a solution. When the user clicks the button to send the e-mail, the subform is automatically exported and later attached to the e-mail message in the "With msg" loop. It works like a charm
    Thanks for y'all's help!

    P.S.
    When sending attachments using an SMTP server (CDO e-mail), you have to use .AddAttachment (FileName) INSTEAD of .Attachments.Add (FileName).
    .Attachments.Add is used for Outlook messages. I found this out the hard way (many many hours of forum surfing, and reading up on VBA language specifications), so I'll pass this knowledge on to anyone who might be experiencing a similar problem in hopes to save them some valuable time.
    Last edited by Chad Jackson; January 3rd, 2013 at 09:56 AM. Reason: Typo

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured