473,385 Members | 1,707 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,385 software developers and data experts.

SQL & VBA

Hello,
I've asked this same question two weeks ago or so. So, this is my last
shot in hoping there's one person with an answer.
I use a lot of SQL statements in my VBA-code. I do it like this: make
the query, go the SQL code, copy and paste this in my VBA-code.
Now here's my problem: each time I have to make the same sort of
corrections in VBA in order to make the SQL work:
e.g. "SELECT tlbA.a & " " & tblB.b as ab" --> has to be changed in
"SELECT tblA.a & " & chr$(34) & " " & chr$(34) & " & tblB.b as ab"
Does anyone know of a plugin or a function that can do these sort of
corrections automaticaly? I could write one myself, but I think it's
gonna be a tough one.
Thanks,
--
bebelino
Nov 12 '05 #1
15 7676
It's easier to use single quotes (apostrophes) rather than chr$(34) in VBA.
That way you just have a continuous string with no interruption:

"SELECT tblA.a & ' ' & tblB.b as ab"

Should work in A2K or 2K2, dunno about A97.

"bebelino" <a.*@c.d> wrote in message
news:ie********************************@4ax.com...
Hello,
I've asked this same question two weeks ago or so. So, this is my last
shot in hoping there's one person with an answer.
I use a lot of SQL statements in my VBA-code. I do it like this: make
the query, go the SQL code, copy and paste this in my VBA-code.
Now here's my problem: each time I have to make the same sort of
corrections in VBA in order to make the SQL work:
e.g. "SELECT tlbA.a & " " & tblB.b as ab" --> has to be changed in
"SELECT tblA.a & " & chr$(34) & " " & chr$(34) & " & tblB.b as ab"
Does anyone know of a plugin or a function that can do these sort of
corrections automaticaly? I could write one myself, but I think it's
gonna be a tough one.
Thanks,
--
bebelino

Nov 12 '05 #2
The answer to your problem is in the problem. If you can state what the
problem is that you are having then someone will be able to suggest how
to solve the problem.

Now here's my problem: each time I have to make the same sort of
corrections in VBA in order to make the SQL work:
e.g. "SELECT tlbA.a & " " & tblB.b as ab" --> has to be changed in
"SELECT tblA.a & " & chr$(34) & " " & chr$(34) & " & tblB.b as ab"
<<

Here you are showing a correction that you make manually, but you are
not stating what the problem is which you are correcting.

Rich

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #3
bebelino <a.*@c.d> wrote in
news:ie********************************@4ax.com:
Hello,
I've asked this same question two weeks ago or so. So, this is
my last shot in hoping there's one person with an answer.
I use a lot of SQL statements in my VBA-code. I do it like
this: make the query, go the SQL code, copy and paste this in
my VBA-code. Now here's my problem: each time I have to make
the same sort of corrections in VBA in order to make the SQL
work: e.g. "SELECT tlbA.a & " " & tblB.b as ab" --> has to be
changed in "SELECT tblA.a & " & chr$(34) & " " & chr$(34) & "
& tblB.b as ab" Does anyone know of a plugin or a function
that can do these sort of corrections automaticaly? I could
write one myself, but I think it's gonna be a tough one.
Thanks,


I suspect that you are trying to concatenate two fields with a
space between them. It might be a lot simpler to write your query
as "SELECT tblA.a & space$(1) & tblB.b as ab". If you do htis in
the query builder, you don't have to change it to make it work in
VBA.

Bob Q
Nov 12 '05 #4
Hum, as others have mentioned, we are not really seeing your problem.

Why not in the query builder just go:
Ab:(a & ' ' & b)

That way, you don't have to change any code at all. Further, I often don't
even paste the sql into the code anyway. You can go:
dim strSql as string
dim rstRecs as dao.RecordSet

strSql = currentdb.querydefs("queryname").sql
strSql = Left(strSql, InStr(strSql, ";") - 1)
The above little trick means you don't have this big ugly mess of sql pasted
into your code, but can continue to use the query builder.

