Print weirdness

M

MikeB

Please help me understand

The following test code

Option Compare Database
Option Explicit

Dim lDots As Long

Sub ExploreDBEngineX()
lDots = 0
Debug.Print "Hello Mike. Today is " & Date & " " & Time()
Open "VBA_Trace.txt" For Output As #1

'Print #1, "Enumerate DBEngine:"

Print #1, String(lDots, "."), "Enumerate DBEnginex"
Close 1
Debug.Print "End of Run."

End Sub


There are two Print statements. One is commented out, one is not.

If I run the code with the statement to print "Enumerate DBEnginex",
then there are 14 spaces prepended to the output line in the file. If
I look at the individual components of that Print statement with
Debug.Print the first one is a string of nothing (0 dots) and the
second is simply the text.

If I switch the commented and the uncommented Print statements, there
are no leading spaces in the output file.

What's going on here?
Thanks
 
D

Douglas J. Steele

Print has a concept of "print zones" (sort of the equivalent of putting tabs
between each piece of output). When you have multiple "things" in the Print
statement (separated by either commas or semicolons), each "thing" is
written to a different print zone.

See what happens if you try

Print #1, String(lDots, ".") & "Enumerate DBEnginex"

so that everything's written to one print zone.
 
M

MikeB

Print has a concept of "print zones" (sort of the equivalent of putting tabs
between each piece of output). When you have multiple "things" in the Print
statement (separated by either commas or semicolons), each "thing" is
written to a different print zone.

See what happens if you try

Print #1, String(lDots, ".") & "Enumerate DBEnginex"

so that everything's written to one print zone.

Great, that was it. Now I have even more questions. :)

I Googled for "VBA Print Zones" and didn't find much, except for this
MSDN article http://msdn.microsoft.com/en-us/library/aa221542(office.11).aspx

This simply mentioned "print zone" in passing. Do you have a link to
somewhere where print zones are fully explained?

The article also raised an additional question. One is that it says
that "You can use this method only in a event procedure or macro
specified by a section's OnPrint event property setting."

Clearly I'm not doing that, so am I looking at the wrong Print method?

Thanks,
 
K

Klatuu

You are looking at two different things.
Print # is a statment used to write data to an external file.
expression.Print is a method that display an expression in the debug window.

Have a look in VBA help at both. You will see the difference there. Also,
as an aside, the statement:
Debug.Print "Hello Mike. Today is " & Date & " " & Time()

could be written as:
Debug.Print "Hello Mike. Today is " & Now
 
M

MikeB

You are looking at two different things.
Print # is a statment used to write data to an external file.
expression.Print is a method that display an expression in the debug window.

Right, fount it. I'm having some difficulty with the VBA help.

In the IDE, at the top right, there is a "help" field, if I type in
there, I don't get the VBA help info. If I open the Object explorer
and press the Help button on that, it opens up help that is much more
comprehensive.

Bit of a pain in the tush.
Have a look in VBA help at both. You will see the difference there. Also,
as an aside, the statement:
Debug.Print "Hello Mike. Today is " & Date & " " & Time()

could be written as:
Debug.Print "Hello Mike. Today is " & Now

Ok, got this too. I'm learning. and you guys are great in helping.
Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top