Introduction

I have spent numerous hours searching for solutions to programming problems that I think should have a more accessible solution. This blog will contain my experiences and the solutions that I found. Your comments are welcome of course.

Thursday, January 24, 2008

Get a Tree View of Folder Structure using Visual Basic 2005

I wanted to create a form that contains a tree view. The tree view would then build a list of all the folders on a particular drive selected. I ran into a few problems with writing this code. Mainly, that when the program tries to read information from the "System Volume Information" folder it returns an exception. The following code shows how to get around this problem and create the list.
The tree generation is started by clicking a button called btnRead. I also have a control on the form called tvFolders (TreeView Folders).

Imports System.IO

Private Sub BuildFolderTree(ByVal frmNode As Tree, ByVal curPath As String)

Dim curDirInfo As DirectoryInfo
Dim newFolder As TreeNode
Dim curFolder As String
Dim subFolder As String

Try
For Each curFolder In My.Computer.FileSystem.GetDirectories(curPath)
subFolder = My.Computer.FileSystem.GetDirectories(curFolder)
curDirInfo = My.Computer.FileSystem.GetDirectoryInfo(curFolder)
If Not (curDirInfo.Attributes = 22 Or curDirInfo.Attributes = FileAttributes.Hidden) Then

If frmNode Is Nothing Then

newFolder = tvFolders.Nodes.Add(subFolder)

Else

newFolder = frmNode.Nodes.Add(subFolder)

End If
BuildFolderTree(newFolder, My.Computer.FileSystem.CombinePath(curPath, subFolder))

End If

Next curFolder

Catch curEx As Security.SecurityException
' You can put a warning message here or just ignore it.
Catch curEx As UnauthorizedAccessException
' You can put a warning message here or just ignore it.

System.Windows.Forms.Application.DoEvents()

End Sub

This code shows also how Recursive Programming works. In order for the tree to be built, the program has to recursively go through each folder and check and see if it has sub folders. If it does a tree has to be built for that subfolder as well. This is the reason for having a BuildFolderTree sub procedure call in the middle of the sub procedure.

One Part I am not sure about is the curDirInfo.Attributes = 22. I did some research to find out why the attribute is equal to 22 but I could not find anything about a Folder Attribute equaling 22. The list of folder Attributes are as follows and doesn't include a 22.

Normal 0
ReadOnly 1
Hidden 2
System 4
Volume 8
Directory 16
Archive 32
Alias 64
Compressed 128

Wednesday, January 23, 2008

Get a List of Access Objects Using Visual Basic 2005

I am writing a program in visual basic 2005 that would perform a number of file system operations. Find the number of files on a drive, the number of folders on a drive or in a specific folder, and mainly find Old Files on a network drive.

I thought that it would be very beneficial to get information about Access Databases and Excel Spreadsheets on the drives as well to eliminate duplicates.

I have a button on the main form of the program (screen shot will be provided soon) that searches the drive for all Access Database (another snippet of code about that later). The program fills a list box with the names of all the Access Databases it finds in a specific location. From the list, I wanted the user to be able to click a button and then get a complete list of tables, queries, modules, and other objects in that database. I wanted this to be done instantly.

This can be achieved in one of two ways:

First Method
1. Click Project.
2. Click Add Reference...
3. Click the COM Tab
4. Select "Microsoft ActiveX Data Objects 2.5 Library"
5. Select "Microsoft ADO Ext. 2.8 for DDL and Security"
6. Click Ok

Use the following Code:

Imports ADODB
Imports ADOX

Dim conn As New ADODB.Connection
Dim cat As New ADOX.Catalog
Dim Table As ADOX.Table
Dim Qry As ADOX.View
Dim DBName As string

DBName = "C:\temp.mdb"

conn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source= " & DBName & " ")
cat.ActiveConnection = AccConn


For Each Table in cat .Tables
lstBox.Items.Add(Table.Name)
lstBox.Items.Add(Table.Type)
Next


For Each Qry in cat.Views
lstBox.Items.Add(Qry.Name)
lstBox.Items.Add(Qry.Command)
Next


Second Method
The second method uses a SQL Query to the Access Database that reads the information directly from the table called MSysObjects, MySysAccessObjects, MySysQueries.

These are system tables that can be queries directly.

for example
SELECT
MsysObjects.Id,
MsysObjects.ForeignName,
MsysObjects.DateCreate,
MsysObjects.Database,
MsysObjects.Connect,
MsysObjects.Name,
MsysObjects.Owner,
MsysObjects.Type

FROM
MsysObjects;


This Code will give you the list of all objects in the System Objects table. You can filter it according to what you're looking for. Then use that information inside of your VB Code. (Another Snippet coming later).