Learn Microsoft Access Advanced Programming Techniques, Tips and Tricks.

Join Split Array Functions-2

This is the continuation of last week's Article: Join Split Array Functions

If you have not gone through the fundamentals of the above functions then please do that, by following the above link, before continuing.

Since, Join() and Split() Functions are related, It is time to try out a real world example with them.  It may not be the easiest solution to the problem we are trying to solve, but it will help to understand these functions' usage better.

  1. Import Employees Table from the Northwind sample database: C:\Program Files\Microsoft Office\Office11\samples\Northwind.mdb 
  2. Open a new Query in SQL View (don't select any Table from the displayed list).
  3. Copy and paste the following SQL string into the SQL editing window and save the Query with the name EmployeeSelectQ.
    SELECT Employees.*
    FROM Employees;
    
  4. Design a new Form like the sample image giving below (you may use the Form Wizard to quickly create the Form) using EmployeeSelectQ Query as Record Source. 
  5. Add a Text Box and a Command Button at the bottom of the design.
  6. Select the Text Box and display the Property Sheet (View - ->Properties or Design - ->Property Sheet in 2007).
  7. Change the Name Property Value to txtCodes.
  8. Select the Command Button, change the Name Property value to cmdFilter and the Caption Property value to Apply Filter.
  9. Select the Child label of the Text Box and change its Caption value to Employee Codes:.
  10. Display the VBA Code Module of the Form (View - ->Code or click View Code toolbar button from Design Menu  of 2007).
  11. Copy and paste the following VBA Code into the Form Module and save the Form with the name EmployeeSelect:
    Private Sub cmdFilter_Click()
    Dim txt_Codes As String, varList As Variant
    Dim varCodes As Variant, j As Integer, x
    Dim maxCodes As Variant, invalid_Codes As String
    
    txt_Codes = Nz(Me.txtCodes, "")
    maxCodes = DMax("EmployeeID", "Employees")
    
    If Len(txt_Codes) = 0 Then
      varList = Null
    Else
       'Split the items into the array.
      
      '(Here Array() Function will not work
      'becuase the values in txtCodes variable is a String
      'and will be treated as a single item).
    
      varCodes = Split(txt_Codes, ",")
      
      'Validation check
      invalid_Codes = ""
      For j = 0 To UBound(varCodes)
         x = Val(varCodes(j))
         If x < 1 Or x > maxCodes Then
            invalid_Codes = invalid_Codes & Format(x, "0 ")
         End If
      Next
      If Len(invalid_Codes) > 0 Then
         MsgBox "Invalid Employee Codes: " & invalid_Codes & vbCr & vbCr & "Correct and retry."
         Exit Sub
      End If
      'here "varList = txt_Codes" is also works
      'because txt_Codes values are separated with commas already
      varList = Join(varCodes, ",")
    End If
    
      'Call the EmployeeFilter() function
      EmployeeFilter varList
      Me.RecordSource = "EmployeeSelectQ"
      Me.Requery
    
    End Sub
  12. Press Alt+F11 to open the VBA Editing window, if you have already closed it, select Module from Insert Menu to add a new Standard Module.
  13. Copy and paste the following VBA Code of EmployeeFilter() Function into the Standard Module and save it:
    Public Function EmployeeFilter(ByVal Criteria As Variant)
    Dim strsql0 As String, sql As String
    Dim db As Database, qryDef As QueryDef
    
    strsql0 = "SELECT Employees.* FROM Employees WHERE (((Employees.EmployeeID) In ("
    
    Set db = CurrentDb
    Set qryDef = db.QueryDefs("EmployeeSelectQ")
    
    If IsNull(Criteria) Or Len(Criteria) = 0 Then
        sql = "SELECT Employees.* FROM Employees;"
    Else
        Criteria = Criteria & ")));"
        sql = strsql0 & Criteria
    
    End If
        qryDef.SQL = sql
        db.QueryDefs.Refresh
    
    End Function
  14. Open the EmployeeSelect Form in normal view. The sample image of the Form is given below:
  15. Enter the EmployeeIDs 2,3,7 and click on the Apply Filter Command Button.

    Now, the EmployeeSelect Form have only three records, with employee codes 2,3 & 7.

  16. Delete all the Text Box contents and click on the Apply Filter Command Button.

The filter action is now reversed and all the records of the Employee Table are now available on the Form.  In other words it works like a Reset command when the Text Box is empty.

You may try with different EmployeeIDs list.

The values entered into the Text Box must be within the range from 1 to Maximum Value of Employee Code. Out of Range numbers will display an Error message and abort the program.

Share:

No comments:

Post a Comment

Comments are subject to moderation before publishing.

Translate



PageRank
Subscribe in a reader
Your email address:

Delivered by FeedBurner

Search

Popular Posts

Search This Blog

Blog Archive

Powered by Blogger.

Follow by Email

Labels

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

Featured Post

WithEvents and Report Line Highlighting

Introduction This is really a re-run of an earlier Post: Highlighting Reports published during August, 2007.  The full Code was written on ...

Labels

Blog Archive

Recent Posts