<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="VB6Parse Library Reference - spc - Graphics">
<title>spc - Graphics - VB6Parse Library Reference</title>
<link rel="stylesheet" href="../../../assets/css/style.css">
<link rel="stylesheet" href="../../../assets/css/docs-style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github-dark.min.css">
<script src="../../../assets/js/theme-switcher.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/vbnet.min.js"></script>
<script>hljs.highlightAll();</script>
</head>
<body>
<header class="docs-header">
<div class="container">
<h1><a href="../../../index.html">VB6Parse</a> / <a href="../../../library/index.html">Library</a> / <a href="../../../library/functions/graphics/index.html">Graphics</a> / spc</h1>
<p class="tagline">VB6 Library Reference</p>
</div>
</header>
<nav class="docs-nav">
<div class="container">
<a href="../../../index.html">Home</a>
<a href="../../../library/index.html">Library Reference</a>
<a href="../../../documentation.html">Documentation</a>
<a href="https://docs.rs/vb6parse" target="_blank">API Docs</a>
<a href="https://github.com/scriptandcompile/vb6parse" target="_blank">GitHub</a>
<button id="theme-toggle" class="theme-toggle" aria-label="Toggle theme">
<span class="theme-icon">🌙</span>
</button>
</div>
</nav>
<main class="container">
<article class="library-item">
<h1 id="spc-function">Spc Function</h1>
<p>Used with the Print # statement or Print method to position output.</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">Spc(n)</code></pre>
<h2 id="parameters">Parameters</h2>
<ul>
<li><code>n</code> - Required. Numeric expression specifying the number of spaces to insert before displaying or printing the next expression in the list.</li>
</ul>
<h2 id="return-value">Return Value</h2>
<p>Used only with Print # statement and Print method. Returns a value used to insert space characters in output.</p>
<h2 id="remarks">Remarks</h2>
<p>The Spc function is used to insert spaces in output when using the Print # statement (for files) or the Print method (for the Immediate window or printer). Unlike the Space function which returns a string of spaces, Spc is a special formatting function that only works within Print statements.
Key characteristics:
- Only valid within Print # or Print (Debug.Print) statements
- Inserts the specified number of space characters
- If current print position + n exceeds output line width, Spc skips to the next line
- If n is less than the output line width, the next print position is current position + n
- If n is greater than the output line width, the next print position is calculated as n Mod width
- Cannot be used independently or assigned to variables
- Returns a variant used internally by the Print statement
The Spc function differs from related positioning functions:
- <strong>Spc(n)</strong>: Inserts n spaces from current position
- <strong>Tab(n)</strong>: Moves to column n (absolute positioning)
- <strong>Space(n)</strong>: Returns a string of n spaces (can be used anywhere)</p>
<h2 id="typical-uses">Typical Uses</h2>
<ol>
<li><strong>File Output</strong>: Space elements in text file output</li>
<li><strong>Report Formatting</strong>: Create formatted reports with spacing</li>
<li><strong>Column Alignment</strong>: Align data in columns</li>
<li><strong>Debug Output</strong>: Format Debug.Print output</li>
<li><strong>Printer Output</strong>: Format printed output</li>
<li><strong>Data Separation</strong>: Separate data elements with spaces</li>
<li><strong>Fixed-Width Output</strong>: Create fixed-width formatted text</li>
<li><strong>Log Files</strong>: Format log file entries</li>
</ol>
<h2 id="basic-examples">Basic Examples</h2>
<pre><code class="language-vbnet">' Example 1: Print with spaces between items
Dim fileNum As Integer
fileNum = FreeFile
Open "output.txt" For Output As #fileNum
Print #fileNum, "Name"; Spc(10); "Age"; Spc(10); "City"
Close #fileNum
' Outputs: "Name Age City"</code></pre>
<pre><code class="language-vbnet">' Example 2: Debug output with spacing
Debug.Print "Item:"; Spc(5); "Value"
' Outputs: "Item: Value"</code></pre>
<pre><code class="language-vbnet">' Example 3: Multiple Spc calls in one statement
Print #1, "A"; Spc(3); "B"; Spc(5); "C"
' Outputs: "A B C"</code></pre>
<pre><code class="language-vbnet">' Example 4: Create formatted columns
Dim i As Integer
For i = 1 To 3
Debug.Print i; Spc(10); i * 10; Spc(10); i * 100
Next i
' Outputs aligned columns of numbers</code></pre>
<h2 id="common-patterns">Common Patterns</h2>
<h3 id="pattern-1-formatfileoutput">Pattern 1: <code>FormatFileOutput</code></h3>
<p>Format data in file with consistent spacing</p>
<pre><code class="language-vbnet">Sub FormatFileOutput(fileNum As Integer, name As String, _
age As Integer, city As String)
Print #fileNum, name; Spc(20 - Len(name)); _
age; Spc(10); city
End Sub</code></pre>
<h3 id="pattern-2-printaligneddata">Pattern 2: <code>PrintAlignedData</code></h3>
<p>Print data with aligned columns</p>
<pre><code class="language-vbnet">Sub PrintAlignedData(label As String, value As Variant, spacing As Integer)
Debug.Print label; Spc(spacing); value
End Sub</code></pre>
<h3 id="pattern-3-createtablerow">Pattern 3: <code>CreateTableRow</code></h3>
<p>Create table row with Spc spacing</p>
<pre><code class="language-vbnet">Sub CreateTableRow(fileNum As Integer, col1 As String, _
col2 As String, col3 As String)
Print #fileNum, col1; Spc(15 - Len(col1)); _
col2; Spc(15 - Len(col2)); _
col3
End Sub</code></pre>
<h3 id="pattern-4-formatlogentry">Pattern 4: <code>FormatLogEntry</code></h3>
<p>Format log entries with timestamps and messages</p>
<pre><code class="language-vbnet">Sub FormatLogEntry(fileNum As Integer, timestamp As String, _
level As String, message As String)
Print #fileNum, timestamp; Spc(5); _
level; Spc(10 - Len(level)); _
message
End Sub</code></pre>
<h3 id="pattern-5-printwithindent">Pattern 5: <code>PrintWithIndent</code></h3>
<p>Print text with indentation using Spc</p>
<pre><code class="language-vbnet">Sub PrintWithIndent(fileNum As Integer, indentLevel As Integer, _
text As String)
Print #fileNum, Spc(indentLevel * 4); text
End Sub</code></pre>
<h3 id="pattern-6-formatkeyvaluepair">Pattern 6: <code>FormatKeyValuePair</code></h3>
<p>Format key-value pairs with consistent spacing</p>
<pre><code class="language-vbnet">Sub FormatKeyValuePair(fileNum As Integer, key As String, _
value As String, Optional totalWidth As Integer = 40)
Dim spacesNeeded As Integer
spacesNeeded = totalWidth - Len(key) - Len(value)
If spacesNeeded < 1 Then spacesNeeded = 1
Print #fileNum, key; Spc(spacesNeeded); value
End Sub</code></pre>
<h3 id="pattern-7-printheader">Pattern 7: <code>PrintHeader</code></h3>
<p>Print formatted header with separators</p>
<pre><code class="language-vbnet">Sub PrintHeader(fileNum As Integer, title1 As String, _
title2 As String, title3 As String)
Print #fileNum, title1; Spc(15 - Len(title1)); _
title2; Spc(15 - Len(title2)); _
title3
Print #fileNum, String(15, "-"); Spc(1); _
String(15, "-"); Spc(1); _
String(15, "-")
End Sub</code></pre>
<h3 id="pattern-8-debugprintarray">Pattern 8: <code>DebugPrintArray</code></h3>
<p>Print array elements with spacing</p>
<pre><code class="language-vbnet">Sub DebugPrintArray(arr() As Variant)
Dim i As Integer
For i = LBound(arr) To UBound(arr)
Debug.Print arr(i); Spc(5);
Next i
Debug.Print ' New line
End Sub</code></pre>
<h3 id="pattern-9-formatnumerictable">Pattern 9: <code>FormatNumericTable</code></h3>
<p>Print numeric data in aligned columns</p>
<pre><code class="language-vbnet">Sub FormatNumericTable(fileNum As Integer, values() As Double)
Dim i As Integer
For i = LBound(values) To UBound(values)
Print #fileNum, Format(values(i), "0.00"); Spc(10);
If (i - LBound(values) + 1) Mod 5 = 0 Then
Print #fileNum, ' New line every 5 values
End If
Next i
End Sub</code></pre>
<h3 id="pattern-10-printreportline">Pattern 10: <code>PrintReportLine</code></h3>
<p>Print formatted report line</p>
<pre><code class="language-vbnet">Sub PrintReportLine(fileNum As Integer, lineNum As Integer, _
description As String, amount As Double)
Print #fileNum, Format(lineNum, "000"); Spc(5); _
description; Spc(30 - Len(description)); _
Format(amount, "$#,##0.00")
End Sub</code></pre>
<h2 id="advanced-usage">Advanced Usage</h2>
<h3 id="example-1-reportwriter-class">Example 1: <code>ReportWriter</code> Class</h3>
<p>Generate formatted text reports with Spc</p>
<pre><code class="language-vbnet">' Class: ReportWriter
Private m_fileNum As Integer
Private m_isOpen As Boolean
Public Sub OpenReport(fileName As String)
m_fileNum = FreeFile
Open fileName For Output As #m_fileNum
m_isOpen = True
End Sub
Public Sub WriteHeader(title As String, col1 As String, _
col2 As String, col3 As String)
If Not m_isOpen Then Exit Sub
' Center title
Dim totalWidth As Integer
totalWidth = 60
Dim leftPad As Integer
leftPad = (totalWidth - Len(title)) \ 2
Print #m_fileNum, Spc(leftPad); title
Print #m_fileNum, String(totalWidth, "=")
Print #m_fileNum,
' Column headers
Print #m_fileNum, col1; Spc(20 - Len(col1)); _
col2; Spc(20 - Len(col2)); _
col3
Print #m_fileNum, String(20, "-"); Spc(1); _
String(20, "-"); Spc(1); _
String(20, "-")
End Sub
Public Sub WriteDataRow(val1 As String, val2 As String, val3 As String)
If Not m_isOpen Then Exit Sub
Print #m_fileNum, val1; Spc(20 - Len(val1)); _
val2; Spc(20 - Len(val2)); _
val3
End Sub
Public Sub WriteSummary(label As String, value As String)
If Not m_isOpen Then Exit Sub
Print #m_fileNum,
Print #m_fileNum, String(60, "-")
Print #m_fileNum, label; Spc(60 - Len(label) - Len(value)); value
End Sub
Public Sub CloseReport()
If m_isOpen Then
Close #m_fileNum
m_isOpen = False
End If
End Sub
Private Sub Class_Terminate()
CloseReport
End Sub</code></pre>
<h3 id="example-2-logfileformatter-module">Example 2: <code>LogFileFormatter</code> Module</h3>
<p>Format log file entries with timestamps</p>
<pre><code class="language-vbnet">' Module: LogFileFormatter
Private m_logFile As Integer
Private m_logOpen As Boolean
Public Sub OpenLog(fileName As String)
m_logFile = FreeFile
Open fileName For Append As #m_logFile
m_logOpen = True
End Sub
Public Sub LogInfo(message As String)
If Not m_logOpen Then Exit Sub
Dim timestamp As String
timestamp = Format(Now, "yyyy-mm-dd hh:nn:ss")
Print #m_logFile, timestamp; Spc(5); "INFO"; Spc(10); message
End Sub
Public Sub LogWarning(message As String)
If Not m_logOpen Then Exit Sub
Dim timestamp As String
timestamp = Format(Now, "yyyy-mm-dd hh:nn:ss")
Print #m_logFile, timestamp; Spc(5); "WARNING"; Spc(6); message
End Sub
Public Sub LogError(message As String, Optional errorNum As Long = 0)
If Not m_logOpen Then Exit Sub
Dim timestamp As String
timestamp = Format(Now, "yyyy-mm-dd hh:nn:ss")
Print #m_logFile, timestamp; Spc(5); "ERROR"; Spc(8); message
If errorNum <> 0 Then
Print #m_logFile, Spc(30); "Error #"; errorNum
End If
End Sub
Public Sub LogDebug(category As String, message As String)
If Not m_logOpen Then Exit Sub
Dim timestamp As String
timestamp = Format(Now, "yyyy-mm-dd hh:nn:ss")
Print #m_logFile, timestamp; Spc(5); "DEBUG"; Spc(8); _
"["; category; "]"; Spc(3); message
End Sub
Public Sub LogSeparator()
If Not m_logOpen Then Exit Sub
Print #m_logFile, String(80, "-")
End Sub
Public Sub CloseLog()
If m_logOpen Then
Close #m_logFile
m_logOpen = False
End If
End Sub</code></pre>
<h3 id="example-3-datatableprinter-class">Example 3: <code>DataTablePrinter</code> Class</h3>
<p>Print data in formatted tables</p>
<pre><code class="language-vbnet">' Class: DataTablePrinter
Private m_columnWidths() As Integer
Private m_fileNum As Integer
Public Sub Initialize(fileNum As Integer, columnWidths() As Integer)
Dim i As Integer
m_fileNum = fileNum
ReDim m_columnWidths(LBound(columnWidths) To UBound(columnWidths))
For i = LBound(columnWidths) To UBound(columnWidths)
m_columnWidths(i) = columnWidths(i)
Next i
End Sub
Public Sub PrintRow(values() As String)
Dim i As Integer
Dim spaces As Integer
For i = LBound(values) To UBound(values)
Print #m_fileNum, values(i);
If i < UBound(values) Then
spaces = m_columnWidths(i) - Len(values(i))
If spaces < 1 Then spaces = 1
Print #m_fileNum, Spc(spaces);
End If
Next i
Print #m_fileNum, ' New line
End Sub
Public Sub PrintHeaderRow(headers() As String)
Dim i As Integer
PrintRow headers
' Print separator
For i = LBound(m_columnWidths) To UBound(m_columnWidths)
Print #m_fileNum, String(m_columnWidths(i), "-");
If i < UBound(m_columnWidths) Then
Print #m_fileNum, Spc(1);
End If
Next i
Print #m_fileNum, ' New line
End Sub
Public Sub PrintRightAligned(values() As String)
Dim i As Integer
Dim spaces As Integer
For i = LBound(values) To UBound(values)
spaces = m_columnWidths(i) - Len(values(i))
If spaces > 0 Then Print #m_fileNum, Spc(spaces);
Print #m_fileNum, values(i);
If i < UBound(values) Then
Print #m_fileNum, Spc(1);
End If
Next i
Print #m_fileNum, ' New line
End Sub</code></pre>
<h3 id="example-4-debugoutputhelper-module">Example 4: <code>DebugOutputHelper</code> Module</h3>
<p>Format debug output with Spc</p>
<pre><code class="language-vbnet">' Module: DebugOutputHelper
Public Sub PrintVariable(varName As String, varValue As Variant)
Debug.Print varName; Spc(20 - Len(varName)); "="; Spc(2); varValue
End Sub
Public Sub PrintVariables(ParamArray vars() As Variant)
Dim i As Integer
For i = LBound(vars) To UBound(vars) Step 2
If i + 1 <= UBound(vars) Then
PrintVariable CStr(vars(i)), vars(i + 1)
End If
Next i
End Sub
Public Sub PrintSection(title As String)
Debug.Print
Debug.Print String(50, "=")
Dim leftPad As Integer
leftPad = (50 - Len(title)) \ 2
Debug.Print Spc(leftPad); title
Debug.Print String(50, "=")
Debug.Print
End Sub
Public Sub PrintKeyValue(key As String, value As Variant, _
Optional totalWidth As Integer = 40)
Dim spacesNeeded As Integer
spacesNeeded = totalWidth - Len(key) - Len(CStr(value))
If spacesNeeded < 1 Then spacesNeeded = 1
Debug.Print key; Spc(spacesNeeded); value
End Sub
Public Sub PrintIndented(level As Integer, text As String)
Debug.Print Spc(level * 4); text
End Sub
Public Sub PrintArray(arr() As Variant, Optional itemsPerLine As Integer = 5)
Dim i As Integer
Dim count As Integer
count = 0
For i = LBound(arr) To UBound(arr)
Debug.Print arr(i); Spc(10);
count = count + 1
If count >= itemsPerLine Then
Debug.Print ' New line
count = 0
End If
Next i
If count > 0 Then Debug.Print ' Final new line if needed
End Sub</code></pre>
<h2 id="error-handling">Error Handling</h2>
<p>The Spc function itself doesn't typically generate errors, but the Print statement it's used with can:
- <strong>Error 52</strong> (Bad file name or number): If file number is invalid
- <strong>Error 54</strong> (Bad file mode): If file not opened for output
- <strong>Error 13</strong> (Type mismatch): If n is not numeric
Always ensure file is properly opened:</p>
<pre><code class="language-vbnet">On Error Resume Next
Print #fileNum, "Data"; Spc(10); "Value"
If Err.Number <> 0 Then
MsgBox "Error writing to file: " & Err.Description
End If</code></pre>
<h2 id="performance-considerations">Performance Considerations</h2>
<ul>
<li>Spc is very efficient for positioning output</li>
<li>More efficient than concatenating <code>Space()</code> strings in Print statements</li>
<li>No performance difference between Spc and Space within Print statements</li>
<li>File I/O is the bottleneck, not Spc itself</li>
</ul>
<h2 id="best-practices">Best Practices</h2>
<ol>
<li><strong>Only in Print</strong>: Use Spc only within Print # or Debug.Print statements</li>
<li><strong>Validate Arguments</strong>: Ensure n is positive and reasonable</li>
<li><strong>Consistent Spacing</strong>: Use constants for column widths</li>
<li><strong>Calculate Dynamically</strong>: Adjust spacing based on content length</li>
<li><strong>Use Space Alternative</strong>: Use <code>Space()</code> function if need string result</li>
<li><strong>Combine with Tab</strong>: Use Tab for absolute positioning, Spc for relative</li>
<li><strong>Test Output</strong>: Verify alignment with actual data</li>
<li><strong>Monospace Fonts</strong>: Ensure output viewed in monospace font</li>
<li><strong>Handle Long Data</strong>: Account for data that exceeds expected width</li>
<li><strong>Document Format</strong>: Comment expected column layout</li>
</ol>
<h2 id="comparison-with-related-functions">Comparison with Related Functions</h2>
<table>
<thead>
<tr>
<th>Function</th>
<th>Usage Context</th>
<th>Positioning</th>
<th>Returns</th>
</tr>
</thead>
<tbody>
<tr>
<td>Spc(n)</td>
<td>Print statements only</td>
<td>Relative (+n spaces)</td>
<td>Variant (internal)</td>
</tr>
<tr>
<td>Tab(n)</td>
<td>Print statements only</td>
<td>Absolute (column n)</td>
<td>Variant (internal)</td>
</tr>
<tr>
<td>Space(n)</td>
<td>Anywhere</td>
<td>N/A</td>
<td>String of n spaces</td>
</tr>
<tr>
<td>String(n, " ")</td>
<td>Anywhere</td>
<td>N/A</td>
<td>String of n spaces</td>
</tr>
</tbody>
</table>
<h2 id="platform-considerations">Platform Considerations</h2>
<ul>
<li>Available in VB6, VBA (all versions)</li>
<li>Part of Print statement syntax</li>
<li>Behavior consistent across platforms</li>
<li>Works with Debug.Print, Print # (files), and Printer.Print</li>
<li>Output width depends on file width setting (default 80 characters)</li>
</ul>
<h2 id="limitations">Limitations</h2>
<ul>
<li>Cannot be used outside Print statements</li>
<li>Cannot assign Spc result to variable</li>
<li>Cannot use in string concatenation</li>
<li>Wrapping behavior depends on output width setting</li>
<li>Not suitable for proportional fonts (use with monospace)</li>
<li>Limited to text output scenarios</li>
</ul>
<h2 id="related-functions">Related Functions</h2>
<ul>
<li><code>Tab</code>: Positions output at absolute column position</li>
<li><code>Space</code>: Returns string of spaces (can be used anywhere)</li>
<li><code>Print</code>: Statement that outputs data</li>
<li><code>Width</code>: Statement that sets output line width</li>
</ul>
</article>
<div style="margin-top: 3rem; padding-top: 2rem; border-top: 1px solid var(--border-color);">
<p>
<a href="index.html">← Back to Graphics</a> |
<a href="../index.html">View all functions</a>
</p>
</div>
</main>
<footer>
<div class="container">
<p>© 2024-2026 VB6Parse Contributors. Licensed under the MIT License.</p>
</div>
</footer>
</body>
</html>