By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,732 Members | 1,435 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,732 IT Pros & Developers. It's quick & easy.

sending email to selected contacts

P: n/a
I have the following peice of code to try and send an email to selected
contacts in my form.The problem is this line:

StrStore = DLookup("[email]", "qrySelectEmail", "??????")

This looks up the email field in the records returned for the query
qrySelectEmail. The final bit needs to tell it to go to the first record
the first time the loop runs, then the second record the second time the
loop runs etc… But I cannot figure out a way to do this, which I am
certain there must be.

Thanks in anticipation
Code:
--------------------------------------------------------------------------------

Private Sub send_email_Click()On Error GoTo Err_send_email_ClickDim StrTo
As StringDim StrBcc As StringDim StrStore As StringDim NumSelected As
IntegerDim NumCurrentRecord As IntegerDim NumTotalRecords As Integer' Set
StrTo and StrBcc to emptyStrTo = ""StrBcc = ""' Check there are records
selected "NumSelected = DCount("[selected]", "qrySelectEmail", "[selected]
= TRUE")'the qryselectemail query only returns records selected and with
email adddress enteredIf NumSelected > 0 Then' Count total records
selected so that loop will stop at last recordNumTotalRecords =
DCount("[selected]", "qrySelectEmail", "[selected] = TRUE")' Set current
record to first recordNumCurrentRecord = 1' Loop through recordsDo Until
NumCurrentRecord > NumTotalRecords'The next line should needs to look up
query for each record in turnStrStore = DLookup("[email]",
"qrySelectEmail", "??????")' store email addressStrTo = StrTo + StrStore +
";"NumCurrentRecord = NumCurrentRecord + 1Loop DoCmd.SendObject
acSendNoObject, , , StrTo, , StrBcc, , , , FalseElse' There are no
selected records MsgBox ("No records have been selected")End
IfExit_send_email_Click: Exit SubErr_send_email_Click: MsgBox "The
email was not sent" Resume Exit_send_email_ClickEnd Sub
--------------------------------------------------------------------------------
Nov 13 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
I never use or seen example of DLookup Function used they way your
using it. But I have used it. I had to reformat copy of your code.
I'm noticing that you DLookup the store number. I think if you
selected just the store number then you could easy retrieve the email
addresses best on that particular store. If email address. changes just
change info in database through form. But use store number to pull info
you need. If that the case here then all your ducks are in order. But
that's ideal. I think ADO would be a lot faster so I am showing you
this piece of code. This doesn't answer your question. However hope
this helps.

Sub FindmeStore()
Dim mArr()
Dim mGetArr As Variant
Dim sTo As String, sBc As String, sNum As Variant

'I had nothing to test this, but this is where I would do something
similar

'known store num's in array

mArr = Array("1sd", "123")

For Each mGetArr In mArr

