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

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,358
Office Version
  1. 365
Platform
  1. Windows
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

Tinkerz

Board Regular
Joined
Feb 26, 2007
Messages
179
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

Smitty

Legend
Joined
May 15, 2003
Messages
29,536
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,191,025
Messages
5,984,197
Members
439,877
Latest member
kellylet

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
Top