As opposed to saving a document, Microsoft Windows
provides the Open File dialog box used to open a file. To display it, call
the FileDialog() method of the Application class and pass
the argument as msoFileDialogOpen). Here is an example:
Private Sub cmdFileDialog_Click()
Dim Action As Integer
Dim dlgOpenFile As FileDialog
Set dlgOpenFile = Application.FileDialog(msoFileDialogOpen)
Action = dlgOpenFile.Show
End Sub
To let you create a file, the VBA language provides a
procedure named Open. Its syntax is:
Open PathName For Input [Access access] [lock] As [#]FileNumber [Len=RecordLength]
The Open word, the For Input expression,
and the As # combination are required. The first argument,
PathName, is required. This is a string that can be the name of the
file. The file can have an extension or not. The mode is required. To
open a file, the mode argument can be:
- Binary: The file will be opened and its value(s) would be
read as (a) binary value(s)
- Append: The file will be opened and new values can be added
to the end of the existing values
- Input: The file will be opened normally
- Random: The file will be opened for random access
The access argument is optional and can have one
of the following values:
- Read: After the file has been opened, values will be read
from it
- Read Write: Whether the file was created or opened, values
can be read from it and/or written to it
If you decide to specify the access argument,
precede its value with the Access keyword. The lock parameter
is optional and its possible values are:
- Shared: Other applications can access this file while the
current application is accessing it
- Lock Read: Other applications are not allowed to access this
file while the current application is reading from it
- Lock Read Write: Other applications are not allowed to access
this file while the current application is using it
On the right side of #, type a number for the
FileNumber, between 1 and 511. Use the same rules/description we saw for
creating a file.
The RecordLength parameter is optional. If the
file was opened, this argument specifies the length of the record that was
read.
To support the ability to open a file, the VBA language
provides two procedures. If you wrote the values using the Print
statement, to read the values, use the Input or the Line Input
statement. The syntax of the Input procedure is:
Input #FileNumber, VariableList
The FileNumber argument is the FileNumber
parameter you would have used to open the file. The FileNumber is
followed by a comma. The VariableList argument can be made of 1 or
more parts. To read only one value, after the comma of the FileNumber
argument, type the name of the variable that will receive the value. You can
read each value on its own line. One of the better uses of the Input
statement is the ability to read many values using a single statement. To do
this, type the variables on the same Input line but separate them
with commas. Here is an example:
Private Sub cmdOpen_Click()
Dim FirstName As String
Dim LastName As String
Dim IsFullTimeEmployee As Boolean
Open "Employee.txt" For Input As #1
Input #1, FirstName, LastName, IsFullTimeEmployee
Close #1
End Sub
If you have a file that contains many lines, to read one
line at a time, you can use the Line Input statement. Its syntax is:
Line Input #FileNumber, VariableName
This statement takes two arguments and both are
required. The FileNumber is the number you would have used to open
the file. When the Line Input statement is called, it reads a line of
text until it gets to the end of the file. One of the limitations of the
Line Input statement is that it has a hard time reading anything other
than text because it may not be able to determine where the line ends.
Practical
Learning: Introducing File Opening
|
|
- In the Navigation Pane, right-click the NewRepairOrder form and
click Copy
- Right-click an empty area in the Navigation Pane and click Paste
- Set the name of the form to OpenRepairOrder and click OK
- In the Navigation Pane, right-click the OpenRepairOrder form and
click Design View
- Change the design of the form as follows:
|
Control |
Name |
Caption |
Other Properties |
Button |
|
cmdOpenRepairOrder |
Open Repair Order |
|
Button |
|
cmdSaveRepairOrder |
Save Repair Order |
Enabled: No |
Button |
|
cmdClose |
Close |
|
|
- Double-click the button at the intersection of the rulers
- In the Properties window, click Event and double-click On Load
- Click the ellipsis button and change the file as follows:
Option Compare Database
Private strFileName As String
Private Sub Form_Load()
strFileName = ""
End Sub
- In the object combo box, select cmdOpenRepairOder
- Implement the event as follows:
Private Sub cmdOpenRepairOder_Click()
On Error GoTo cmdOpenRepairOder_Error
Dim Action As Integer
Dim dlgOpenFile As FileDialog
Dim CustomerName As String, Address As String
Dim City As String, State As String
Dim ZIPCode As String, Make As String
Dim Model As String, CarYear As String
Dim ProblemDescription As String
Dim PartName1 As String, UnitPrice1 As String
Dim Quantity1 As String, SubTotal1 As String
Dim PartName2 As String, UnitPrice2 As String
Dim Quantity2 As String, SubTotal2 As String
Dim PartName3 As String, UnitPrice3 As String
Dim Quantity3 As String, SubTotal3 As String
Dim PartName4 As String, UnitPrice4 As String
Dim Quantity4 As String, SubTotal4 As String
Dim PartName5 As String, UnitPrice5 As String
Dim Quantity5 As String, SubTotal5 As String
Dim JobName1 As String, JobCost1 As String
Dim JobName2 As String, JobCost2 As String
Dim JobName3 As String, JobCost3 As String
Dim JobName4 As String, JobCost4 As String
Dim JobName5 As String, JobCost5 As String
Dim TotalParts As String, TotalLabor As String
Dim TotalRepair As String, Recommendations As String
Set dlgOpenFile = Application.FileDialog(msoFileDialogOpen)
Action = dlgOpenFile.Show
If Action <> 0 Then
Open dlgOpenFile.SelectedItems(1) For Input As #1
strFileName = dlgOpenFile.SelectedItems(1)
Caption = "College Park Auto-Repair - Open Repair Order: " & strFileName
cmdSaveRepairOrder.Enabled = True
Input #1, CustomerName
Input #1, Address
Input #1, City
Input #1, State
Input #1, ZIPCode
Input #1, Make
Input #1, Model
Input #1, CarYear
Input #1, ProblemDescription
Input #1, PartName1
Input #1, UnitPrice1
Input #1, Quantity1
Input #1, SubTotal1
Input #1, PartName2
Input #1, UnitPrice2
Input #1, Quantity2
Input #1, SubTotal2
Input #1, PartName3
Input #1, UnitPrice3
Input #1, Quantity3
Input #1, SubTotal3
Input #1, PartName4
Input #1, UnitPrice4
Input #1, Quantity4
Input #1, SubTotal4
Input #1, PartName5
Input #1, UnitPrice5
Input #1, Quantity5
Input #1, SubTotal5
Input #1, JobName1
Input #1, JobCost1
Input #1, JobName2
Input #1, JobCost2
Input #1, JobName3
Input #1, JobCost3
Input #1, JobName4
Input #1, JobCost4
Input #1, JobName5
Input #1, JobCost5
Input #1, TotalParts
Input #1, TotalLabor
Input #1, TotalRepair
Input #1, Recommendations
If CustomerName = "#NULL#" Then
txtCustomerName = ""
Else
txtCustomerName = CustomerName
End If
If Address = "#NULL#" Then
txtAddress = ""
Else
txtAddress = Address
End If
If City = "#NULL#" Then
txtCity = ""
Else
txtCity = City
End If
If State = "#NULL#" Then
txtState = ""
Else
txtState = State
End If
If ZIPCode = "#NULL#" Then
txtZIPCode = ""
Else
txtZIPCode = ZIPCode
End If
If Make = "#NULL#" Then
txtMake = ""
Else
txtMake = Make
End If
If Model = "#NULL#" Then
txtModel = ""
Else
txtModel = Model
End If
If CarYear = "#NULL#" Then
txtCarYear = ""
Else
txtCarYear = CarYear
End If
If ProblemDescription = "#NULL#" Then
txtProblemDescription = ""
Else
txtProblemDescription = ProblemDescription
End If
If PartName1 = "#NULL#" Then
txtPartName1 = ""
Else
txtPartName1 = PartName1
End If
If UnitPrice1 = "#NULL#" Then
txtUnitPrice1 = ""
Else
txtUnitPrice1 = UnitPrice1
End If
If Quantity1 = "#NULL#" Then
txtQuantity1 = ""
Else
txtQuantity1 = Quantity1
End If
If SubTotal1 = "#NULL#" Then
txtSubTotal1 = ""
Else
txtSubTotal1 = SubTotal1
End If
If PartName2 = "#NULL#" Then
txtPartName2 = ""
Else
txtPartName2 = PartName2
End If
If UnitPrice2 = "#NULL#" Then
txtUnitPrice2 = ""
Else
txtUnitPrice2 = UnitPrice2
End If
If Quantity2 = "#NULL#" Then
txtQuantity2 = ""
Else
txtQuantity2 = Quantity2
End If
If SubTotal2 = "#NULL#" Then
txtSubTotal2 = ""
Else
txtSubTotal2 = SubTotal2
End If
If PartName3 = "#NULL#" Then
txtPartName3 = ""
Else
txtPartName3 = PartName3
End If
If UnitPrice3 = "#NULL#" Then
txtUnitPrice3 = ""
Else
txtUnitPrice3 = UnitPrice3
End If
If Quantity3 = "#NULL#" Then
txtQuantity3 = ""
Else
txtQuantity3 = Quantity3
End If
If SubTotal3 = "#NULL#" Then
txtSubTotal3 = ""
Else
txtSubTotal3 = SubTotal3
End If
If PartName4 = "#NULL#" Then
txtPartName4 = ""
Else
txtPartName4 = PartName4
End If
If UnitPrice4 = "#NULL#" Then
txtUnitPrice4 = ""
Else
txtUnitPrice4 = UnitPrice4
End If
If Quantity4 = "#NULL#" Then
txtQuantity4 = ""
Else
txtQuantity4 = Quantity4
End If
If SubTotal4 = "#NULL#" Then
txtSubTotal4 = ""
Else
txtSubTotal4 = SubTotal4
End If
If PartName5 = "#NULL#" Then
txtPartName5 = ""
Else
txtPartName5 = PartName5
End If
If UnitPrice5 = "#NULL#" Then
txtUnitPrice5 = ""
Else
txtUnitPrice5 = UnitPrice5
End If
If Quantity5 = "#NULL#" Then
txtQuantity5 = ""
Else
txtQuantity5 = Quantity5
End If
If SubTotal5 = "#NULL#" Then
txtSubTotal5 = ""
Else
txtSubTotal5 = SubTotal5
End If
If JobName1 = "#NULL#" Then
txtJobName1 = ""
Else
txtJobName1 = JobName1
End If
If JobCost1 = "#NULL#" Then
txtJobCost1 = ""
Else
txtJobCost1 = JobCost1
End If
If JobName2 = "#NULL#" Then
txtJobName2 = ""
Else
txtJobName2 = JobName2
End If
If JobCost2 = "#NULL#" Then
txtJobCost2 = ""
Else
txtJobCost2 = JobCost2
End If
If JobName3 = "#NULL#" Then
txtJobName3 = ""
Else
txtJobName3 = JobName3
End If
If JobCost3 = "#NULL#" Then
txtJobCost3 = ""
Else
txtJobCost3 = JobCost3
End If
If JobName4 = "#NULL#" Then
txtJobName4 = ""
Else
txtJobName4 = JobName4
End If
If JobCost4 = "#NULL#" Then
txtJobCost4 = ""
Else
txtJobCost4 = JobCost4
End If
If JobName5 = "#NULL#" Then
txtJobName5 = ""
Else
txtJobName5 = JobName5
End If
If JobCost5 = "#NULL#" Then
txtJobCost5 = ""
Else
txtJobCost5 = JobCost5
End If
If TotalParts = "#NULL#" Then
txtTotalParts = ""
Else
txtTotalParts = TotalParts
End If
If TotalLabor = "#NULL#" Then
txtTotalLabor = ""
Else
txtTotalLabor = TotalLabor
End If
If TotalRepair = "#NULL#" Then
txtTotalRepair = ""
Else
txtTotalRepair = TotalRepair
End If
If Recommendations = "#NULL#" Then
txtRecommendations = ""
Else
txtRecommendations = Recommendations
End If
Close #1
End If
Set dlgOpenFile = Nothing
Exit Sub
cmdOpenRepairOder_Error:
MsgBox "There is a problem with the repair order. It cannot be opened.", _
vbOKOnly Or vbInformation, _
"College Park Auto Repair"
Resume Next
End Sub
- In the Object combo box, select cmdSaveRepairOrder
- Implement the Click event as follows:
Private Sub cmdSaveRepairOrder_Click()
On Error GoTo cmdSaveRepairOder_Error
Dim Action As Integer
If strFileName <> "" Then
Open strFileName For Output As #1
Write #1, txtCustomerName
Write #1, txtAddress
Write #1, txtCity
Write #1, txtState
Write #1, txtZIPCode
Write #1, txtMake
Write #1, txtModel
Write #1, txtCarYear
Write #1, txtProblemDescription
Write #1, txtPartName1
Write #1, txtUnitPrice1
Write #1, txtQuantity1
Write #1, txtSubTotal1
Write #1, txtPartName2
Write #1, txtUnitPrice2
Write #1, txtQuantity2
Write #1, txtSubTotal2
Write #1, txtPartName3
Write #1, txtUnitPrice3
Write #1, txtQuantity3
Write #1, txtSubTotal3
Write #1, txtPartName4
Write #1, txtUnitPrice4
Write #1, txtQuantity4
Write #1, txtSubTotal4
Write #1, txtPartName5
Write #1, txtUnitPrice5
Write #1, txtQuantity5
Write #1, txtSubTotal5
Write #1, txtJobName1
Write #1, txtJobCost1
Write #1, txtJobName2
Write #1, txtJobCost2
Write #1, txtJobName3
Write #1, txtJobCost3
Write #1, txtJobName4
Write #1, txtJobCost4
Write #1, txtJobName5
Write #1, txtJobCost5
Write #1, txtTotalParts
Write #1, txtTotalLabor
Write #1, txtTotalRepair
Write #1, txtRecommendations
Close #1
End If
Exit Sub
cmdSaveRepairOder_Error:
MsgBox "There is a problem with the repair order. It cannot be saved.", _
vbOKOnly Or vbInformation, _
"College Park Auto Repair"
Resume Next
End Sub
- Close Microsoft Visual Basic
- Close the form
- When asked whether you save, click Yes
|
|