And, for sure, if you don't actually need to modify the sql, then you can
use:

set rstRecs = currentdb.OpenRecordSet("queryName")

Again, I not really sure what your problem is, but in the thousands and
thousands of posts here I have not seen your question, nor your problem.
Clearly either we here in the group are totally missing something, or you
are missing something?

Feel free to ask more questions, or perhaps you might post the 2 or 3 lines
of vba code that uses the sql text to give us an idea of what you are trying
to accomplish.

However, I see no reason as to why you don't just create the expression in
the query grid builder as:

Ab:(a & ' ' & b)
--
Albert D. Kallal (MVP)
Edmonton, Alberta Canada
ka****@msn.com
http://www.attcanada.net/~kallal.msn

Nov 12 '05 #5
On Mon, 6 Oct 2003 17:10:48 -0400, "Bruce Rusk"
<ho**********@spamless.ucla.edu> wrote:
It's easier to use single quotes (apostrophes) rather than chr$(34) in VBA.
That way you just have a continuous string with no interruption:

"SELECT tblA.a & ' ' & tblB.b as ab"


Okay, thanks, didn't know that,
--
bebelino
Nov 12 '05 #6
On 06 Oct 2003 21:22:19 GMT, Rich P <rp*****@aol.com> wrote:
The answer to your problem is in the problem. If you can state what the
problem is that you are having then someone will be able to suggest how
to solve the problem.
Off course.
Here you are showing a correction that you make manually, but you are
not stating what the problem is which you are correcting.


It's just the problem that I need to manually correct all over again
SQL-statements in VBA. Not only those " character but the format of a
date etc. But I begin to relealize that I'm doing it all wrond,
thanks,
--
bebelino
Nov 12 '05 #7
On Mon, 06 Oct 2003 22:50:59 GMT, Bob Quintal
<bq******@generation.net> wrote:
I suspect that you are trying to concatenate two fields with a
space between them. It might be a lot simpler to write your query
as "SELECT tblA.a & space$(1) & tblB.b as ab". If you do htis in
the query builder, you don't have to change it to make it work in
VBA.


I've learned that it's even possible in a shorter way. Just ' '
instead of chr$(34) or space$(1).
regards,
--
bebelino
Nov 12 '05 #8
On Tue, 07 Oct 2003 02:46:47 GMT, "Albert D. Kallal" <ka****@msn.com>
wrote:
That way, you don't have to change any code at all. Further, I often don't
even paste the sql into the code anyway. You can go:
You're right. I've spent a lot of time over the last years to edit
queries in VBA where it could be a whole lot easier.
dim strSql as string
dim rstRecs as dao.RecordSet

strSql = currentdb.querydefs("queryname").sql
strSql = Left(strSql, InStr(strSql, ";") - 1)

The above little trick means you don't have this big ugly mess of sql pasted
into your code, but can continue to use the query builder.
Wonderful tip, thanks.
And, for sure, if you don't actually need to modify the sql, then you can
use:

set rstRecs = currentdb.OpenRecordSet("queryName")
Did know that, but since I use a lot of SQL in code I found it not
clear to have so many queries in my querytab, and each time find an
explanatory name for it. Is there some good way to divide those
queries into groups? For instance those you only use in code etc. Or
am I missing again something?
Again, I not really sure what your problem is, but in the thousands and
thousands of posts here I have not seen your question, nor your problem.
Clearly either we here in the group are totally missing something, or you
are missing something?
I think it's the last ;-)
The way I builded queries with the query-editor forced me to make the
same corrections over and over again when I copied the SQL into code.
And then offcourse I've always used the chr$(34) and it became very
messy.
Feel free to ask more questions, or perhaps you might post the 2 or 3 lines
of vba code that uses the sql text to give us an idea of what you are trying
to accomplish.


Thanks, if I run into other query-problems I will ask them.
Many thanks for the great tips. I wonder how it's possible that I've
never seen a solution to my problem.
regards,
--
bebelino
Nov 12 '05 #9
"bebelino" <a.*@c.d> wrote in message
news:jk********************************@4ax.com...
Again, I not really sure what your problem is, but in the thousands and
thousands of posts here I have not seen your question, nor your problem.
Clearly either we here in the group are totally missing something, or you
are missing something?
I think it's the last ;-)


