Learn Microsoft Access Advanced Programming Techniques, Tips and Tricks.

Monday, November 22, 2010

Join Split Array Functions-2

Continued from last week's topic.

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

  1. If you haven’t reviewed the fundamentals of the functions mentioned above, I recommend doing so by following the link provided earlier before continuing.

    Since the Join() and Split() functions are closely related, this is a good time to explore a real-world example that demonstrates their use. While the approach we are about to take may not be the simplest solution to the problem at hand, it will clearly illustrate how these functions work and help deepen your understanding of their practical applications.

  2. Import Employees Table from the Northwind sample database: C:\Program Files\Microsoft Office\Office11\samples\Northwind.mdb 

  3. Open a new Query in SQL View (don't select any Table from the displayed list).

  4. Copy and paste the following SQL String into the SQL editing window and save the Query with the name EmployeeSelectQ

    .
    SELECT Employees.*
    FROM Employees;
    
  5. Design a new Form similar to the sample image shown below. You can use the Form Wizard to create it quickly, and set EmployeeSelectQ as the record source for the Form. 

  6. Add a Text Box and a CommandButton at the bottom of the design.

  7. Select the Text Box and display the Property Sheet (View ->Properties or Design ->Property Sheet in 2007).

  8. Change the Name Property Value to txtCodes.

  9. Select the CommandButton, change the Name Property value to cmdFilter, and the Caption Property value to Apply Filter.

  10. Select the Child label of the Text Box and change its Caption value to Employee Codes:.

  11. Display the VBA Code Module of the Form (View ->Code or click the View Code toolbar button from the Design Menu of 2007).

  12. 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
  13. Press Alt+F11 to open the VBA Editing window. If you have already closed it, select Module from the Insert Menu to add a new Standard Module.

  14. 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
  15. Open the EmployeeSelect Form in a normal view. The sample image of the Form is given below:

  16. Enter Employee IDs 2, 3, 7, and click on the Apply Filter Command Button.

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

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

The filter action is 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 TextBox is empty.

You may try it out with a different Employee ID.

The values entered into the TextBox must be within the range of the available Employee Code. The numbers entered outside this range will display an Error message and abort the program.

No comments:

Post a Comment

Comments subject to moderation before publishing.

Powered by Blogger.