By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,842 Members | 2,186 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,842 IT Pros & Developers. It's quick & easy.

How to Use Debug.Print

Narender Sagar
100+
P: 189
Hi Neo,
I think, I need further more clarity on above mentioned procedure to Debug SQL string. I can see immediate pane on the debugger screen.
I tried to enter Debug.Print into the immediate pane. But what next to do after that? (I think again I am asking something very basic. :(

** Edit **
This was moved from the article Debug SQL String as it's a question and doesn't add to the knowledgebase itself (as articles are there to do).
Jul 26 '11 #1
Share this Question
Share on Google+
2 Replies


NeoPa
Expert Mod 15k+
P: 31,306
Basic yes - but problem no.

Firstly, from within the Immediate Pane there is no need to specify Debug. Indeed the easiest shortcut for Debug.Print from within the Immediate Pane is as simple as ?.

Next, Debug.Print cam be researched easily and more fully than I can tell you about, simply by pressing F1 while the cursor is on the word Print.

Essentially though, it takes as parameters a list of items that are displayed (printed) in the Immediate Pane itself. At its simplest though ?strSQL gives you the actual SQL used (or about to be used) for you to study, or alternatively copy and paste into your Bytes forum post.

Example :

Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
  2. Dim cdb As DAO.Database
  3.  
  4. strSQL = "SELECT * " & _
  5.          "FROM   [MyTable] " & _
  6.          "WHERE  [ID] = " & Me.IDControl
  7. Set cdb = CurrentDb
  8. Debug.Print strSQL
  9. Call cdb.Execute(strSQL)
In this code I have actually included the command to print this to the Immediate Pane, but you could stop the code instead at line #9 instead and simply type ?strSQL in the Immediate Pane. Assuming your form is running and currently has a record selected with an ID (Control name is [IDControl]) value of 22431 then the result would be :
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM   [MyTable] WHERE  [ID] = 22431
This can be very helpful for those of us who have no access to your database. EG. Consider you have a form where you show student info and the VBA code is :
Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
  2. Dim cdb As DAO.Database
  3.  
  4. strSQL = "SELECT * " & _
  5.          "FROM   [tblStudent] " & _
  6.          "WHERE  [Student] = " & Me.Student
  7. Set cdb = CurrentDb
  8. Debug.Print strSQL
  9. Call cdb.Execute(strSQL)
You report that it's not working but we have no information as to what type of data the field or control (both called Student) contain.

Now you post the actual SQL and things become clearer :
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM   [tblStudent] WHERE  [Student] = Fred Jones
There is a clear mismatch between the field [Student] and the data passed. Either the field is numeric and the control is text, which would explain the problem, or they are both text and the format of the SQL is wrong (It should then be SELECT * FROM [tblStudent] WHERE [Student] = 'Fred Jones').

I hope that clarifies matters for you.

PS. Don't forget to have a play with some of those tutorials ;-)
Jul 26 '11 #2

Narender Sagar
100+
P: 189
Thanks Neo, Now I am understanding these things, slowly.
Jul 29 '11 #3

Post your reply

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