MSAccess 将表单中的字段名称引用到查询和电子邮件中
MSAccess reference a field name from a form into a query and email
我有一个 table "emailtocustomer",有 3 个字段,一个 ID,type_id(与类型 table(数字 1-6)中的 typeid 的关系),主题(键入文本)和正文(键入备忘录),以便我或其他用户在需要时可以轻松更改文本或主题。此 table 引用要发送给客户的电子邮件。
预期的设计是应用程序的用户将使用客户的电子邮件、联系人姓名和公司名称生成一个参考号(在本例中为 RMA 票号),然后单击表格上的按钮生成 RMA 编号是为了向客户生成一封电子邮件,其中包含有关如何 return 他们的产品的说明。
Access 然后将信息转储到电子邮件 window。我在按钮的 "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 的 "type",这是生成 RMA 的原因,一个数字 ID 号 (me.type)。用户还填写联系人姓名 (me.contactfirst)、联系人电子邮件 (me.contactemail),应用程序生成 RMA 编号 (me.rmanbr)
emailtocustomer.emailbody 包含要发送的电子邮件的正文。它和我需要的 emailtocustomer.email_subject 字段包含对 "New RMA" 表单中字段 'contactfirst'、'companyname' 和 'rmanbr' 的引用。
以下是来自 emailtocustomer table 的示例摘录:
email_subject:
RMA Information for RMA# R" & forms![New RMA]![rmanbr] & "
电子邮件正文:
<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] & "
中,我需要将适当的字段转移到此电子邮件文本中。无论如何要做到这一点?我试过指定 "Me.",但似乎也不行。
感谢大家的帮助。
考虑在 table 的 EmailBody 字段中使用占位符 %...%
。注意:百分比没有任何意义,可以使用任何其他标记、特殊字符或 none。
<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
...
不起作用,因为对表单控件的引用只是一串字符。尝试用一些无意义的字符串代替 form/control 引用,例如 zzzzzzzzz。然后在 VBA 代码中对字符串进行 Replace() 操作:
Replace(strBody, "zzzzzzzzz", Me.contactfirst)
好吧,伟大的思想...我正在作曲时发布了 Parfait。
我有一个 table "emailtocustomer",有 3 个字段,一个 ID,type_id(与类型 table(数字 1-6)中的 typeid 的关系),主题(键入文本)和正文(键入备忘录),以便我或其他用户在需要时可以轻松更改文本或主题。此 table 引用要发送给客户的电子邮件。
预期的设计是应用程序的用户将使用客户的电子邮件、联系人姓名和公司名称生成一个参考号(在本例中为 RMA 票号),然后单击表格上的按钮生成 RMA 编号是为了向客户生成一封电子邮件,其中包含有关如何 return 他们的产品的说明。
Access 然后将信息转储到电子邮件 window。我在按钮的 "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 的 "type",这是生成 RMA 的原因,一个数字 ID 号 (me.type)。用户还填写联系人姓名 (me.contactfirst)、联系人电子邮件 (me.contactemail),应用程序生成 RMA 编号 (me.rmanbr)
emailtocustomer.emailbody 包含要发送的电子邮件的正文。它和我需要的 emailtocustomer.email_subject 字段包含对 "New RMA" 表单中字段 'contactfirst'、'companyname' 和 'rmanbr' 的引用。
以下是来自 emailtocustomer table 的示例摘录:
email_subject:
RMA Information for RMA# R" & forms![New RMA]![rmanbr] & "
电子邮件正文:
<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] & "
中,我需要将适当的字段转移到此电子邮件文本中。无论如何要做到这一点?我试过指定 "Me.",但似乎也不行。
感谢大家的帮助。
考虑在 table 的 EmailBody 字段中使用占位符 %...%
。注意:百分比没有任何意义,可以使用任何其他标记、特殊字符或 none。
<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
...
不起作用,因为对表单控件的引用只是一串字符。尝试用一些无意义的字符串代替 form/control 引用,例如 zzzzzzzzz。然后在 VBA 代码中对字符串进行 Replace() 操作:
Replace(strBody, "zzzzzzzzz", Me.contactfirst)
好吧,伟大的思想...我正在作曲时发布了 Parfait。