MSAccess将表单中的字段名称引用到查询和电子邮件中

时间:2017-05-18 16:58:25

标签: vba ms-access access-vba ms-access-2010

我有一张桌子" emailtocustomer"有3个字段,一个ID,type_id(与类型表中的typeid的关系(编号1-6)),主题(类型文本)和正文(类型备忘录),这样我或其他用户可以轻松更改文本或主题,如果我需要继续前进。此表引用要发送给客户的电子邮件。

预期的设计是应用程序的用户将使用客户的电子邮件,联系人姓名和公司名称生成参考号(在本例中为RMA票号),然后单击表单上的按钮。生成RMA号码以生成电子邮件给客户,并附有如何退回产品的说明。

Access然后将信息转储到电子邮件窗口。我在" OnClick"中有以下VBA代码。生成电子邮件的按钮:

' Generate Email to customer
Dim EmailTo, Subject, EmailBody, strSQL As String
Dim RMAType As Integer
Dim olApp As Object
Dim objMail As Object
Dim rs As DAO.Recordset
Dim db As Database

If IsNull(Me.contactemail) Then
    MsgBox "You cannot generate an email for this RMA - there is no email address!"
    Exit Sub
End If
Set olApp = CreateObject("Outlook.Application")
Set objMail = olApp.CreateItem(0)

EmailTo = Me.contactemail

RMAType = Me.type

Set db = CurrentDb

strSQL = "SELECT * FROM emailtocustomer WHERE type_id = " & RMAType & ""

Set rs = db.OpenRecordset(strSQL)

Subject = rs!email_subject
EmailBody = rs!EmailBody

With objMail
    '.BodyFormat = olFormatHTML
    .To = EmailTo
    .Subject = Subject
    .HTMLBody = EmailBody
    .Display
End With

Set rs = Nothing
Set db = Nothing

在表单上,​​用户选择"键入" RMA的原因是生成RMA的原因,是一位数的ID号(me.type)。用户还填写联系人姓名(me.contactfirst),联系电子邮件(me.contactemail),应用程序生成RMA号码(me.rmanbr)

emailtocustomer.emailbody包含要发送的电子邮件正文。它和emailtocustomer.email_subject字段我需要包含对字段" contactfirst',' companyname'和' rmanbr'的引用。来自"新RMA"形式。

以下是来自emailtocustomer表的示例摘录:

EMAIL_SUBJECT:

RMA Information for RMA# R" & forms![New RMA]![rmanbr] & "

emailbody:

<html>
<head><body style='font-family: tahoma'></head>
<body>Hello " & forms![New RMA]![contactfirst] & ",<br /><br />
Thank you for requesting an RMA from us. For quick and 
consistent communications regarding this RMA, please reply directly to 
this email, and leave the subject line intact.<br /><br />
Your RMA Number is R" & forms![New RMA]![rmanbr] & ". This RMA number should 
be <u>marked on the outside of your package(s)</u>. Packages received 
without an RMA number may be returned to the sender at their expense.<br />
<br />

在占位符" & forms![New RMA]![contactfirst] & "中,我需要相应的字段来继承此电子邮件文本。反正有没有完成这个?我已经尝试过这个名称&#34;我。&#34;但这似乎也没有。

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

请考虑在表格的 EmailBody 字段中使用占位符%...%。注意:百分比没有任何意义,可以使用任何其他标记,特殊字符或根本不使用。

<html>
<head><body style='font-family: tahoma'></head>
<body>Hello %ContactFirst%,<br /><br />
Thank you for requesting an RMA from us. For quick and 
consistent communications regarding this RMA, please reply directly to 
this email, and leave the subject line intact.<br /><br />
Your RMA Number is R %RMAnbr%. This RMA number should 
be <u>marked on the outside of your package(s)</u>. Packages received 
without an RMA number may be returned to the sender at their expense.<br />
<br />

然后,在开发电子邮件时,使用Replace

将占位符替换为表单值
...
Subject = "RMA Information for RMA# R" & forms![New RMA]![rmanbr]

EmailBody = Replace(rs!EmailBody, "%ContactFirst%", forms![New RMA]![contactfirst])    
EmailBody = Replace(rs!EmailBody, "%RMAnbr%", forms![New RMA]![rmanbr])

With objMail
    '.BodyFormat = olFormatHTML
    .To = EmailTo
    .Subject = Subject
    .HTMLBody = EmailBody
    .Display
End With 
...

答案 1 :(得分:1)

不起作用,因为对表单控件的引用只是一串字符。尝试使用一些无意义的字符串代替表单/控件引用,例如zzzzzzzzz。然后在VBA代码中对字符串执行Replace()操作:

Replace(strBody, "zzzzzzzzz", Me.contactfirst)

好的,很棒的头脑...... Parfait在我写作时发布了。