16

I am new to VB. I want to test some old VB code, but I need the ability to print to the console to be able to test certain values that are set in the code. How to print to the console from VB?

4 Answers 4

26

Use debug.print. But there is no console on a VB6 application, that will print to the debug window.

0
12

This isn't expected to be the accepted answer because Debug.Print is the way to go for IDE testing.

However just to show how to use the standard I/O streams easily in VB6:

Option Explicit
'
'Reference to Microsoft Scripting Runtime.
'

Public SIn As Scripting.TextStream
Public SOut As Scripting.TextStream

'--- Only required for testing in IDE or Windows Subsystem ===
Private Declare Function AllocConsole Lib "kernel32" () As Long
Private Declare Function GetConsoleTitle Lib "kernel32" _
    Alias "GetConsoleTitleA" ( _
    ByVal lpConsoleTitle As String, _
    ByVal nSize As Long) As Long
Private Declare Function FreeConsole Lib "kernel32" () As Long

Private Allocated As Boolean

Private Sub Setup()
    Dim Title As String

    Title = Space$(260)
    If GetConsoleTitle(Title, 260) = 0 Then
        AllocConsole
        Allocated = True
    End If
End Sub

Private Sub TearDown()
    If Allocated Then
        SOut.Write "Press enter to continue..."
        SIn.ReadLine
        FreeConsole
    End If
End Sub
'--- End testing ---------------------------------------------

Private Sub Main()
    Setup 'Omit for Console Subsystem.

    With New Scripting.FileSystemObject
        Set SIn = .GetStandardStream(StdIn)
        Set SOut = .GetStandardStream(StdOut)
    End With

    SOut.WriteLine "Any output you want"
    SOut.WriteLine "Goes here"

    TearDown 'Omit for Console Subsystem.
End Sub

Note that very little of the code there is required for an actual Console program in VB6. The bulk of it is about allocating a Console window when the program is not running in the Console Subsystem.

2
  • 3
    +1: Nice one. So, just like I said then, easy when you know how :) Commented May 10, 2012 at 21:41
  • Doesn't work for me. I included the Microsoft Scripting Runtime. It compiles and runs, it just doesn't print anything to the dos prompt which I'm running it from. Perhaps it doesn't work in Windows 10? Commented Oct 1, 2020 at 13:15
4

Use OutputDebugString and view the messages with the excellent free DebugView. Debug executables or from the IDE. More information and reusable code from Karl Peterson here

1
  • 1
    This is an excellent answer. Minimal fuss, works like a charm.
    – BoredBsee
    Commented Apr 14, 2021 at 18:55
0

This isn't something that Vb6 can easily do (I'm sure it can be done, but you'd be calling native Win32 APIs, and isn't worth the pain if you're just using it for debugging)

Your best bet (IMHO) is to write those values to a log file.

3
  • Actually it can be done quite trivially from VB6 using the FSO. Of course it only applies when you have a console, which you seldom do in a VB6 program - unless you AllocConsole() or are running as an EXE relinked for the Console Subsystem. But I agree it isn't the best tool for debugging.
    – Bob77
    Commented May 9, 2012 at 17:20
  • @Bob: So, comes under the heading of "Easy when you know how" maybe? :) Commented May 10, 2012 at 8:48
  • Not much to it. See the extraneous answer I posted.
    – Bob77
    Commented May 10, 2012 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.