How to Programmatically Add Projects to the SPEED Ferret Workspace
Article ID: SFKB0027
Last Reviewed:   April 10, 2001
Applies To:   SPEED Ferret 4.0

Summary

You can use SPEED Ferret's Automation interface to programmatically add projects to the SPEED Ferret workspace. This might be helpful in a situation where you periodically create a large number of databases that you later wish to inspect using SPEED Ferret. Adding the projects programmatically saves you time and ensures that you don't overlook any projects.

Code Sample

The following code sample demonstrates how to programmatically add a collection of Access 2000 databases to the SPEED Ferret workspace. This code locates all MDB files located in a specified folder and its subfolders. It is assumed that each MDB file is an Access 2000 database.

Sub AddProjects()

  Const cRootFolder = "c:\"
  Const cNamePrefix = "auto_"

  Dim FileList() As String
  Dim FileCount As Long
  Dim i As Long
  Dim Workspace As sflib.Workspace
  Dim ProjectName As String
  Dim Project As sflib.Project

' Create the SPEED Ferret workspace object.

  Set Workspace = New sflib.Workspace

' Delete all existing projects having the specified name prefix.
' This prevents deletion of projects added manually.

  For i = Workspace.Projects.Count - 1 To 0 Step -1
    Set Project = Workspace.Projects.Item(i)
    If Project.Name Like cNamePrefix + "*" Then Workspace.Projects.Remove i, False
  Next

' Add all projects that are found in or under the specified root directory.

  EnumFolders cRootFolder, FileList(), FileCount
  For i = 0 To FileCount - 1
    ProjectName = cNamePrefix + MakeProjectName(FileList(i))
    Set Project = Workspace.Projects.Item(ProjectName)
    If Project Is Nothing Then Set Project = Workspace.Projects.Add("MSAC90", ProjectName)
    Project.Parameters = "Database=""" + FileList(i) + """;"
    Project.Parameters = Project.Parameters + "Workgroup=""c:\winnt\system32\system.mdw"";"
    Project.Save
  Next

End Sub

Sub EnumFolders(ByVal Folder As String, FileList() As String, FileCount As Long)

  Dim FileName As String
  Dim SubfolderList() As String
  Dim SubfolderCount As Long
  Dim i As Long

  If Right$(Folder, 1) <> "\" Then Folder = Folder + "\"

' List all MDB files in the specified folder.

  FileName = Dir$(Folder + "*.*", vbNormal + vbDirectory)
  Do While Len(FileName) > 0
    If FileName <> "." And FileName <> ".." Then
      If GetAttr(Folder + FileName) And vbDirectory Then
        ReDim Preserve SubfolderList(SubfolderCount)
        SubfolderList(SubfolderCount) = Folder + FileName
        SubfolderCount = SubfolderCount + 1
      ElseIf (FileName Like "*.mdb") Then
        ReDim Preserve FileList(FileCount)
        FileList(FileCount) = Folder + FileName
        FileCount = FileCount + 1
      End If
    End If
    FileName = Dir$()
  Loop

' List all MDB files in the subfolders.

  For i = 0 To SubfolderCount - 1
    EnumFolders SubfolderList(i), FileList(), FileCount
  Next

End Sub

Function MakeProjectName(ByVal Path As String)

' Converts a complete file path into a valid project name by
' converting \ to _ and removing all other invalid characters.
' Modify this routine if desired to make the names look the way you want them to.

  Const cValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQUSTUVWXYZ0123456789_"

  Dim c As String
  Dim i As Long
  Dim Result As String

  For i = 1 To Len(Path)
    c = Mid$(Path, i, 1)
    If InStr(cValidChars, c) <> 0 Then
      Result = Result + c 'omit invalid chars
    ElseIf c = "\" Then
      Result = Result + "_"
    End If
  Next
  MakeProjectName = Result

End Function

Notes

  • In order to run this code you will need to add a reference to the SPEED Ferret 4.0 Object Library. In Access, go to Tools > References and check the box next to SPEED Ferret 4.0 Object Library.
  • A name prefix is used to keep the programmatically added projects separate from other projects added manually.
  • MSAC90 is the name of the Access 2000 interface. This is used as the first parameter to the Projects.Add method. To add projects of a different type, use a different interface name.
  • When adding a project, it is also necessary to supply a project parameter string. The format of this string is driver-dependent. For the Access 2000 driver, the parameter string has the format "Database={database_path};Workgroup={workgroup_path}. Example: Database="c:\MyStuff\MyDatabase.mdb";Workgroup="c:\winnt\system32\system.mdw".
  • To browse the entire SPEED Ferret object model, press F2 to open the Object Browser. Then select the sflib library in the combo box at the upper left.