Join Split Array Functions.
The Join() and Split() functions in MS Access are not widely used, but they can be both interesting and surprisingly powerful. To understand their potential, let’s examine them. Later, we will write a program to demonstrate how these functions can be applied in real-world scenarios.
We will start with the Array() function. But before exploring it in detail, let’s first review how to define an array variable and assign a value to each array element.
Demo of Array() Function
'Dimension the array for six elements
Dim varNumber(0 To 5) As Variant, j As Integer
For j = 0 To 5
varNumber(j) = j + 1
NextThe first statement in the program defines a variable named varNumber as a Variant Type Array for 6 elements.
Another variable j is defined as an integer type that will be used as an index variable in the For. . .Next loop.
The next three statements in the above program assign values 1 to 6 to the Array elements as:
- varNumber(0) = 1
- varNumber(1) = 2
- varNumber(2) = 3
- varNumber(3) = 4
- varNumber(4) = 5
- varNumber(5) = 6
We can do this task with only one statement if we use the Array() Function as below:
varNumber = Array(1, 2, 3, 4, 5, 6)
Unlike the first example, we don’t need to define the variable with a fixed number of elements—the array is automatically sized based on the number of items in the argument list. In this case, we assigned constant values from 1 to 6 to the array elements indexed 0 to 5.
Another important point to note is that the target variable (varNumber) must always be declared as a Variant type. This provides greater flexibility, allowing you to assign mixed data types to different elements of the array, as shown in the example below:
varNumber = Array("Nancy", 25, "Andrew", 30, 172.5)
We have assigned a mix of String, Integer, and Double Data Types to different elements of the same array. Again, we have used constant values to assign to the array.
The Array Function can accept several parameters as a single block, without defining several parameter declarations in the main program.
You can use Constants, Variables, or data Field Values to assign values to the array.
Example-1:
a = "Nancy" b = 25 c = 172.5 varNumber = Arrary(a,b,c)
Example-2:
varNumber = Array(Me![FirstName],Me![BirthDate],Me![Address])
Next, we will explore the Join() function. To better understand how it works, let’s create another array of values and use it as input for this function.
varNumber = Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat")
We have the names of weekdays in the array variable varNumber.
varNumber(0) = "Sun" . . . varNumber(6) = "Sat"
If you want to combine all seven elements of this array variable into a single string, with each item separated by commas (for example: Sun, Mon, Tue, Wed, Thu, Fri, Sat), you can do so with the following statements:
Dim strWeeks as string, j as integer
strWeeks=""
For j = 0 to 6
if j=6 then
strWeeks = strWeeks & varNumber(j)
Else
strWeeks = strWeeks & varNumber(j) & ","
end if
NextJoin() Function:
The above task takes only one statement with the Join() Function:
strWeeks = Join(varNumber,",")
The first parameter of the Join() function is the array containing the values to be combined into a single string. The second parameter specifies the separator character inserted between the array elements. If this parameter is omitted, a space is used as the default separator. Otherwise, the character or string you specify will be used as the separator.
Result: strWeeks = "Sun,Mon,Tue,Wed,Thu,Fri,Sat"
Split() Function:
Split() is the complementary Function of Join(). It splits the individual items, separated by the delimiter character, and stores the values into an array variable of Variant Type.
We need the following lines of code to do the same task as the Split() Function:
Dim strWeeks(0 To 6) As Variant, strtxt As String Dim j As Integer, k As Integer strtxt = "Sun,Mon,Tue,Wed,Thu,Fri,Sat" k = 0 For j = 1 To Len(strtxt) Step 4 strWeeks(k) = Mid(strtxt, j, 3) k = k + 1 Next
With the use of the Split() Function, it takes only one statement to do the job that we did with the above program:
strWeeks = Split(strTxt,",")
Next week, we will use these functions in a Program to redefine a Query linked to a Form to filter and view data.








[...] [...]
ReplyDelete