Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    malamute20 is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    22

    VBA Send Email using DoCmd.SendObject

    I'm having trouble getting this to work as it should, and maybe there's a better way I don't know about. I'm trying to write a VBA script to send an email out when a "Submit" command box is clicked on a form. I have this in the OnClick event.



    Code:
    Private Sub Submit_Click()
    DoCmd.SendObject acSendForm, Me.[Entry Log], acFormatXLSX, "various.company_personnel@company.com", "", "", "Incident Report", "", True
     
    End Sub
    Help is very welcome.
    Last edited by malamute20; 08-01-2011 at 10:57 AM. Reason: Mark solved.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Welcome to the site. It usually helps us if you describe the problem. "I'm having trouble getting this to work as it should" doesn't tell us what the problem is. Are you getting an error? If so, what is it? Are you getting an unexpected result?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    malamute20 is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    22
    Yes, sorry.

    RunTime error '2465': Microsoft Access cannot find the field '|1' referred to in your expression.

    Then I get an opportunity to debug and the entire line of code is highlighted.

    I'm running MSA 2010

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    What does Me.[Entry Log] contain? Access is expecting the name of a form, since that's what you specified you were sending.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    malamute20 is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    22
    Entry Log is the name of the form. The form contains various fields to be filled out prior to submitting.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Access is trying to find a control (textbox, etc) on the form named [Entry Log]. Try Me.Name or "[Entry Log]"
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    malamute20 is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    22
    Ok. Using "[Entry Log]" I now get:

    Run-time error '2102':

    The form name '[Entry Log]' is misspelled (it's not) or refers to a form that doesn't exist. (It is the active form.)

    An option to debug is given, it still highlights the entire string of script.

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Try without the brackets. The space in the name isn't a good idea; normally it requires brackets, but not always. This just worked in a test:

    DoCmd.SendObject acSendForm, "FormName", acFormatXLS, "", "", "", "Incident Report", "", True
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    malamute20 is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    22
    Thank you, that works.

    One little thing: is there any way to prevent an error from popping when I close out the email without sending? As it is it pops up with an error indicating the "SendObject action was cancelled." And prompts for the debugger.

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You have to trap for the error (I think it's 2501). Here's a primer on that if you're not familiar:

    http://www.baldyweb.com/ErrorTrap.htm

    In this case, you probably wouldn't want a message box for that error.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    malamute20 is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    22
    Sweet. Marking solved- Thanks a bunch.

  12. #12
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No problem, glad we got it sorted out for you.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  13. #13
    Tomfernandez1 is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Feb 2011
    Posts
    74
    The information on this thread is great! As a follow-up, say, I filtered a form that results in 4 records and 4 different email addresses. How do I adjust the code to show all 4 email address in the "To:" section of the email message?

    Tommy

  14. #14
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You could open a recordset on the recordset clone of the form, though I'm not sure how that would work if you had a applied a filter or something. You might need to open the recordset on the source of the form using the same criteria as the filter. Then you loop through the recordset and build a string of delimited email addresses.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  15. #15
    Tomfernandez1 is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Feb 2011
    Posts
    74
    I am not sure how to open a recordset. Where do I start this process? What information do you need to help me get started?

    Thanks a bunch!!

    Tommy

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 11-17-2010, 11:24 AM
  2. DoCmd.SendObject Question
    By HunterEngineeringCoop in forum Programming
    Replies: 3
    Last Post: 10-04-2010, 01:42 PM
  3. Email sent via DoCmd.SendObject
    By silverback in forum Programming
    Replies: 0
    Last Post: 10-29-2009, 06:26 AM
  4. Send email using SendObject instruction
    By stecco in forum Access
    Replies: 4
    Last Post: 09-09-2009, 01:55 AM
  5. DoCmd.SendObject Help
    By bgreer5050 in forum Programming
    Replies: 0
    Last Post: 01-12-2007, 06:27 PM

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
  •  
Other Forums: Microsoft Office Forums