Arrays and the Split Function (1 Viewer)

Status
Not open for further replies.

pono1

Registered User.
Local time
Yesterday, 21:59
Joined
Jun 23, 2002
Messages
1,186
In its simplest form an array is like a train composed of boxcars, each boxcar with its own cargo.

Train's boxcars...
-------------------------------
| Milk | Bread | Apples |
-------------------------------

An array's boxcars are called elements, and its elements hold data.

An array of numbers...
------------------------
| 333 | 222 | 672 |
------------------------

An array of text...
-----------------------
| Bob | Lee | Joe |
-----------------------

This post will show you how to create simple, one-dimensional dynamic arrays (an array that can have an uncertain, flexible number of elements), filling the elements with the handy Split function. Once data is dished-out to an array, it is relatively easy to analyze...

To use the code below, first create a "test form" and put a button on it. In the button's click event handler, paste the following code...

Code:
'Get the file name.
   MsgBox GetFileName("C:\MyFolder\Myfile.txt")

'Get the drive letter.
   MsgBox GetDriveLetter("C:\MyFolder\Myfile.txt")

'Is there beer?
   If DoesItemExist("beer-bread-peaches-yams", "-", "beer") Then
      MsgBox "Yes! Beer Exists!"
   Else
      MsgBox "No beer here."
   End If

And in the form's code window, below the button's code, paste the following...

Code:
Function GetFileName(ByVal Path As String) As String
' Description:   Given a file path, return the file name.
' Parameter:     Path = Full folder path.
' Returns:       "C:\MyFolder\Myfile.txt" returns Myfile.txt

'Create a dynamic array.
   Dim sParts() As String

'Split the path into parts,
'using the backslash as a delimiter,
'filling the array's elements with the
'broken up string segments.
   sParts = Split(Path, "\")

' At this point, the array
' named sParts would look 
' something like the following:
' --------------------------------
' |  0   |     1    |     2      |
' --------------------------------
' |  C:  | MyFolder | MyFile.txt |
' --------------------------------

' In code you may refer to a specific
' element by its numbered position:

' sParts(0) = C:
' sParts(1) = MyFolder
' sParts(2) = MyFile.txt

' Or you can simply get the last element
' using the handy UBound function...
   GetFileName = sParts(UBound(sParts))

End Function

Function GetDriveLetter(ByVal Text As String) _
As String
' Description:   Given a file path, return the drive letter.
' Parameter:     Text = Full file path.
' Returns:       "C:\MyFolder\Myfile.txt" returns C:

' See the code comments in the GetFileName function above.
   Dim sParts() As String
   sParts = Split(Text, "\")
   GetDriveLetter = sParts(LBound(sParts))

End Function

Function DoesItemExist(sList As String, _
                       sDelimiter As String, _
                       sItem As String) _
As Boolean
'Say your program receives or produces lists,
'lists that may look like this:
'   beer-bread-peaches-yams

'Notice that the list has a character that separates
'-- or delimits -- the items, a hyphen.

'One more thing: say your list may or may not
'contain certain items. That is, say you only
'want to know if beer is in your list...

'Create a dynamic array.
   Dim sItems() As String
   
'Put the list into the array.
   sItems = Split(sList, sDelimiter)

'Move through each item in the array,
   Dim i As Integer
   For i = LBound(sItems) To UBound(sItems)
   'searching for a specific item...
      If sItems(i) = sItem Then
      'Found it.
        DoesItemExist = True
      'We're done, so leave.
        Exit For
      End If
   Next i

End Function
Use the code comments and, if that's not enough, the Access Help file to learn how it all works.

Regards,
Tim
 
Last edited:

حسين

New member
Local time
Today, 07:59
Joined
Mar 16, 2005
Messages
6
hi all ...
Example Achievable
or
example Bearable

thanks
 

Adeptus

What's this button do?
Local time
Today, 15:29
Joined
Aug 2, 2006
Messages
300
I use split quite often for finding substrings.
Say you have someone's fullname, eg Joe Bloggs, and just want their first name.

Code:
Firstname = Split(fullname," ")(0)

The (0) at the end returns the first element of the (zero-based) array.
 
Status
Not open for further replies.

Users who are viewing this thread

Top Bottom