home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


To find out what file the user wants to work with, you check the Common Dialog’s FileName property after the user clicks on OK in the dialog box. That property holds the fully qualified (that is, with the path) name of the file to open. If you just want the file’s name, use the FileTitle property.

Here’s an example. In this case, we’ll let the user select a file to open, and then display the file’s name and path in a message box.

Add a Common Dialog control to a form and set the control’s CancelError property to True so we can check if the user clicked the Cancel button. To check that, we use On Error GoTo:

Private Sub Command1_Click()
   On Error GoTo Cancel
...
Cancel:
End Sub

Then we display the Open dialog box:

Private Sub Command1_Click()
   On Error GoTo Cancel
   CommonDialog1.ShowOpen
...
Cancel:
End Sub

Finally, assuming the user clicked OK, we display the name of the file the user selected in a message box using the FileName property:

Private Sub Command1_Click()
   On Error GoTo Cancel
   CommonDialog1.ShowOpen
   MsgBox "File to open: " & CommonDialog1.FileName
Cancel:
End Sub

When you run this code and click the button, the Open dialog box appears, as in Figure 17.1.


Figure 17.1  The Open dialog box.

If you make a file selection and click on OK, the Open dialog box closes and the program displays the name of the file you selected, and its path, in a message box, as shown in Figure 17.2.


Figure 17.2  Getting a file to open from the user.

Creating A File

The Testing Department is on the phone again. Your new SuperDuperTextPro word-processing program is great, but shouldn’t it offer users some way to save their text in a file? Hmm, you think, could be a good idea.

So how do you create a file in Visual Basic? The standard way is to use the Open statement (we’ll see another way when we work with TextStream objects later in this chapter). Here’s how the Open statement works:

Open pathname For mode [Access access] [lock] As [#] filenumber [Len= reclength]

Here are what the various arguments mean:

  pathname—A file name (may include directory or folder, and drive).
  mode—A keyword specifying the file mode: Append, Binary, Input, Output, or Random (if unspecified, the file is opened for Random access).
  access—A keyword specifying the operations permitted on the open file: Read, Write, or Read Write.
  lock—A keyword specifying the operations restricted on the open file by other processes: Shared, Lock Read, Lock Write, and Lock Read Write.
  filenumber—A valid file number in the range 1 to 511, inclusive. Use the FreeFile function to obtain the next available file number.
  reclength—A number less than or equal to 32,767 (bytes). For files opened for random access, this value is the record length. For sequential files, this value is the number of characters buffered.

If the file is already opened by another process and the specified type of access is not allowed, the Open operation fails and an error occurs. Also note that the Len clause is ignored if mode is Binary.

So how do you create a file with Open? If the file specified by pathname doesn’t exist, it is created when a file is opened for Append, Binary, Output, or Random modes. After you’ve created the file, you refer to it using the file number.

Let’s see an example. Here, we’ll let users write the text in a text box, Text1, to a file on disk, file.txt, when they press a button. Because file operations are prone to error (we might run into missing diskettes, locked files, and so on), we start by checking for errors:

Private Sub Command1_Click()
   On Error GoTo FileError
...
FileError:
   MsgBox "File Error!"
End Sub

Next, we create file.txt as file #1:

Private Sub Command1_Click()
   On Error GoTo FileError
   Open "c:\file.txt" For Output As #1
...
FileError:
   MsgBox "File Error!"
End Sub

Now we write the text in Text1 to the file with the Print # method:

Private Sub Command1_Click()
   On Error GoTo FileError
   Open "c:\file.txt" For Output As #1
   Print #1, Text1.Text
...
FileError:
   MsgBox "File Error!"
End Sub

And finally we close the file:

Private Sub Command1_Click()
   On Error GoTo FileError
   Open "c:\file.txt" For Output As #1
   Print #1, Text1.Text
   Close #1
   Exit Sub

FileError:
   MsgBox "File Error!"
End Sub

When you add a text box, Text1, to the form, and a command button, Command1, labeled “Write text to file”, and run the program, you see the display much like that in Figure 17.3. When you click the command button, the new file is created and written.


Figure 17.3  Writing text to a file.


TIP:  We should note that each open file needs its own unique file number; you can use the FreeFile function to return the next available free file number. You use FreeFile like this: FreeFile[(rangenumber)]. Here, the optional rangenumber argument is a variant that specifies the range from which the next free file number is to be returned. Pass a 0 (default) to return a file number in the range 1 to 255. Specify a 1 to return a file number in the range 256 to 511.

Getting A File’s Length

When you start reading files in code, it can help to know the file’s length (for one thing, it can tell you how many bytes to read in). There are two ways to determine file length, the FileLen and the LOF functions.

The FileLen Function

The FileLen function returns the length of a file (in bytes) on disk. Here’s an example in which we report the size of a file, file.txt, in a message box using FileLen:

Private Sub Command1_Click()
   MsgBox "The file.txt file is" & Str(FileLen("c:\file.txt")) & _
       " bytes long."
End Sub

Running this code gives a result such as you see in Figure 17.4.


Figure 17.4  Reporting a file’s length.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.