Convert an array to a string and a string to an array

Option Explicit

'Purpose : Converts a 1d or 2d array to a deliminated String
'Inputs : avInArray The array to convert to a string
' sDelimRows Delimeter to seperate rows
' sDelimCols Delimeter to seperate columns (for 2d arrays)
'Outputs : A string containing all the elements of avInArray seperated by sDelimRows
'Notes :

Function ArrayToString(avInArray As Variant, Optional sDelimRows As String = "æ", Optional sDelimCols As String = "") As String
Dim lThisItem As Long, lNumItems As Long, lFirstRow As Long, lLastRow As Long
Dim lThisRow As Long, lThisCol As Long, lFirstCol As Long, lLastCol As Long

On Error GoTo ExitProc
Select Case ArrayNumDimensions(avInArray)
Case 0
'Empty array
Case 1
'1D Array
For lThisItem = LBound(avInArray) To UBound(avInArray)
ArrayToString = ArrayToString & (CStr(avInArray(lThisItem)) & sDelimRows) 'Join the small to the large
Next
Case 2
'2D Arrays
lFirstRow = LBound(avInArray, 2)
lLastRow = UBound(avInArray, 2)
lFirstCol = LBound(avInArray, 1)
lLastCol = UBound(avInArray, 1)
'Loop over each column then row to create the result string
For lThisCol = lFirstCol To lLastCol
For lThisRow = lFirstRow To lLastRow
ArrayToString = ArrayToString & (CStr(avInArray(lThisCol, lThisRow)) & sDelimRows) 'Join the small to the large
Next
ArrayToString = ArrayToString & sDelimCols
Next
Case Else
MsgBox "ArrayToString: Invalid array structure"
End Select

ExitProc:
On Error GoTo 0
End Function

'Purpose : Converts a string to string array.
'Inputs : sThisString The string to convert to an array
' asResults String array containing results
' sDelimRows Delimeter to seperate rows
' sDelimCols Delimeter to seperate columns (for 2d arrays)
'Outputs : N/A
'Notes :
'Revisions :

Sub StringToArray(ByVal sThisString As String, asResults() As String, Optional sDelimRows As String = "æ", Optional sDelimCols As String = "")
Dim lPos1dDel As Long, lPos2dDel As Long, lLenString As Long, lColSepLen As Long
Dim lLastPos As Long, lNumCols As Long, lThisRow As Long, lThisCol As Long, lRowSepLen As Long

lLenString = Len(sThisString)

If lLenString Then
lLastPos = 1
lPos1dDel = InStr(1, sThisString, sDelimRows)
lPos2dDel = InStr(1, sThisString, sDelimCols)
lNumCols = StringCount(sThisString, sDelimCols, vbTextCompare)
lRowSepLen = Len(sDelimRows)

If lNumCols Then
'Convert a 2d string
lThisCol = 1
lColSepLen = Len(sDelimCols)
'Create buffer to store results
On Error GoTo 0
ReDim asResults(1 To lNumCols, 1 To Int(lLenString / 2))
Do While lPos1dDel
lThisRow = lThisRow + 1
If lPos1dDel > lPos2dDel Then
'Next Column
lThisCol = lThisCol + 1
lThisRow = 0
lPos2dDel = InStr(lPos2dDel + 1, sThisString, sDelimCols)
lLastPos = lLastPos + lColSepLen
Else
'Store Row
asResults(lThisCol, lThisRow) = Mid$(sThisString, lLastPos, lPos1dDel - lLastPos)
lLastPos = lPos1dDel + lRowSepLen
End If
lPos1dDel = InStr(lLastPos, sThisString, sDelimRows)
Loop
ReDim Preserve asResults(1 To lNumCols, 1 To lThisRow)
Else
'Convert a 1d string
'Create buffer to store results
ReDim asResults(1 To Int(lLenString / 2))
lPos1dDel = InStr(lLastPos, sThisString, sDelimRows)
Do While lPos1dDel
lThisRow = lThisRow + 1
asResults(lThisRow) = Mid$(sThisString, lLastPos, lPos1dDel - lLastPos)
lLastPos = lPos1dDel + lRowSepLen
lPos1dDel = InStr(lLastPos, sThisString, sDelimRows)
Loop
ReDim Preserve asResults(1 To lThisRow)
End If
Else
Erase asResults
End If
End Sub

