473,396 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Why do people do this?

Often I notice many users writing a SQL string like the following:

sql = "SELECT * FROM <tblName> "
sql = sql & "WHERE X = " & <variablename> & ""

Why concatenate the string? If the actual string doesn't change, why
concatenate it?

sql = "SELECT * FROM <tblName> WHERE X = " & <variablename> & ""

seems the better method.

Comments?

Jan 29 '06 #1
6 1820
Ozzone wrote:
Often I notice many users writing a SQL string like the following:

sql = "SELECT * FROM <tblName> "
sql = sql & "WHERE X = " & <variablename> & ""

Why concatenate the string? If the actual string doesn't change, why
concatenate it?

sql = "SELECT * FROM <tblName> WHERE X = " & <variablename> & ""

seems the better method.

Comments?


It's simply a method to break the string over multiple lines in the VBA window
to make it easier to read. My preference is to do it this way...

sql = "SELECT Field1, Field2, etc.. " & _
"FROM TableName " & _
"WHERE SomeField = SomeValue " & _
"ORDER BY SomeOtherField"

....where each significant "piece" of the statement is on its own line.
--
I don't check the Email account attached
to this message. Send instead to...
RBrandt at Hunter dot com
Jan 29 '06 #2
Seems the better method to whom?

Why do I write as I write ...

Because when I debug I want to see the string in easy to follow small
parts?

Because I don't want to scroll across the page to see all of my string?

Because I don't want to have errors produced by the VBA' editor's line
length limit?

Because when I use vbNewline and single quotes as in
sql = sql & vbNewline "WHERE X = ' & <variablename> & "
I can Debug.Print the string and use it
as the script for a SavedQuery or a SPROC
and in those forms it is easy to read, and is the usual way of writing
it.

Because when we transfer the procedure to other languages it's easy to
do a search and replace on
sql = sql & vbnewline & "
to (for example)
sql += '\n

Because, being one of the few roll your own SQL proponents (who hasn't
used the Query Grid.Wizard thingme for anything non-trival for five or
six years) I've written 187296 such strings and decided that
sql = sql & vbNewString & "Where ...
suits me best and TTBOMK JET SQL couldn't care less what I use.

Because when I post it seems less likely that news clients will hack up
this
sql = sql & "INSERT INTO BCustomers"
sql = sql & vbNewLine & "SELECT * FROM"
sql = sql & vbNewLine & "[C:\Documents and Settings\Lyle Fairfield\My
Documents\Access\Northwind.mdb].[Customers]"

than this
sql = "INSERT INTO BCustomers SELECT * FROM [C:\Documents and
Settings\Lyle Fairfield\My Documents\Access\Northwind.mdb].[Customers]"

(although it may hack up the last line too).

Jan 29 '06 #3

"Ozzone" <oz**********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Often I notice many users writing a SQL string like the following:

sql = "SELECT * FROM <tblName> "
sql = sql & "WHERE X = " & <variablename> & ""

Why concatenate the string? If the actual string doesn't change, why
concatenate it?

sql = "SELECT * FROM <tblName> WHERE X = " & <variablename> & ""

seems the better method.

Comments?


It's strictly a personal preference. Choose a style that you find easy to
write and easy to read (remember you'll likely be maintaining the code for a
long time).

Me, I hate scrolling horizontally to view code. My personal choice is
precisely the style demonstrated by Rick Brandt.

Go with what works for you.

--
Randy Harris
tech at promail dot com
I'm pretty sure I know everything that I can remember.

Jan 29 '06 #4
DFS
Ozzone wrote:
Often I notice many users writing a SQL string like the following:

sql = "SELECT * FROM <tblName> "
sql = sql & "WHERE X = " & <variablename> & ""

Why concatenate the string? If the actual string doesn't change, why
concatenate it?

sql = "SELECT * FROM <tblName> WHERE X = " & <variablename> & ""

seems the better method.

Comments?

Ozz,

Write enough code using both methods and you'll quickly find out how much
easier it is to edit if it's split up into multiple concatenated lines.
It's easier to read the query, there's no horizontal scrolling past the edge
of the screen, and it's easier to replace or comment out/in the entire line
than highlight just a section.

In fact, I usually place every single criteria on a separate line:

