Learn Microsoft Access Advanced Programming Techniques, Tips and Tricks.

Join Split Array Functions

Introduction

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 values to each element within the array.

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
Next
  1. The first statement in the above program defines a variable named varNumber as a Variant Type Array for 6 elements.

  2. Another variable j is defined as an integer type that will be used as an index variable in the For. . .Next loop.

  3. 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

If we attempt to assign a value to element varNumber(6), then it will run into an error because we have not dimensioned the Array beyond the varNumber(5) element.

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.

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 Type values to different elements of the same array.  Again, we have used constant values to assign to the array.

This Function has several parameters to pass 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
Next

Join() Function:

The above task takes only one statement with the Join() Function:

strWeeks = Join(varNumber,",")

The first parameter to the Join() Function is the array of values to be joined together to form a string.  The second parameter is the item separator character; comma (,) if omitted, a space character will be used as a separator character by default; otherwise, whatever character 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 item, 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.

Earlier Post Link References:

Share:

1 comment:

Comments subject to moderation before publishing.

PRESENTATION: ACCESS USER GROUPS (EUROPE)

Translate

PageRank

Post Feed


Search

Popular Posts

Blog Archive

Powered by Blogger.

Labels

Forms Functions How Tos MS-Access Security Reports msaccess forms Animations msaccess animation Utilities msaccess controls Access and Internet MS-Access Scurity MS-Access and Internet Class Module External Links Queries Array msaccess reports Accesstips WithEvents msaccess tips Downloads Objects Menus and Toolbars Collection Object MsaccessLinks Process Controls Art Work Property msaccess How Tos Combo Boxes Dictionary Object ListView Control Query VBA msaccessQuery Calculation Event Graph Charts ImageList Control List Boxes TreeView Control Command Buttons Controls Data Emails and Alerts Form Custom Functions Custom Wizards DOS Commands Data Type Key Object Reference ms-access functions msaccess functions msaccess graphs msaccess reporttricks Command Button Report msaccess menus msaccessprocess security advanced Access Security Add Auto-Number Field Type Form Instances ImageList Item Macros Menus Nodes RaiseEvent Recordset Top Values Variables Wrapper Classes msaccess email progressmeter Access2007 Copy Excel Export Expression Fields Join Methods Microsoft Numbering System Records Security Split SubForm Table Tables Time Difference Utility WScript Workgroup database function msaccess wizards tutorial Access Emails and Alerts Access Fields Access How Tos Access Mail Merge Access2003 Accounting Year Action Animation Attachment Binary Numbers Bookmarks Budgeting ChDir Color Palette Common Controls Conditional Formatting Data Filtering Database Records Defining Pages Desktop Shortcuts Diagram Disk Dynamic Lookup Error Handler External Filter Formatting Groups Hexadecimal Numbers Import Labels List Logo Macro Mail Merge Main Form Memo Message Box Monitoring Octal Numbers Operating System Paste Primary-Key Product Rank Reading Remove Rich Text Sequence SetFocus Summary Tab-Page Union Query User Users Water-Mark Word automatically commands hyperlinks iSeries Date iif ms-access msaccess msaccess alerts pdf files reference restore switch text toolbar updating upload vba code