473,748 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to Debug SQL String

NeoPa
32,571 Recognized Expert Moderator MVP
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 13589
ADezii
8,834 Recognized Expert Expert
@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
Dave44000
15 New Member
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
27810
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
4
4529
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
2589
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 in release mode, I want it to keep the error to itself. is there a way to do this if so how ? Micah
5
11361
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, unmanaged C++. I have written a managed wrapper for it using C++/CLI. problem comes when I try to pass a C# string over to the DLL via C++/CLO. Basically I have a textbox in the C# GUI, and I declared my C++/CLI property as follows. C++/CLI property...
4
2286
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 call a static method: public static string ValidateMessage(params...)
10
7312
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
1800
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 executed in BizTalk assemblies which is repeating debug statements. I tried debug.flush() and debug.close() which did not solve the problem. In my BizTalk process I call a static method:
3
2956
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 strConnection = "Server1"; if the code was compiled in release: string strConnection = "Server2"; Thanks!
2
9037
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 figure this problem out. Any suggestions? thanks Joel
0
4509
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 modify to suit your purposes. The topic came up in a question thread (somewhat) and mostly it reminded me that I had been meaning to make one of these so figured I'd share it. It's important to note that there are other methods of gathering debug...
0
8991
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9552
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9326
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9249
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6796
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4877
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.