Nah, don't let me discourage you in anyway. As mentioned, it is only that
myself was not quite grasping the problem here! (this is not your fault!).
I think in reading the above...I was being a bit harsh. I am sorry about
that!
My apologies.
The way I builded queries with the query-editor forced me to make the
same corrections over and over again when I copied the SQL into code.
And then offcourse I've always used the chr$(34) and it became very
messy.


yes, I hoping that little trick of:

ab:(a & ' ' & b)

Will solve your problem. (as mentioned, I might be slight missing your
point).

The query builder will turn the above into:

select (a & ' ' & b) as ab

And, I will say for sure, I use the query builder just like, and then paste
in the resulting code. In many cases, I wish the query builder did NOT place
the table name in front of each field.

I would say that I am also near lost with out the query builder. I also
OFTEN have to modify the resulting text after I paste the sql into VBA. So,
I don't want in any way to suggest that the resulting text never needs
modifying (it certainly does!). However, I am suggesting the above little
expression should fix your concatenation problem.

And yes, I wish all of the forms, queries etc could be organized by folders.

--
Albert D. Kallal (MVP)
Edmonton, Alberta Canada
ka****@msn.com
http://www.attcanada.net/~kallal.msn
Nov 12 '05 #10
On Tue, 07 Oct 2003 02:46:47 GMT, "Albert D. Kallal" <ka****@msn.com>
wrote:
That way, you don't have to change any code at all. Further, I often don't
even paste the sql into the code anyway. You can go:
You're right. I've spent a lot of time over the last years to edit
queries in VBA where it could be a whole lot easier.
dim strSql as string
dim rstRecs as dao.RecordSet

strSql = currentdb.querydefs("queryname").sql
strSql = Left(strSql, InStr(strSql, ";") - 1)

The above little trick means you don't have this big ugly mess of sql pasted
into your code, but can continue to use the query builder.
Wonderful tip, thanks.
And, for sure, if you don't actually need to modify the sql, then you can
use:

set rstRecs = currentdb.OpenRecordSet("queryName")
Did know that, but since I use a lot of SQL in code I found it not
clear to have so many queries in my querytab, and each time find an
explanatory name for it. Is there some good way to divide those
queries into groups? For instance those you only use in code etc. Or
am I missing again something?
Again, I not really sure what your problem is, but in the thousands and
thousands of posts here I have not seen your question, nor your problem.
Clearly either we here in the group are totally missing something, or you
are missing something?
I think it's the last ;-)
The way I builded queries with the query-editor forced me to make the
same corrections over and over again when I copied the SQL into code.
And then offcourse I've always used the chr$(34) and it became very
messy.
Feel free to ask more questions, or perhaps you might post the 2 or 3 lines
of vba code that uses the sql text to give us an idea of what you are trying
to accomplish.


Thanks, if I run into other query-problems I will ask them.
Many thanks for the great tips. I wonder how it's possible that I've
never seen a solution to my problem.
regards,
--
bebelino
Nov 12 '05 #11
On Wed, 08 Oct 2003 03:02:10 GMT, "Albert D. Kallal" <ka****@msn.com>
wrote:
Nah, don't let me discourage you in anyway. As mentioned, it is only that
myself was not quite grasping the problem here! (this is not your fault!).
I think in reading the above...I was being a bit harsh. I am sorry about
that!
My apologies.

Really, no problem.
The way I builded queries with the query-editor forced me to make the
same corrections over and over again when I copied the SQL into code.
And then offcourse I've always used the chr$(34) and it became very
messy.