sNum = DLookup("[StoreNum]", "Employees", "[Selected] = True And
[StoreNum] = & mGetArr)
sBc = DLookup("[EmailManger]", "Employees", "[Selected] = True And
[StoreNum] = " & mGetArr)
sTo = DLookup("[EmployeeAddress]", "Employees", "[Selected] = True And
[StoreNum] " & mGetArr)

' DoCmd.SendObject acSendNoObject, , , sTo, , sBc, , , False
DoCmd.SendObject acSendReport, "ObjectName", , sTo, "CC", sBc,
"Store Num:" & sNum, ""
Next mGetArr

End Sub
'
'--------------------------------------------------------------------------------------------------------------------------------
Sub GetCriteria()

sql1 = "SELECT qryEmail_Addresses.Selected,
qryEmail_Addresses.EmployeeAddress ,
qryEmail_Addresses.EmailStoreMangersAddress, Email_Addresses.StoreNum"
& _
" FROM qryEmail_Addresses" & _
" Where StoreNum = """ & intStoreNum & """"

'One store at a time or if Selected = true then comment out the where
statement.
' Because of the Docmd. in the loop. I would do one store at time,
depends how many stores of course.
Set rs = New ADODB.Recordset

intStoreNum = "45D"

rs.ActiveConnection = CurrentProject.Connection
rs.LockType = adLockPessimistic
rs.CursorType = adOpenKeyset
rs.Open sql1

rs.MoveFirst

strEmpAdd = ""
strManAdd = ""

Do Until rs.EOF

If rs.Fields("Selected") = True Then

StrTo = strEmpAdd + rs.Fields("EmployeeAddress") & ";"
StrBcc = strManAdd + rs.Fields("EmailStoreMangersAddress") & ";"

'DoCmd.SendObject acSendNoObject, , , StrTo, , StrBcc, , , ,
FalseElse

'Open up immediate window see the result.

Debug.Print StrTo
Debug.Print StrBcc

End If

rs.MoveNext
Loop

' Reset Variable

StrTo = ""
StrBcc = ""

rs.Close

Set rs = Nothing
End Sub

Nov 13 '05 #2

P: n/a
I wrote the following to do something similar

Put the following in 'on click' event of button (called cmdEmailReps) on the
form (MyForm) you want to email from. Put a text box on this form called
'Text'.

You will need a table (MyTable) with the email addresses (E-mail) and an
index number(IndexNo) for each record. You may have a table with this info
already if so just modify field names in code below to suit your own.

The table also needs fields 'Address', 'Recipient'

You need a query (qMyTable) with all the fields from MyTable and
'[Forms]![fEmailReps]![text]' in the criteria for 'IndexNo' (no quotes)

This was quickly cobbled together from something that was specific to me to
produce code that is more general. I have not tested it so it may have some
errors. Play around with it until you get it working

Ian

************************************************** *************
Private Sub cmdEmailReps_Click()

Dim Counter As Integer
Dim stDocName As String
Dim Address As String
Dim Recipient As String
Dim Sender As String

PupFilter = "[IndexNo] = Forms!MyForm!text"
For Counter = CLng(DMin("[IndexNo]", "[qMyTable]")) To
CLng(DMax("[IndexNo]", "[qMyTable]"))

Forms!YourFormName!text = Counter
If Counter = DLookup("[IndexNo]", "[qMyTable]") Then

Recipient = DLookup("[Recipient]", "[qMyTable]", PupFilter)
Sender = "Your Name"

If IsNull(DLookup("[E-mail]", "[qMyTable]", PupFilter)) Then
MsgBox "You do not have an e-mail address for " & Recipient,
vbCritical, "Warning"
GoTo line1
Else: Address = DLookup("[E-Mail]", "[qMyTable], PupFilter")
End If


DoCmd.SendObject acReport, stDocName, acFormatRTF, Address, , ,
"Title", "Dear" & " " & Recipient & Chr(10) & Chr(10) & Chr(13) & "Type your
text here." & Chr(10) & Chr(10) & Chr(13) & "Regards" & Chr(10) & Chr(13) &
Sender, 0
MsgBox Recipient & "'s e-mail was sent successfully.", vbInformation
, "Information"
End If

line1:
Next

Exit_cmdEmailReps_Click:
Exit Sub

Err_cmdEmailReps_Click:
MsgBox Err.Description
Resume Exit_cmdEmailReps_Click

End Sub

************************************************** ******
"acni" <nb***@artscouncil-ni.org> wrote in message
news:f2******************************@localhost.ta lkaboutdatabases.com...
I have the following peice of code to try and send an email to selected
contacts in my form.The problem is this line:

StrStore = DLookup("[email]", "qrySelectEmail", "??????")

This looks up the email field in the records returned for the query
qrySelectEmail. The final bit needs to tell it to go to the first record
the first time the loop runs, then the second record the second time the
loop runs etc. But I cannot figure out a way to do this, which I am
certain there must be.

Thanks in anticipation
Code:
-------------------------------------------------------------------------- ------
Private Sub send_email_Click()On Error GoTo Err_send_email_ClickDim StrTo
As StringDim StrBcc As StringDim StrStore As StringDim NumSelected As
IntegerDim NumCurrentRecord As IntegerDim NumTotalRecords As Integer' Set
StrTo and StrBcc to emptyStrTo = ""StrBcc = ""' Check there are records
selected "NumSelected = DCount("[selected]", "qrySelectEmail", "[selected]
= TRUE")'the qryselectemail query only returns records selected and with
email adddress enteredIf NumSelected > 0 Then' Count total records
selected so that loop will stop at last recordNumTotalRecords =
DCount("[selected]", "qrySelectEmail", "[selected] = TRUE")' Set current
record to first recordNumCurrentRecord = 1' Loop through recordsDo Until
NumCurrentRecord > NumTotalRecords'The next line should needs to look up
query for each record in turnStrStore = DLookup("[email]",
"qrySelectEmail", "??????")' store email addressStrTo = StrTo + StrStore +
";"NumCurrentRecord = NumCurrentRecord + 1Loop DoCmd.SendObject
acSendNoObject, , , StrTo, , StrBcc, , , , FalseElse' There are no
selected records MsgBox ("No records have been selected")End
IfExit_send_email_Click: Exit SubErr_send_email_Click: MsgBox "The
email was not sent" Resume Exit_send_email_ClickEnd Sub
-------------------------------------------------------------------------- ------

Nov 13 '05 #3

P: n/a
Typo

'[Forms]![fEmailReps]![text]' in the criteria for 'IndexNo' (no quotes)
should be
'[Forms]![MyForm]![text]' in the criteria for 'IndexNo' (no quotes)
or what ever you decided to call your form

Also to make it more selective you can put dropdown lists on the form to
enable u to choose criteria. These criteria can then be fed into the query

Ian

"Ian Davies" <ia********@virgin.net> wrote in message
news:Tv***************@newsfe2-win.ntli.net... I wrote the following to do something similar

Put the following in 'on click' event of button (called cmdEmailReps) on the form (MyForm) you want to email from. Put a text box on this form called
'Text'.

You will need a table (MyTable) with the email addresses (E-mail) and an
index number(IndexNo) for each record. You may have a table with this info already if so just modify field names in code below to suit your own.

The table also needs fields 'Address', 'Recipient'

You need a query (qMyTable) with all the fields from MyTable and
'[Forms]![fEmailReps]![text]' in the criteria for 'IndexNo' (no quotes)

This was quickly cobbled together from something that was specific to me to produce code that is more general. I have not tested it so it may have some errors. Play around with it until you get it working

Ian

************************************************** *************
Private Sub cmdEmailReps_Click()

Dim Counter As Integer
Dim stDocName As String
Dim Address As String
Dim Recipient As String
Dim Sender As String

PupFilter = "[IndexNo] = Forms!MyForm!text"
For Counter = CLng(DMin("[IndexNo]", "[qMyTable]")) To
CLng(DMax("[IndexNo]", "[qMyTable]"))

Forms!YourFormName!text = Counter
If Counter = DLookup("[IndexNo]", "[qMyTable]") Then

Recipient = DLookup("[Recipient]", "[qMyTable]", PupFilter)
Sender = "Your Name"

If IsNull(DLookup("[E-mail]", "[qMyTable]", PupFilter)) Then
MsgBox "You do not have an e-mail address for " & Recipient,
vbCritical, "Warning"
GoTo line1
Else: Address = DLookup("[E-Mail]", "[qMyTable], PupFilter")
End If


DoCmd.SendObject acReport, stDocName, acFormatRTF, Address, , ,
"Title", "Dear" & " " & Recipient & Chr(10) & Chr(10) & Chr(13) & "Type your text here." & Chr(10) & Chr(10) & Chr(13) & "Regards" & Chr(10) & Chr(13) & Sender, 0
MsgBox Recipient & "'s e-mail was sent successfully.", vbInformation , "Information"
End If

line1:
Next

Exit_cmdEmailReps_Click:
Exit Sub

Err_cmdEmailReps_Click:
MsgBox Err.Description
Resume Exit_cmdEmailReps_Click

End Sub

************************************************** ******
"acni" <nb***@artscouncil-ni.org> wrote in message
news:f2******************************@localhost.ta lkaboutdatabases.com...
I have the following peice of code to try and send an email to selected
contacts in my form.The problem is this line:

StrStore = DLookup("[email]", "qrySelectEmail", "??????")

This looks up the email field in the records returned for the query
qrySelectEmail. The final bit needs to tell it to go to the first record
the first time the loop runs, then the second record the second time the
loop runs etc. But I cannot figure out a way to do this, which I am
certain there must be.

Thanks in anticipation
Code:


--------------------------------------------------------------------------
------

Private Sub send_email_Click()On Error GoTo Err_send_email_ClickDim StrTo As StringDim StrBcc As StringDim StrStore As StringDim NumSelected As
IntegerDim NumCurrentRecord As IntegerDim NumTotalRecords As Integer' Set StrTo and StrBcc to emptyStrTo = ""StrBcc = ""' Check there are records
selected "NumSelected = DCount("[selected]", "qrySelectEmail", "[selected] = TRUE")'the qryselectemail query only returns records selected and with
email adddress enteredIf NumSelected > 0 Then' Count total records
selected so that loop will stop at last recordNumTotalRecords =
DCount("[selected]", "qrySelectEmail", "[selected] = TRUE")' Set current
record to first recordNumCurrentRecord = 1' Loop through recordsDo Until
NumCurrentRecord > NumTotalRecords'The next line should needs to look up
query for each record in turnStrStore = DLookup("[email]",
"qrySelectEmail", "??????")' store email addressStrTo = StrTo + StrStore + ";"NumCurrentRecord = NumCurrentRecord + 1Loop DoCmd.SendObject
acSendNoObject, , , StrTo, , StrBcc, , , , FalseElse' There are no
selected records MsgBox ("No records have been selected")End
IfExit_send_email_Click: Exit SubErr_send_email_Click: MsgBox "The
email was not sent" Resume Exit_send_email_ClickEnd Sub


--------------------------------------------------------------------------
------


Nov 13 '05 #4

P: n/a
acni wrote:
I have the following peice of code to try and send an email to selected
contacts in my form.The problem is this line:

StrStore = DLookup("[email]", "qrySelectEmail", "??????")

This looks up the email field in the records returned for the query
qrySelectEmail. The final bit needs to tell it to go to the first record
the first time the loop runs, then the second record the second time the
loop runs etc… But I cannot figure out a way to do this, which I am
certain there must be.

Thanks in anticipation


This approach may be a little different but it works well for me and
it's easy to maintain.

I have a table of "Groups" which contains logical group names I want to
email. Then I have a table of "Recipients" that has a foreign key to
Groups, actual email addresses, and a boolean "active" value. On my form
I have a few command buttons corresponding to the groups. You may
consider populating a combobox or listbox with the group names instead
of individual command buttons, especially if you have several groups or
the group population tends to change.

The buttons fire code which runs SQL to obtain the active email addys
for the group, builds the recipient list, subject and message, and does
the DoCmd that actually creates the email.

HTH
--
Smartin
Nov 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.