使用访问表单中的信息填充电子邮件,如何在电子邮件上显示所选的引用表选项,而不是主键编号

populate an email with information from an access form, how to have the referrenced tables selected option show on email, not primary key number

我在表单中创建了一个按钮,该按钮将用特定的文件填充电子邮件。其中一些字段是从另一个 table 获取信息的下拉框。在此示例中,我有一个标题字段(先生、夫人等),表单显示先生,但是当将其写入电子邮件时,主键会代替实际标题。 “1”而不是“先生”。

我试过使用看起来合乎逻辑的方法从“TitleTBL”(table 的名称)获取信息,但我似乎没有得到正确的结果。请查看下面的 VBA 代码,让我知道如何引用实际标题而不是该选项的主键。

谢谢

Private Sub TransferDanR_Click()

    Dim ID As Variant
    Dim Title As Variant
    Dim First As Variant
    Dim Last As Variant
    Dim Addr1 As Variant
    Dim Addr2 As Variant
    Dim Postcode As Variant
    Dim HomePhone As Variant
    Dim MobilePhone As Variant
    Dim Insurer As Variant
    Dim RenewalDate As Variant
    Dim PolicyNotes As Variant
    Dim ContactNotes As Variant
    Dim LGAgent As Variant
    Dim objOutlook As Object
    Dim objEmail As Object
    
    ID = Forms("DataToDialFRM").ID
    Title = Forms("DataToDialFRM").Title
    First = Forms("DataToDialFRM").First
    Last = Forms("DataToDialFRM").Last
    Addr1 = Forms("DataToDialFRM").Addr1
    Addr2 = Forms("DataToDialFRM").Addr2
    Postcode = Forms("DataToDialFRM").Postcode
    HomePhone = Forms("DataToDialFRM").HomePhone
    MobilePhone = Forms("DataToDialFRM").MobilePhone
    Insurer = Forms("DataToDialFRM").Insurer
    RenewalDate = Forms("DataToDialFRM").RenewalDate
    PolicyNotes = Forms("DataToDialFRM").PolicyNotes
    ContactNotes = Forms("DataToDialFRM").ContactNotes
    LGAgent = Forms("DataToDialFRM").LGAgent
    
    Set objOutlook = CreateObject("Outlook.Application")
    Set objEmail = objOutlook.CreateItem(0)
    
    With objEmail
    .To = "emailaddress; emailaddress; emailaddress"
    .Subject = ID & " " & Last & " from " & LGAgent & " (Automated Transfer Email)"
    .HTMLBody = "<p>" & "Name: " & Title & ", " & First & ", " & Last & "<p>" & "Address: " & Addr1 & ", " & Addr2 & ", " & Postcode & "<p>" & "HomePhone: " & HomePhone & "<p>" & "MobilePhone: " & MobilePhone & "<p>" & "Insurer " & Insurer & "<p>" & "Renewal Date: " & RenewalDate & "<p>" & "Policy Notes: " & "<p>" & PolicyNotes & "<p>" & "Contact Notes: " & "<p>" & ContactNotes
    .Display
    
    End With
    
    Set objEmail = Nothing
    Set objOutlook = Nothing
    
End Sub

要检索组合框的显示值,您可以使用 .Column 属性。

例如如果您的 Title 组合框有 2 列,标题 ID ("1") 和标题 ("Mr."),您需要 .Column(1),因为列索引是 zero-bound.

您可以使用 With 语句使您的代码更易读:

With Forms("DataToDialFRM")
    ID = !ID
    Title = !Title.Column(1)
    First = !First
    ' ...
End With

使用 bang ! 来引用表单控件,而不是表单记录源值。