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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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?


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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Dale FyeOwner, Developing Solutions LLCCommented:
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.


It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.