简体   繁体   中英

ms access form fields into a string vba

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

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.

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" .

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM