Saving text and variables in a single table record

Hope someone can give me some advice.  I am just starting to design a new application and one part of it is causing me some problems.

I want to generate and send an email from within the application.  The bones of that I have sorted out.  However, I need to generate the text of the email from some text and some variables and I want to save the standard templates in a table.

To explain, I will have a table (tblMessages) with two fields – TableName and TableText.  This will allow the setup of multiple templates to use in different situations.  A tblMessages record could look something like this:

TableName = Get details
TableText = ..
Dear [firstname]
Thankyou for showing an interest in the opportunity [opportunity] for [organisation]
Please send us additional details about yourself blah blah.

The email will be generated and sent using a button on a form.  The form will contain the necessary data to use (eg emailaddress, firstname, opportunity and organisation.

So my question is, is it possible to create the tblMessage data in such a way that it contains within it the variable placeholders.  I know that I could assemble the whole text within the code of the button, but I really want one place to define the mail template text etc.

Any suggestions would be welcome.


Who is Participating?
Public Function test1()
    Dim strResult As String
    Dim strText As String
    Dim strFirstName As String
    strFirstName = "Pat"
    strText = "Dear {FirstName}, Please review the following...."
    strResult = Replace(strText, "{FirstName}", strFirstName)
    Debug.Print strResult
End Function

Open in new window

Obviously, your code will be much more complex since you'll be reading a recordset and sending emails and you will probably have more than one variable to replace.  But this should give you a start.
Yes.  You will have to encase the variables in something that isn't commonly found in your text such as {}.  So,  You could then use the Replace() function to replace {firstname} with the first name field from your record.  If you have only a few variables, you could hard-code them.  However, if you want something flexible and expandable, you are probably looking at creating tables and crossreferences that map {firstname} to tblClient.FirstName so your code can be automated and you won't have to change it each time you find you want to add a new variable.
rltomalinAuthor Commented:
Hi Pat
Thank you for your quick reply.  I am not quite sure that I entirely understand your solution.
Do you have time to give me a short snippet of the code I would need to build the message text please?


Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Dale FyeCommented:
No points please, to add to Pat's comment.  You might want to create a table of field names and message prompts, something like below.

MsgPrompt   FieldName
{firstname}    [Name First]
{opportunity}  [Opportunity]
{organization} [Organization]

Note that I included the {} in the msgPrompt field and the [ ] in the FieldName.  This will ensure that you don't find the word "opportunity" (example) embedded within some other MsgPrompt.

Your users would simply type the {firstname} and other prompt values into your message.  Then, when you or the user clicks the "Save" or "Run" button on your form, the first thing I would do is create a loop through the message text looking for the "{" character followed by the "}" character.  I would search the table shown above for the text between these characters to ensure there is a field defined by that message prompt.  If not, I would display an error message and highlight the invalid message prompt using SelFirst and SelLen methods.  If it is found, I would continue the loop.

Then, after the message is saved, I would use similar code to search for those words and replace the {msgPrompt} with the fieldname shown in the other field of your mapping table using the Replace( ) function.  This part might look something like the following untested air code (sent from my iPad).

Dim strMsg as string, strSearch as string, varReplace as Variant
Dim strCriteria as string
Dim intLeftPosn as integer, intRightPosn as integer

intLeftPosn = instr(intRightPosn + 1, strMsg, "{")
Do while intLeftPosn > 0
     intRightPosn = instr(intLeftPosn + 1, strMsg, "}")

     strSearch = mid(strMsg, intLeftPosn, intRightPosn-intLeftPosn + 1)
     strCriteria = "[MsgPrompt] = '" & strSearch & "'"
     varReplace = DLOOKUP("FieldName", "newTableName", strCriteria)

    strMsg = Replace(strMsg, strSearch, NZ(varReplace, "###")
    intLeftPosn = instr(intLeftPosn + 1, strMsg, "{")

Open in new window

rltomalinAuthor Commented:
That's very helpful, thank you.
In my application there would not be more than 3 variables in a message and the templates would be pretty static.
Do I understand, that for subsequent variables in a message I would simply repeat the Replace statement, processing the text string from the previous statement?

I will not have time now to look at this until next week, so don't think that I have gone all quiet now!


rltomalinAuthor Commented:
Hello Pat

Excellent solution.  Does just what I wanted.


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.

All Courses

From novice to tech pro — start learning today.