Microsoft Access
  Home arrow Microsoft Access arrow Page 3 - Advanced Ideas using VBA
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MICROSOFT ACCESS

Advanced Ideas using VBA
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2007-01-04

    Table of Contents:
  • Advanced Ideas using VBA
  • 7.7 Sort an Array in VBA
  • 7.8 Fill a List Box with a List of Files
  • 7.9 Handle Object Properties, in General
  • 7.10 Detect Whether an Object Exists

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Advanced Ideas using VBA - 7.8 Fill a List Box with a List of Files


    (Page 3 of 5 )

    Problem

    You need to present your users with a sorted list of files with a specific filename extension in a particular directory. You found the Dir function, but you can't find a way to get this information into a list box. Is there a way to do this?

    Solution

    This problem provides the perfect opportunity to use the past three solutions. It involves creating a list-filling callback function, passing arrays as parameters, and sorting an array. In addition, you'll fill that array with a list of files matching a particular criterion, using the Dir function.

    Load the form frmTestFillDirList from 07-08.MDB. Enter a file specification into the text box (for example, c:\*.exe). Once you leave the text box (by pressing either Tab or Return), the code attached to the AfterUpdate event will force the list box to requery. When that happens, the list box will fill in with the matching filenames. Figure 7-12 shows the results of a search for c:\\*.*.

    To include this functionality in your own applications, follow these steps:

    1. On a form, create a text box and a list box, with properties set as shown in Table 7-6.


      Figure 7-12.  frmTestFillDirList, searching for *.* in the C:\folder

      Table 7-6. Property settings for the controls on the directory list form



      Control

      Property

      Setting

      Text box

      Name

      txtFileSpec

       

      AfterUpdate

      [Event Procedure]

      List box

      Name

      lstDirList

       

      RowSourceType

      FillList

       

      AfterUpdate

      [Event Procedure]


      1. Enter the following code in the text boxs AfterUpdate event procedure. (See the Preface for more information on creating event procedures.) This code forces the list box to requery itself when you enter a value in the text box, and then move to some other control:

        Sub txtFileSpec_AfterUpdate ( )  
           Me.lstDirList.Requery
        End Sub
         
      2. Enter the following code in the list box's AfterUpdate event. This is sample code that pops up a message box indicating which file you chose:

        Sub lstDirList_AfterUpdate ( )
           MsgBox "You chose: " & Me.lstDirList.Value
        End Sub
         
      3. Enter the following code into a global module so that it can be called from any form. Though this code would work fine in a form's module, it's general enough that it will serve you best as part of a global module that can be copied from one database to another. This is the function that fills the array of files:

        Public Function FillDirList(ByVal strFileSpec As String, _
         astrFiles() As String) As Integer
              ' Given the file specification in strFileSpec, fill in the
              ' dynamic array passed in avarFiles().

              Dim intNumFiles As Integer
              Dim strTemp As String

              On Error GoTo HandleErr 
              intNumFiles = 0

              ' Set the filespec for the dir() and get the first filename.
              strTemp = Dir(strFileSpec)
              Do While Len(strTemp) > 0
                  intNumFiles = intNumFiles + 
        1
                  astrFiles(intNumFiles - 1) = strTemp
                  strTemp = Dir
              Loop

        ExitHere:
            If intNumFiles > 0 Then
               ReDim Preserve astrFiles(intNumFiles - 1)
               acbSortArray astrFiles()
            End If
            FillDirList = intNumFiles
            Exit Function

        HandleErr:
            Select Case Err.Number
                Case 9
                   ' The array needs to be resized
                   ' Just add room for 100 more files.
                   ReDim Preserve astrFiles(intNumFiles + 100)
                   Resume
                Case Else
                   FillDirList = intNumFiles 
                   Resume ExitHere
            End Select
          End Function

        Rather than resizing the array for each matching file name, the Fill-DirList function traps the error that occurs when the array is full, and resizes it 100 slots at a time. Using the Redim Preserve statement is quite expensive in VBA, and you should consider looking for ways to call it as seldom as possible. In this example, the code resizes the array to the correct size once it's done filling in all the file names. 
      4. Import basSortArray from 07-08.MDB. This is the same sorting code that we used in the Solution in Recipe 7.7.

      Discussion

      The list box in this example uses a list-filling callback function, FillList, to supply its data. (See the Solution in Recipe 7.5 for information on callback functions.) Here's the code:

        Private Function FillList(ctl As Control, _
        
      varID As Variant, lngRow As Long, lngCol As Long, _
        
      intCode As Integer)
           
      Static astrFiles() As String
           
      Static intFileCount As Integer

            Select Case intCode
               
      Case acLBInitialize
                   
      If Not IsNull(Me.txtFileSpec) Then
                       
      intFileCount = FillDirList(Me.txtFileSpec, astrFiles())
                    End If
                    FillList = True

                Case acLBOpen
                    FillList = Timer

                Case acLBGetRowCount
                    FillList = intFileCount

                Case acLBGetValue
                    FillList = astrFiles(lngRow)

                Case acLBEnd
                    Erase astrFiles
            End Select
        End Function

      In FillList's acLBInitialize case, it calls the FillDirList function to fill in the astrFiles array, based on the value in the txtFileSpec text box. FillDirList fills in the array, calling acbSortArray along the way to sort the list of files, and returns the number of files it found. Given that completed array, FillList can return the value from the array that it needs when requested in the acLBGetValue case. It uses the return value from
      FillDirList, the number of files found, in response to the acLBGetRowCount case.

      There's also an interesting situation you should note in the FillList and FillDirList routines. FillList declares a dynamic array, astrFiles, but doesn't give a size because it doesn't yet know the number of files that will be found. FillList passes the array off to FillDirList, which adds filenames to the array based on the file specification until it doesn't find any more matches. FillDirList returns the number of matching filenames, but it also has the side effect of having set the array's size and filled it in. Here's the code that does the work. This code fragment uses the ReDim Preserve keywords to resize the array every time it finds a matching filename:

        ' Set the filespec for the dir() and get the first filename.
        strTemp = Dir(strFileSpec)
       
      Do While Len(strTemp) > 0
           
      intNumFiles = intNumFiles + 1
           
      astrFiles(intNumFiles - 1) = strTemp
           
      strTemp = Dir
        Loop

      FillDirList uses the Dir function to create the list of files. This function is unusual in that you call it multiple times. The first time you call it, you send it the file
      specification you're trying to match, and Dir returns the first matching filename. If it returns a nonempty value, you continue to call it, with no parameters, until it does return an empty value. Each time you call Dir, it returns the next matching filename.

      Once FillDirList has finished retrieving the list of filenames, it sorts the names in the array. Its return value is the number of files it found. The following code shows how this works:

        If intNumFiles > 0 Then
           
      ReDim Preserve astrFiles(intNumFiles - 1)
           
      acbSortArray astrFiles()
        End If
        FillDirList = intNumFiles

      Note that when Access calls the list-filling callback function, values for the lngRow  and lngCol parameters are always zero-based. Therefore, when you use arrays within callback functions, you should always consider using zero-based arrays to hold the data you'll display in the control. If you don't, you'll always be dealing with "off by one" errors. Using a zero-based array will mean that the row values (sent to your code in lngRow) will match your array indices.

      More Microsoft Access Articles
      More By O'Reilly Media


         · This article is an excerpt from the book "Access Cookbook, Second Edition,"...
       

      Buy this book now. This article is excerpted from chapter seven of the Access Cookbook, Second Edition, written by Ken Getz, Paul Litwin, and Andy Baron (O'Reilly, 2005; ISBN: 0596006780). Check it out today at your favorite bookstore. Buy this book now.

    MICROSOFT ACCESS ARTICLES

    - Linking SQL Express 2005 Tables to MS Access...
    - Working with Access Projects in Access 2007
    - Exploring Access 2007
    - Working with Stored Procedures in an MS Acce...
    - Creating and Using Action Queries
    - Creating Data Access Pages with Charts using...
    - Advanced Ideas using VBA
    - VBA Details
    - Updating Records in MS Access
    - Using ADO`s Record Object with URLs
    - Exporting XML from MS Access 2003
    - Importing XML into MS Access 2003
    - On Using Pass-through Queries in MS Access
    - Distributed Queries in MS Access
    - Configuring a Linked Microsoft Access Server...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway