VB6Parse / Library / String / space_dollar

VB6 Library Reference

Space$ Function

The Space$ function in Visual Basic 6 returns a string consisting of the specified number of space characters (ASCII 32). The dollar sign ($) suffix indicates that this function always returns a String type, never a Variant.

Syntax

Space$(number)

Parameters

Return Value

Returns a String containing number space characters.

Behavior and Characteristics

Number Handling

Type Differences: Space$ vs Space

Common Usage Patterns

1. Padding Strings

Function PadRight(text As String, width As Integer) As String
If Len(text) >= width Then
PadRight = text
Else
PadRight = text & Space$(width - Len(text))
End If
End Function

Dim padded As String
padded = PadRight("Hello", 10)  ' "Hello     "

2. Creating Fixed-Width Fields

Function FormatField(value As String, fieldWidth As Integer) As String
Dim temp As String
temp = value & Space$(fieldWidth)
FormatField = Left$(temp, fieldWidth)
End Function

Dim field As String
field = FormatField("Name", 20)  ' "Name                "

3. Indentation

Function IndentText(text As String, level As Integer) As String
IndentText = Space$(level * 4) & text
End Function

Debug.Print IndentText("Nested Item", 2)  ' "        Nested Item"

4. Column Alignment in Reports

Sub PrintReport()
Dim col1 As String, col2 As String, col3 As String
col1 = "Name"
col2 = "Age"
col3 = "City"
Debug.Print col1 & Space$(15) & col2 & Space$(10) & col3
End Sub

5. Creating Separator Lines

Function CreateSeparator(width As Integer, char As String) As String
' Create base with spaces then replace
CreateSeparator = String$(width, char)
End Function

' Or use spaces for visual separation
Debug.Print "Header" & Space$(10) & "Value"

6. Text Centering

Function CenterText(text As String, width As Integer) As String
Dim padding As Integer
If Len(text) >= width Then
CenterText = Left$(text, width)
Else
padding = (width - Len(text)) \ 2
CenterText = Space$(padding) & text & Space$(width - Len(text) - padding)
End If
End Function

Dim centered As String
centered = CenterText("Title", 20)

7. Creating Empty Buffers

Function CreateBuffer(size As Integer) As String
CreateBuffer = Space$(size)
End Function

Dim buffer As String
buffer = CreateBuffer(100)  ' 100-character buffer

8. Formatting Tables

Sub PrintTableRow(col1 As String, col2 As String, col3 As String)
Dim row As String
row = Left$(col1 & Space$(20), 20) & _
Left$(col2 & Space$(15), 15) & _
Left$(col3 & Space$(10), 10)
Debug.Print row
End Sub

9. Creating Blank Lines

Sub AddVerticalSpace(lines As Integer)
Dim i As Integer
For i = 1 To lines
Debug.Print Space$(0)  ' Or just Debug.Print
Next i
End Sub

10. Formatting Currency Values

Function FormatAmount(amount As Currency) As String
Dim amountStr As String
amountStr = Format$(amount, "#,##0.00")
FormatAmount = Space$(15 - Len(amountStr)) & amountStr
End Function

Debug.Print FormatAmount(1234.56)  ' Right-aligned in 15 chars

Best Practices

When to Use Space$ vs String$

' Use Space$ for creating spaces specifically
Dim spaces As String
spaces = Space$(10)  ' Clear intent

' Use String$ for other repeated characters
Dim dashes As String
dashes = String$(10, "-")  ' More flexible

' Note: Space$(10) is equivalent to String$(10, " ")

Prefer Constants for Fixed Padding

' Less efficient: creating spaces repeatedly
For i = 1 To 1000
Debug.Print "Item" & Space$(10) & values(i)
Next i

' More efficient: create once
Const PADDING As String = "          "  ' 10 spaces
For i = 1 To 1000
Debug.Print "Item" & PADDING & values(i)
Next i

Validate Negative Values

Function SafeSpace(count As Integer) As String
If count < 0 Then
SafeSpace = ""
Else
SafeSpace = Space$(count)
End If
End Function

Combine with Left$ or Right$ for Fixed Width

' Ensure exact width regardless of input length
Function FixedWidth(text As String, width As Integer) As String
Dim temp As String
temp = text & Space$(width)
FixedWidth = Left$(temp, width)
End Function

Performance Considerations

' Inefficient: creating space string in loop
For i = 1 To 10000
output = output & Space$(5) & data(i)
Next i

' More efficient: create once
Dim spacer As String
spacer = Space$(5)
For i = 1 To 10000
output = output & spacer & data(i)
Next i

' Even better: use array and Join
Dim parts() As String
ReDim parts(1 To 10000)
For i = 1 To 10000
parts(i) = spacer & data(i)
Next i
output = Join(parts, "")

Common Pitfalls

1. Negative Values Cause Errors

' Runtime error: Invalid procedure call or argument
result = Space$(-5)  ' ERROR!

' Always validate
If count >= 0 Then
result = Space$(count)
Else
result = ""
End If

2. Confusion with Spc() Function

' Space$ returns a string
Dim s As String
s = Space$(5)  ' Returns "     " (5 spaces)

' Spc is used in Print statements only
Debug.Print "A"; Spc(5); "B"  ' Prints "A     B"

' Cannot assign Spc to variable
s = Spc(5)  ' ERROR!

3. Memory Issues with Large Values

' Be careful with very large values
Dim huge As String
huge = Space$(10000000)  ' 10 million spaces = ~20 MB

' Consider if you really need that many spaces
' Often there are better alternatives

4. Not Accounting for Existing Length

' Wrong: may create string longer than desired width
result = text & Space$(width)

' Correct: ensure exact width
temp = text & Space$(width)
result = Left$(temp, width)

5. Using for Non-Space Padding

' Wrong: Space$ only creates spaces
underline = Space$(20)  ' Trying to create underline
' This creates "                    ", not "____________________"

' Correct: use String$ for other characters
underline = String$(20, "_")

6. Floating Point Rounding

Debug.Print Space$(5.4)   ' Creates 5 spaces (rounds down)
Debug.Print Space$(5.6)   ' Creates 6 spaces (rounds up)
Debug.Print Space$(5.5)   ' Creates 6 spaces (banker's rounding)

' Be explicit with integer conversion if needed
Dim count As Integer
count = Int(5.7)  ' Force truncation
result = Space$(count)

Practical Examples

Creating Fixed-Width Reports

Sub PrintInvoice()
Dim header As String
Dim separator As String

' Create header with aligned columns
header = Left$("Item" & Space$(30), 30) & _
Left$("Qty" & Space$(10), 10) & _
Left$("Price" & Space$(15), 15)

separator = String$(55, "-")

Debug.Print header
Debug.Print separator
Debug.Print Left$("Widget" & Space$(30), 30) & _
Right$(Space$(10) & "5", 10) & _
Right$(Space$(15) & "$10.00", 15)
End Sub

Building Hierarchical Output

Sub PrintTree(text As String, level As Integer)
Debug.Print Space$(level * 2) & "- " & text
End Sub

PrintTree("Root", 0)
PrintTree("Child 1", 1)
PrintTree("Grandchild", 2)
PrintTree("Child 2", 1)

Formatting Data for Fixed-Width Files

Function FormatRecord(name As String, age As Integer, city As String) As String
Dim record As String
record = Left$(name & Space$(25), 25) & _
Right$(Space$(3) & CStr(age), 3) & _
Left$(city & Space$(20), 20)
FormatRecord = record
End Function

' Writes: "John Doe              25New York            "
Print #1, FormatRecord("John Doe", 25, "New York")

Limitations

← Back to String | View all functions