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...
And in the form's code window, below the button's code, paste the following...
Use the code comments and, if that's not enough, the Access Help file to learn how it all works.
Regards,
Tim
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
Regards,
Tim
Last edited: