HandsOn Using Builtin VBA Functions

1. In the Database window of the currently open Acc2003_Chap02.mdb file, click the Modules button, and then click New to create a new standard module.

2. In the Code window, enter the name of the procedure: Sub NewFolder().

3. Click the Enter key. Visual Basic will enter the ending keywords: End Sub.

4. Press F2 to activate the Object Browser.

5. Click the drop-down arrow in the Project/Library list box and select VBA.

6. Enter file in the Search Text box and press Enter.

7. Scroll down in the Members list box and highlight the MkDir method.

8. Click the Copy button in the Object Browser window to copy the selected method name to the Windows clipboard.

9. Close the Object Browser window and return to the Code window. Paste the copied instruction inside the NewFolder procedure.

10. Now, enter a space, followed by "C:\Study". Be sure to enter the name of the entire path in the quotation marks. The NewFolder procedure is shown below.

Sub NewFolder()

MkDir "C:\Study" End Sub

11. Choose Run | Run Sub/UserForm to run the NewFolder procedure.

Introduction to Access 2003 VBA Programming

When you run the NewFolder procedure, Visual Basic creates a new folder on drive C. To see the folder, activate Windows Explorer. After creating a new folder, you may realize that you don't need it after all. Although you could easily delete the folder while in Windows Explorer, how about getting rid of it programmatically?

The Object Browser contains many other methods that are useful for working with folders and files. The RmDir method is just as simple to use as the MkDir method. To remove the Study folder from your hard drive, replace the MkDir method with the RmDir method and rerun the NewFolder procedure. Or create a new procedure called RemoveFolder, as shown below:

Sub RemoveFolder()

RmDir "C:\Study" End Sub

When writing procedures from scratch, it's a good idea to consult the Object Browser for names of the built-in VBA functions.

0 0

Post a comment