473,394 Members | 1,932 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,394 developers and data experts.

How to Debug SQL String

NeoPa
32,556 Expert Mod 16PB
Overview

One of the most popular (frequently occurring rather than best liked) problems we get here is with SQL strings being created/manipulated in VBA code. This is also true in other coding environments, but I'll work here with the VBA situation.


Explanation

The reason this is so difficult is that all the work is being done at a level of redirection. What I mean by this is that the coder is never working directly with the SQL itself, but rather with code, which in turn is relied on to produce the SQL that they are envisaging is required. It's rather similar to the problems coders have historically had dealing with pointers.


Solutions

A technique I often suggest to coders struggling with this (Coders of any level - This happens to experienced coders too.) is to use either the MsgBox() function, or Debug.Print into the Immediate Pane of the debugger window, to display the value of the SQL in the string before using it. The benefit of the Immediate Pane option over the MsgBox() is that it can then be copied and pasted - into a post in Bytes for instance.

Another alternative, assuming you're relatively comfortable with Debugging in VBA, is simply to trace through (You can set up a breakpoint to do this) to the line that will execute the SQL string, then you can look at the string value directly (in various ways in Debug mode - including printing into the Immediate Pane).

A third thing to try, because sometimes the behaviour and error messages seen when running via VBA are different from those seen running a full QueryDef (Saved Query in an Access database), is to take the SQL printed and put it into a QueryDef object and seeing what happens when you try to run it. To do this it is only necessary to create a QueryDef and, when it is opened for design, switch to SQL view and paste your copied SQL string in place of what's already there. Run it from there to see what happens. You may, or may not, get more information that way than you get from trying to execute it from VBA.


Conclusion
It's really much easier to appreciate what a SQL string is meant to do, and where there may be problems, when you can see it in its entirety and in its true form, rather than as the code is about to create it.

This is also a much better way to ask SQL questions on a forum. No-one is going to appreciate being asked a SQL question about your posted VBA code.
Jul 23 '11 #1
2 13494
ADezii
8,834 Expert 8TB
@NeoPa:
As a compliment to your Informative Post, I am including a Utility that will assist in the conversion of SQL Strings to their VBA Code counterparts. I realize that it will probably be of no Value to you, but it may be to others.
Jul 23 '11 #2
Thanks for the info on debugging SQL in VBA.
Mar 5 '14 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

97
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
4
by: emma middlebrook | last post by:
I have a question regarding asserting ... here's some code: string GetAssertMessage() { ... prepare a message string and return it... } void SomeMethod() { ...
12
by: Micah | last post by:
I am looking for a way to tell if the program is compiled in debug mode, I want the application I am writing to spit out a message to QA if a error occured and if it is in debug mode, but if it is...
5
by: Lonewolf | last post by:
Hi, I'm not sure if this has been asked before so please pardon me if this is a repeated question. Basically I have some performance critical directshow codes which is implemented in native,...
4
by: BA | last post by:
Hello, I have a very strange code behavior that I cannot make heads or tails of: I have c# code being executed in BizTalk assemblies which is acting very strangely. In my BizTalk process I...
10
by: mr_sorcerer | last post by:
Hi! I just found something interesting. I mean what do you think about this: char *p = 0; std::string str = p; Why std::string doesn't check null pointers?
0
by: BA | last post by:
I posted on this once before and could not get a solution, I am hoping someone can help. I have a very strange code debug behavior that I cannot make heads or tails of: I have c# code being...
3
by: =?Utf-8?B?ZWxhZGxh?= | last post by:
Hi. I was wondering if it was possible to give a variable different values depending on if the code was compiled in debug or release? i.e. if the code was compiled in debug: string...
2
by: joelkeepup | last post by:
Hi, I made a change this morning and now im getting an error that says either "a is undefined or null" or "e is undefined or null" the microsoft ajax line is below, I have no idea how to...
0
GaryTexmo
by: GaryTexmo | last post by:
Nothing amazing here, this is just a base groundwork for a debug window that someone could use to output debug text in a windows application. It provides fairly basic functionality so feel free 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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.