I'm trying to learn how to use arrays and I having some difficulty.
What I'm trying to do is split a line of text in to individual elements separated by a space.
eg the function call would be strSplitField("the man is walking down the" ,3) As String. this would return "is"
The first part of the function counts the numbers of spaces, in this case 5
Using this im trying to dim an array = to the number of spaces +1. this will act as a container for each word.
It works if I dim the array with a static number but not with the redim function.
Can anyone spot what Im doing wrong. thanks
See my code attached
Option Compare Database
Option Explicit
Global strContainer()
Options Base 1
Public Function strSplitField(strField As String, iSplitno As Integer) As String
'*******************************************
'Name: tester (Function)
'Purpose:
'Author: Mcgrco
'Date: October 14, 2002, 11:55:16 AM
'Called by:
'Calls:
'Inputs:
'Output:
'*******************************************
On Error GoTo ErrHandler
Dim iStart As Integer
Dim iNext As Integer
Dim DelimCount As Integer
Dim i As Integer
Dim NextField As String
Dim x
iStart = InStr(strField, " ")
If iStart = 0 Then
DelimCount = 1
Else
DelimCount = 0
Do
iNext = InStr(iStart + 1, strField, " ")
If iNext <> 0 Then iStart = iNext
DelimCount = Trim(DelimCount) + 1
Loop Until iNext = 0
End If
Debug.Print DelimCount 'Count the number of delimeters in the record
ReDim strContainer(1, DelimCount + 1)
i = 1
Do While i <= DelimCount + 1
If InStr(1, strField, " ") = 0 Then
strContainer(i) = strField
Else
x = Mid(strField, 1, InStr(1, strField, " ") - 1)
strContainer(i) = x ' Load the array
NextField = Trim$(Mid(strField, Len(strContainer(i)) + 1))
If NextField <> " " Then strField = NextField
End If
i = i + 1
Loop
Select Case iSplitno
Case 1 To DelimCount
strSplitField = IIf(IsNull(strContainer(iSplitno)), " ", strContainer(iSplitno))
End Select
ExitHere:
Exit Function
ErrHandler:
Dim strErrString As String
strErrString = "Error Information..." & vbCrLf
strErrString = strErrString & "Error#: " & Err.Number
strErrString = strErrString & "Description: " & Err.Description
MsgBox strErrString, vbCritical + vbOKOnly, "Function: tester"
Resume ExitHere
End Function
What I'm trying to do is split a line of text in to individual elements separated by a space.
eg the function call would be strSplitField("the man is walking down the" ,3) As String. this would return "is"
The first part of the function counts the numbers of spaces, in this case 5
Using this im trying to dim an array = to the number of spaces +1. this will act as a container for each word.
It works if I dim the array with a static number but not with the redim function.
Can anyone spot what Im doing wrong. thanks
See my code attached
Option Compare Database
Option Explicit
Global strContainer()
Options Base 1
Public Function strSplitField(strField As String, iSplitno As Integer) As String
'*******************************************
'Name: tester (Function)
'Purpose:
'Author: Mcgrco
'Date: October 14, 2002, 11:55:16 AM
'Called by:
'Calls:
'Inputs:
'Output:
'*******************************************
On Error GoTo ErrHandler
Dim iStart As Integer
Dim iNext As Integer
Dim DelimCount As Integer
Dim i As Integer
Dim NextField As String
Dim x
iStart = InStr(strField, " ")
If iStart = 0 Then
DelimCount = 1
Else
DelimCount = 0
Do
iNext = InStr(iStart + 1, strField, " ")
If iNext <> 0 Then iStart = iNext
DelimCount = Trim(DelimCount) + 1
Loop Until iNext = 0
End If
Debug.Print DelimCount 'Count the number of delimeters in the record
ReDim strContainer(1, DelimCount + 1)
i = 1
Do While i <= DelimCount + 1
If InStr(1, strField, " ") = 0 Then
strContainer(i) = strField
Else
x = Mid(strField, 1, InStr(1, strField, " ") - 1)
strContainer(i) = x ' Load the array
NextField = Trim$(Mid(strField, Len(strContainer(i)) + 1))
If NextField <> " " Then strField = NextField
End If
i = i + 1
Loop
Select Case iSplitno
Case 1 To DelimCount
strSplitField = IIf(IsNull(strContainer(iSplitno)), " ", strContainer(iSplitno))
End Select
ExitHere:
Exit Function
ErrHandler:
Dim strErrString As String
strErrString = "Error Information..." & vbCrLf
strErrString = strErrString & "Error#: " & Err.Number
strErrString = strErrString & "Description: " & Err.Description
MsgBox strErrString, vbCritical + vbOKOnly, "Function: tester"
Resume ExitHere
End Function