I would say that I am also near lost with out the query builder. I also
OFTEN have to modify the resulting text after I paste the sql into VBA. So,
I don't want in any way to suggest that the resulting text never needs
modifying (it certainly does!). However, I am suggesting the above little
expression should fix your concatenation problem.
It was this I tried to explain. Since modifications of SQL-code in VBA
often need the same modifications (dates and so on) I was wondering
if someone had a module or whatever where you passed the sqlstring in
and would return a modified sql-string, ready to paste into code. But
with ' ', I'm a whole lot further. And when I think about it now, I
will have less more work to do.
And yes, I wish all of the forms, queries etc could be organized by folders.


Just like me indeed.
best regards,
--
bebelino
Nov 12 '05 #12
Did know that, but since I use a lot of SQL in code I found it not
clear to have so many queries in my querytab, and each time find an
explanatory name for it. Is there some good way to divide those
queries into groups? For instance those you only use in code etc. Or
am I missing again something?

There are a couple of ways to do this; one is by a naming convention
(something like qvbaxxx for any query that will be used in code only). One
advantage is that it will group queries in alphabetical order.

I use a simple, quick-and-dirty approach: make all the queries the user
shouldn't be seeing (those used by modules and/or forms and reports) hidden
and turning View Hidden on (they become visible but greyed out in the
database window), and when you give the mdb to users you just turn View
Hidden off. There are also ways to make groups, but probably not worth it.
Nov 12 '05 #13
On Wed, 8 Oct 2003 11:08:05 -0400, "Bruce Rusk"
<ho**********@spamless.ucla.edu> wrote:
There are a couple of ways to do this; one is by a naming convention
(something like qvbaxxx for any query that will be used in code only). One
advantage is that it will group queries in alphabetical order.

I use a simple, quick-and-dirty approach: make all the queries the user
shouldn't be seeing (those used by modules and/or forms and reports) hidden
and turning View Hidden on (they become visible but greyed out in the
database window), and when you give the mdb to users you just turn View
Hidden off. There are also ways to make groups, but probably not worth it.


Thanks, qvbaxxx --> off course, simple.
I found those grouping possibilities with the favoritesfolder. Have to
check it further
regards,
--
bebelino

Nov 12 '05 #14
bebelino <a.*@c.d> wrote:
It was this I tried to explain. Since modifications of SQL-code in VBA
often need the same modifications (dates and so on) I was wondering
if someone had a module or whatever where you passed the sqlstring in
and would return a modified sql-string, ready to paste into code.


What I prefer to do is the following from my page "One method of using SQL queries in
code as parameter queries can be difficult to work with in code"
http://www.granite.ab.ca/access/sqlqueries.htm

The problem is that you need to use a query in code such as looping through a
recordset or running an action query of some sort but you want to limit the number of
records via some criteria.

However when you run the query in the query window you have to enter appropriate
parameter values. Locating an appropriate value can be a pain in the ...

Actually what I always do when action or recordset queries get the slightest bit ugly
I make a SELECT query with the required fields. By ugly I mean one join to another
table, a lot of fields or whatever. I get this query working nicely showing all the
records I want. But with no selection criteria. Now I can quickly see if the query
looks proper now or six months from now when I forget what appropriate values were
for the criteria.

I make a SELECT query with the required fields. I get this query working nicely
showing all the records I want. But with no selection criteria. Now I can quickly
see if the query looks proper now or six months from now when my memory is a bit hazy
on the specifics.

I then create the action or SELECT query in the QBE grid with dummy selection
criteria. I do NOT save it. Instead I click to the SQL code view and copy and Ctrl+C
to copy the code to the clipboard. Now I go into my VBA module and Ctrl+V to paste
the SQL code into the VBA code.. I then setup the strings with the double quotes and
line continuation for readability along the proper WHERE clause criteria from my
calling form or my code.

' decrement the QOH MatItemSizeQty record, ie change 5 to 3
strSQL = "UPDATE [Mat Rcvg - Update MRR - Update Qtys] " & _
"SET miqQtyOnHand = [miqQtyOnHand]-" & sngInventoryQty & " " & _
"WHERE itdID=" & me!InvTransDetailID & ";"
CurrentDb.Execute strSQL, dbFailOnError

where [Mat Rcvg - Update MRR - Update Qtys] is the ugly basic query.

