• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 253
  • Last Modified:

Using email from a form

I am sending an email from a sub-sub form. This part of the code works on one computer but not the other computer. Is there a better way to call that field? The parent form is titled MainForm

objEmailMessage.To = Me.Parent.Parent.[Email]

Thanks!
0
cansevin
Asked:
cansevin
  • 4
  • 2
  • 2
  • +1
1 Solution
 
Rey Obrero (Capricorn1)Commented:
try

objEmailMessage.To = Forms!MainForm.[Email]
0
 
Nick67Commented:
Unless you reuse the subforms in many places (doubtful!) this is one time I wouldn't torture yourself about hard-coding a name.

objEmailMessage.To = Forms!MainForm![Email]

should be just fine
0
 
Rey Obrero (Capricorn1)Commented:
it is better to name control different from the name of the field it is bound to.
i.e., change the control Email to txtEmail

objEmailMessage.To = Forms!MainForm.txtEmail
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
Nick67Commented:
@Rey
Why?
The default for all bound controls is the fieldname.
I have never understood why anyone goes to the scutwork of renaming everything.
Was it a problem pre-Access 2003/
It never has been a problem A2003+
0
 
cansevinAuthor Commented:
Still not working. Any ideas?

I've tried:

        objEmailMessage.To = Forms!MainForm.[Email]

and
objEmailMessage.To = Forms!MainForm.txtEmail


The who code is:

Set objOutlook = CreateObject("Outlook.Application")
        Set objEmailMessage = objOutlook.CreateItem(olMailItem)
        objEmailMessage.To = Forms!MainForm.txtEmail
        objEmailMessage.Subject = sSubj
        objEmailMessage.htmlBody = sBody & vbCrLf & vbCrLf
        objEmailMessage.Display
        objEmailMessage.Attachments.Add "C:\Invoices\CustomerInvoices.pdf"

Open in new window

0
 
Nick67Commented:
First things first
What contains the email address?
A control on a form named MainForm?

Throw in a MsgBox and let's see
If It is a control, then

MsgBox Forms!MainForm!NameOfTheControlWithTheEmailInIt

Subsititute in the control's name

That should toss back the email address.
Does it?
0
 
cansevinAuthor Commented:
Sorry guys... didn't have an email in the box. The first answer solved it.
0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
It never has been a problem A2003+
That is not correct. I worked on a project last week in 2010 that refused to see a textbox until I changed the name to txCustName. Prior to that - when it was named the same as the Field - the code would fail reliably. The machine was fully updated for both Windows and Office, but it still would not see the field on the form.

I recreated the form with just a handful of fields, and tried again - same result.

I based the form on a different table, and tried again - same result.

I change the name from "CustName" to "txCustName" and it worked.

Note this was not an "upgraded" project. This was one created from scratch in Access 2010, in the .accdb format.
0
 
Nick67Commented:
Weird.
I've never seen that yet, and I must have well over a thousand bound controls where the fieldname and controlname are the same running for the last 10 years in A2003+ without ever encountering that.  Was it run-of-the-mill code and a control bound to a DAO object's field?

Note that I only use the !bang operator where it is required  -- in other word, places where .dot notation doesn't work (Forms!SomeForm!SomeControl, but never Me!Anything etc)
0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Yes, it was vanilla code in a DLookup statement, and the object was a standard Textbox bound to an underlying field in a typical form. The author had not renamed the controls, and the code simply refused to see the value. I mucked around for a bit, removed and replaced the textbox, etc etc - and finally changed the control name and the code began to work. I actually changed it back to see if it would fail, and it did so immediately.

I had the same issue occur in a 2007 application about a year ago (which is what prompted me to changed the name). Same scenario, except that code was trying to open a DAO Recordset filtered on a value being pulled from a form.

Of course, like you I've seen innumerable instances where the Control name is exactly the same as the bound Field name, and it works flawlessly. In the case of the 2007 issue I spent a considerable amount of time insuring the database was "clean" (i.e. compact/decompile/import to new, etc etc) and that the environment was "clean" (i.e. all updates, rebooted, only running 2007, etc). Nothing helped. I moved the 2007 database to my own machine, and it failed there in 2007, 2010, and 2013.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now