cSQL = "SELECT DISTINCT VisitType, Count(VisitType) as CountTypes "
cSQL = cSQL & "FROM ReportedVisits "
cSQL = cSQL & "WHERE EmpID = " & frm.listEmployee.Column(0) & " "
cSQL = cSQL & "AND (VisitDate Between #" & frm.dateBegin & "# AND #" &
frm.dateEnd & "#) "
cSQL = cSQL & "AND VisitComment IS NOT NULL "
cSQL = cSQL & "AND MgrAttend = 'Y' "
cSQL = cSQL & "GROUP BY VisitType;"

It's one of the few places in VB\VBA development where I don't try to
conserve lines.

And, it bothers me that queries written in the Access SQL window usually
won't save with the cr/lf so they resemble this style.


Jan 29 '06 #5
This is the 'old style'

sql = ""
sql = sql & "SELECT * FROM <tblName> "
sql = sql & "WHERE X = " & <variablename> & ";"
This is the 'new style'

sql = "SELECT * FROM <tblName> " _
& " WHERE X = " & <variablename> & ";"
The advantage of the 'new style' is that it is shorter
(permitting more content on the visible line), and requires
less typing. The 'new style' was only possible when
line continuation characters where added around 1995.

The advantage of the 'old style' is that you can
see which line has compilation errors, you can trace
each part of the string separately, and you can
easily add/remove sections of the string.

Of course if you only have 50 characters, you can put it
all on one line, but when you are doing 500 or 1000
characters, it makes it easier to follow if you use
multiple lines.

And when you go past the VBA line limit (1023 characters),
you have to split into multiple lines anyway.

(david)
"Ozzone" <oz**********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Often I notice many users writing a SQL string like the following:

sql = "SELECT * FROM <tblName> "
sql = sql & "WHERE X = " & <variablename> & ""

Why concatenate the string? If the actual string doesn't change, why
concatenate it?

sql = "SELECT * FROM <tblName> WHERE X = " & <variablename> & ""

seems the better method.

Comments?

Jan 29 '06 #6
Ozzone wrote:
Often I notice many users writing a SQL string like the following:

sql = "SELECT * FROM <tblName> "
sql = sql & "WHERE X = " & <variablename> & ""

sql = "SELECT * FROM <tblName> WHERE X = " & <variablename> & ""

seems the better method.


The use of the line continuation with the second method is useful as
others have pointed out.

I use a combination of the two. I like the first method for
particularly long statements because I can add comments indicating what
each line is or why I've chosen a particular function. Also, depending
on choices a user makes in my GUI, there maybe a requirement for more or
less tables to be pulled into the mix.

The line continuation also has a limit to the number of times you can
use it. I forget exactly how many. In my current project, the Oracle
SQL I'm writing, which consists of up to four main select statements
"union all'ed" together, with some of the from clauses containing
lengthy in-line queries which are themselves multiple union select
statements can add up to a huge number of characters. Haven't counted
them yet, but some of the larger statements, when pasted to MS Word span
15 pages in Tahoma 8 font.

With such monstrous statements, the first method, along with comments
really helps you realize where you are.

BTW, is "concatenate" the correct term here?
--
Tim http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "What's UP, Dittoooooo?" - Ditto
Jan 30 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Eivind Lie Nitter | last post by:
Hello, I'm currently making a CMS for my company, and in the CMS, the user has the possibility to upload any file to a documents folder - both for having a document archive and uploading pdf /...
303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
24
by: Greg N. | last post by:
Sorry if this topic has been discussed before: Is there any statistical data available about what percentage of browsers run with JS disabled? Thanks for any and all insights, Greg
4
by: Amir Michail | last post by:
Hi, It appears that Google does not provide an incentive for people to provide helpful links to other sites on their home pages. I believe that most people don't bother doing this and that this...
3
by: c676228 | last post by:
Hi everyone, I will develop a program to enroll a group of people on-line. Since we don't have number limitation for the people in the group. I am wondering if there is any company allow people...
3
by: fel | last post by:
People don't really appreciate open source as much as they should, given that so many people give away such magnificent tools, like linux, apache, php, etc, this is hard work, and in my opinion,...
16
by: Lucas J. Riesau | last post by:
I'm a serial killer and unlike other killers I don't kill people with guns, knives or chainsaws. I kill people with software. By reading the sourcecode of one of my programs, the victim's brain...
4
by: Peter Michaux | last post by:
On Feb 18, 11:02 pm, David Mark <dmark.cins...@gmail.comwrote: I've been thinking about this more for a library API. prefix namespacing FORK_addListener
2
by: Nomen Nescio | last post by:
Hello, just an announcement to say that I have killfiled some people here (well, not really). I won't see their posts (well, not really). I won't be replying to them, but I will be replying to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.