Tony

--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems at
http://www.granite.ab.ca/accsmstr.htm
Nov 12 '05 #15
On Thu, 09 Oct 2003 18:27:59 GMT, Tony Toews <tt****@telusplanet.net>
wrote:
What I prefer to do is the following from my page "One method of using SQL queries in
code as parameter queries can be difficult to work with in code"
http://www.granite.ab.ca/access/sqlqueries.htm

The problem is that you need to use a query in code such as looping through a
recordset or running an action query of some sort but you want to limit the number of
records via some criteria.

However when you run the query in the query window you have to enter appropriate
parameter values. Locating an appropriate value can be a pain in the ...

Actually what I always do when action or recordset queries get the slightest bit ugly
I make a SELECT query with the required fields. By ugly I mean one join to another
table, a lot of fields or whatever. I get this query working nicely showing all the
records I want. But with no selection criteria. Now I can quickly see if the query
looks proper now or six months from now when I forget what appropriate values were
for the criteria.

I make a SELECT query with the required fields. I get this query working nicely
showing all the records I want. But with no selection criteria. Now I can quickly
see if the query looks proper now or six months from now when my memory is a bit hazy
on the specifics.

I then create the action or SELECT query in the QBE grid with dummy selection
criteria. I do NOT save it. Instead I click to the SQL code view and copy and Ctrl+C
to copy the code to the clipboard. Now I go into my VBA module and Ctrl+V to paste
the SQL code into the VBA code.. I then setup the strings with the double quotes and
line continuation for readability along the proper WHERE clause criteria from my
calling form or my code.

' decrement the QOH MatItemSizeQty record, ie change 5 to 3
strSQL = "UPDATE [Mat Rcvg - Update MRR - Update Qtys] " & _
"SET miqQtyOnHand = [miqQtyOnHand]-" & sngInventoryQty & " " & _
"WHERE itdID=" & me!InvTransDetailID & ";"
CurrentDb.Execute strSQL, dbFailOnError

where [Mat Rcvg - Update MRR - Update Qtys] is the ugly basic query.


Thanks for your reply tony,
regards,
--
bebelino
Nov 12 '05 #16

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

Similar topics

9
by: Collin VanDyck | last post by:
I have a basic understanding of this, so forgive me if I am overly simplistic in my explanation of my problem.. I am trying to get a Java/Xalan transform to pass through a numeric character...
1
by: DrTebi | last post by:
Hello, I have the following problem: I used to "encode" my email address within links, in order to avoid (most) email spiders. So I had a link like this: <a...
0
by: Thomas Scheffler | last post by:
Hi, I runned in trouble using XALAN for XSL-Transformation. The following snipplet show what I mean: <a href="http://blah.com/?test=test&amp;test2=test2">Test1&amp;</a> <a...
4
by: Luklrc | last post by:
Hi, I'm having to create a querysting with javascript. My problem is that javscript turns the "&" characher into "&amp;" when it gets used as a querystring in the url EG: ...
4
by: johkar | last post by:
When the output method is set to xml, even though I have CDATA around my JavaScript, the operaters of && and < are converted to XML character entities which causes errors in my JavaScript. I know...
8
by: Nathan Sokalski | last post by:
I add a JavaScript event handler to some of my Webcontrols using the Attributes.Add() method as follows: Dim jscode as String = "return (event.keyCode>=65&&event.keyCode<=90);"...
11
by: Jeremy | last post by:
How can one stop a browser from converting &amp; to & ? We have a textarea in our system wehre a user can type in some html code and have it saved to the database. When the data is retireved...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
12
by: InvalidLastName | last post by:
We have been used XslTransform. .NET 1.1, for transform XML document, Dataset with xsl to HTML. Some of these html contents contain javascript and links. For example: // javascript if (a &gt; b)...
7
by: John Nagle | last post by:
I've been parsing existing HTML with BeautifulSoup, and occasionally hit content which has something like "Design & Advertising", that is, an "&" instead of an "&amp;". Is there some way I can get...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.