0

I have a form (say) Data, which contains text boxes A,B,C.

I wish to write an email based on the form data. In my email body, I want the following format:

A is : (actual value of A in text box in the form) (a newline) B is : ((actual value of B in text box in the form) ( a newline) C is : ((actual value of C in text box in the form).

I know I can access values by Forms!Data!A_value (assuming I named the box as A_value). I am not able to combine them into a string and add a newline too.

I have tried the following:

Dim body as String

body = "A is : & Forms!Data!A_value &" & "B is : & Forms!Data!B_value &" & "C is : & Forms!Data!C_value &"

It is because I read an & results to a new line somewhere.

However, when i do that, the whole thing is concatenated as written in the code and no values are obtained from the form field. Please suggest options: Thanks in advance

3 Answers 3

1

You probably want something like:

Dim body as String

body = "A is : " & Forms!Data!A_value & vbNewLine & _
       "B is : " & Forms!Data!B_value & vbNewLine & _
       "C is : " & Forms!Data!C_value 

Note: the fact that I wrote that code on 3 lines, using line continuation characters, is nothing to do with the insertion of the new line characters in the output. It could have also been written as

body = "A is : " & Forms!Data!A_value & vbNewLine & "B is : " & Forms!Data!B_value & vbNewLine & "C is : " & Forms!Data!C_value 

but I find that harder to read.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! That worked quite well. Didn't know about vbNewLine. :)
1

The ampersand is used to concatenate text "hello " & "there".

If you quote the ampersand it does nothing, just reproduces the ampersand "bits & bobs".

You can use the character vbCrLf (carriage return/linefeed) to add (concatenate) a linebreak, "time for " & vbCrLf & "a break".

Comments

1

Another trick you can use is to create a single string template with placeholders for the values, then use Replace statements to fill them in, like:

body = "A is: {A} & B is: {B} & C is: {C}"
body = Replace(body, "{A}", Forms!Data!A_value)
body = Replace(body, "{B}", Forms!Data!B_value)
body = Replace(body, "{C}", Forms!Data!C_value)

And break out to multiple lines, like:

body = "A is: {A}{CR}B is: {B}{CR}C is: {C}{CR}"
body = Replace(body, "{A}", Forms!Data!A_value)
body = Replace(body, "{B}", Forms!Data!B_value)
body = Replace(body, "{C}", Forms!Data!C_value)
body = Replace(body, "{CR}", vbCrLf)

2 Comments

Seems to be an interesting option to be done if dealing with a lot of fields. Thanks.
Nice - that would be especially useful if there were fields being used multiple times in the output, e.g. people's names - one Replace updating every occurrence.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.