'Purpose : Calculates the number of dimensions in an array
'Inputs : avInArray. The array to evaluate.
'Outputs : The number of dimensions the array has.
'Notes :

Function ArrayNumDimensions(avInArray As Variant) As Long
Dim lNumDims As Long

If IsArray(avInArray) Then
On Error GoTo ExitSub
Do
lNumDims = UBound(avInArray, ArrayNumDimensions + 1)
ArrayNumDimensions = ArrayNumDimensions + 1
Loop
End If

ExitSub:
On Error GoTo 0
End Function

'Purpose : Count the number of matching instances of one string within another
'Inputs : sSearchString The string to search
' sForItem The string to search for
' [tCompare] The method of comparison. Defaults to a case insensative search.
' Can also specify vbBinaryCompare.
'Outputs : Returns the number of instances of the sForItem within sSearchString
'Notes :
'Revisions :

Function StringCount(sSearchString As String, sForItem As String, Optional tCompare As Long = vbTextCompare) As Long
Dim lPos As Long, lLenItem As Long

lLenItem = Len(sForItem)
Do
lPos = InStr(lPos + lLenItem, sSearchString, sForItem, tCompare)
If lPos Then
StringCount = StringCount + 1
Else
Exit Do
End If
Loop
End Function

No comments:

Visual Basic-6 has emerged as one of the standard Windows Programming Language and it has become a must for all Software people for developing Applications in Visual Environment. So it is, one must learn Visual Basic-6.

What is our Objective in this Courseware?


The Overall Objective in this Courseware is to give a Hands-on Approach to develop different projects in Visual Basic-6.0 using intrinsic, professional and user–created ActiveX controls and also develop projects using databases, DAO’s, ADO’s, DLL’s, Documents, Crystal Reports etc. covering almost all the essential features of VB-6 Professional Edition. After reading one lesson any interested reader will be able to get complete hands-on experience with the VB project and get a sense of fulfilment and achievement. Learning by doing is the motto with which this courseware is written. After giving a short introduction about VB-6 we will explain how to create and execute a project in VB using some intrinsic ActiveX controls. Creating and executing projects will be the central theme of all the lessons which we will be giving in this courseware.

What is Visual Basic-6?


Visual Basic-6 has its origin in Basic which was developed round about the year 1960, when high level languages were just being introduced to the computer community. Microsoft has made it extremely powerful by gearing all its good features to the Windows environment. Starting with the version 3 and then with 4, and then with 6, Visual Basic is now at version 6. Basic is a Procedure Oriented Language intended to implement single tasks in text based environment whereas Visual Basic is an Event Driven Language intended to implement Projects or Applications containing multiple tasks in Windows Environment.

What can Visual Basic do for you?

Visual Basic can serve as an ideal front end tool for the clients to interact. It has got connectivity mechanisms for all types of databases situated far and wide in a network and so it can cater to the needs of a large body of clients. Using the latest ActiveX technologies, it can integrate the functionalities provided by other applications like Word Excel and other Windows. Its internet capabilities provide easy access to documents and applications across the internet. Above all it embodies the Object Oriented Technology, which is the cutting edge technology for all the present day developments in the Software World. The final application is a true EXE file and so can be freely distributed.


Structure of VB-6 Projects:


We said earlier that VB-6 implements projects or applications. A project is developed using one or more Forms. A Form is simply a window containing one or more Controls. Controls in VB consist of labels, text boxes, list boxes, combo boxes, scroll bars etc. which are the constituents of windows environment. It is only the controls that give VB, its immense power and so there is a lot of interest in creating more and more powerful controls. ActiveX controls mark a significant development in controls technology. In fact all controls in VB-6 are ActiveX controls, which have the extension .ocx. These controls have properties whose values can be initialized at design time and also varied during run time. The properties are something like variables. The controls are activated by codes written in a high level language. By associating our problem variables with the properties of the controls, our problem variables can be manipulated to give the problem solution. In summary we can say that a VB project is made of forms, controls and their properties and codes.

Integrated Development Environment:

The working environment in VB is often referred to as the Integrated Development Environment or IDE, because it integrates many different functions such as design, editing, compiling and debugging within a common environment. Since all our projects are developed only in the IDE, let us now have a brief look at its features. You will be able to understand their uses at the time of building projects. The VB IDE looks as shown in the figure.