Joining long line in VB with " & _"

Tinkerz

Board Regular
Joined
Feb 26, 2007
Messages
179
what is the correct way to join long lines of code in VB with & _
I have a very long statment and need to check what is correct do I need "statement" & _
"statement"?

EDIT: removed SQL statment - it was blowing the page out of proportion - Moderator
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Tinkerz

You could use the line continuation character _ but since what you are constructing is a string I would suggest another method.

EDIT: removed SQL statment - it was blowing the page out of proportion - Moderator

Note this could probably be split up even more, I'm just basing it on your posted code.:)
 
Upvote 0
Thanks that is a great help and i will use that

but what is &_ and the rules from that any example would be great
 
Upvote 0
Use the Line-Continuation Character - As you have probably noticed, the Module Window does not have a word-wrapping feature. If you enter a very long statement, you can wrap the statement yourself by use of the line-continuation character wich consists of a space followed by an underscore ( _). If you don't break a long line with this character, you will need to use the horizontal scrollbars to view parts of the statement. You can't use the line-continuation character to wrap a string expression to another line; instead, you can divide the string into smaller pieces and concatenate the pieces.

Code: ( text )
Dim strMsg As String
'Instead of this
'strMsg = "The rules of Referential Integrity have been violated. You have made an attempt to enter a Training Record for an Employee in the Training Table when this individual doesn't exist in the Parent (Employees) Table. Enter a valid Employee Record first, then proceed"

'Use this syntax
strMsg = "The rules of Referential Integrity have been violated. You have made an " & _
"attempt to enter a Training Record for an Employee in the Training Table " & _
"when this individual doesn't exist in the Parent (Employees) Table. Enter " & _
"a valid Employee Record first, then proceed"

MsgBox strMsg, vbExclamation, "Referential Integrity Violation"

From: A 6-Pack of Good Programming Style Hints

And here's a great post dealing specifically with long SQL statements

HTH,

Smitty
 
Upvote 0

Forum statistics

Threads
1,214,645
Messages
6,120,711
Members
448,984
Latest member
foxpro

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top