VBA and Sequential Text Files (Cont.)
To write a parametric routine, we need to store the parametric data for
each individual object somewhere. We could hard code it into the program.
Safe and secure but difficult to maintain. Alright then, a database or a
spreadsheet. Also fine if you've got the software and know how to use it.
A text file on the other hand is simple to maintain and does not require
any other type of software or the knowledge to use and configure it.
Let's say for example we would like to write an application that
parametrically draws structural steel sections for us. We know all the
names and basic dimensions of all the sections but just need to be able to
retrieve these values for our program to use.
O.K. let's set up our text file first. Open a new file in Notepad and add
this :
ROLLED STEEL UNEQUAL ANGLES DATA
---------------------------------
H X W X T1 X R1 X R2
---------------------------------
*65x50x6
65.0,50.0,6.0,6.0,3.0
*65x50x8
65.0,50.0,8.0,6.0,3.0
*75x50x6
75.0,50.0,6.0,7.0,3.5
*75x50x8
75.0,50.0,8.0,7.0,3.5
*80x60x6
80.0,60.0,6.0,8.0,4.0
*80x60x8
80.0,60.0,8.0,8.0,4.0
*90x65x6
90.0,65.0,6.0,8.0,4.0
*90x65x8
90.0,65.0,8.0,8.0,4.0
*90x65x10
90.0,65.0,10.0,8.0,4.0
*100x65x8
100.0,65.0,8.0,10.0,5.0
*100x65x10
100.0,65.0,10.0,10.0,5.0
*100x75x6
100.0,75.0,6.0,10.0,5.0
*100x75x8
100.0,75.0,8.0,10.0,5.0
*100x75x10
100.0,75.0,10.0,10.0,5.0
*100x75x12
100.0,75.0,12.0,10.0,5.0
*125x75x8
125.0,75.0,8.0,11.0,5.5
*125x75x10
125.0,75.0,10.0,11.0,5.5
*125x75x12
125.0,75.0,12.0,11.0,5.5
*150x75x10
150.0,75.0,10.0,11.0,5.5
*150x75x12
150.0,75.0,12.0,11.0,5.5
*150x75x15
150.0,75.0,15.0,11.0,5.5
*150x90x10
150.0,90.0,10.0,12.0,6.0
*150x90x12
150.0,90.0,12.0,12.0,6.0
*150x90x15
150.0,90.0,15.0,12.0,6.0
Save the file as Extext.dat.
The file basically contains two main lines of information required for
each unequal angle.
The first line - *100x75x10" is the name of the angle and will be the
string that appears in the list box. The asterix "*" preceding
the name, we will use as a marker for searching.
The second line - "100.0,75.0,10.0,10.0,5.0" is the
information we are trying to retrieve. It is in comma delimited format to
allow us to read each item into it's appropriate variable.
Right onto the coding. Create a new Project and add a userform with two
buttons, a list box and two labels, maintaining the default names. Now add
this coding :
'CODING STARTS HERE
Option Explicit
Private Sub UserForm_Initialize()
Dim sTempa As String
Dim nFilea As Integer
On Error GoTo err_handler
'setup and format the form
UserForm1.Caption = "Un-Equal Angles"
CommandButton1.Caption = "OK"
CommandButton1.Accelerator = "O"
CommandButton1.Default = False
CommandButton2.Caption = "Cancel"
CommandButton2.Accelerator = "C"
CommandButton2.Cancel = False
Label1.Caption = "www.AfraLisp.com"
Label2.Caption = "Select Size :"
'=============================================
'=============================================
'get the next free file number
nFilea = FreeFile
'clear listbox
ListBox1.Clear
'open the text file
Open "Extext.dat" For Input As #nFilea
'until the end of file
While Not EOF(nFilea)
'read the line and store it in a variable
Line Input #nFilea, sTempa
'isolate the first character and test
If Left$(sTempa, 1) = "*" Then
'remove the first character
sTempa = Right$(sTempa, (Len(sTempa) - 1))
'display the remainder in the list box
ListBox1.AddItem sTempa
'end if
End If
'loop
Wend
'close the file
Close #nFilea
Exit Sub
err_handler:
MsgBox "Error No " & Err.Number & " - " &
Err.Description
Err.Clear
Exit Sub
End Sub
'-------------------------------------------------------------------------------------------------
Private Sub CommandButton1_Click()
Dim sTemp As String
Dim sTemp1 As String
Dim nFile As Integer
Dim sHeight As String
Dim sWidth As String
Dim sThickness As String
Dim sRadius1 As String
Dim sRadius2 As String
On Error GoTo err_handler
'hide the form
UserForm1.Hide
'get the selection from the list box
sTemp = ListBox1.Text
'get the next free file number
nFile = FreeFile
'open the text file
Open "Extext.dat" For Input As #nFile
'until the end of file
While Not EOF(nFile)
'read the line and store it in a variable
Line Input #nFile, sTemp1
'compare the selection
If sTemp1 = "*" & sTemp Then
'read the next line and retrieve the values from the
text file
'storing them into their respective variables
Input #nFile, sHeight, sWidth, sThickness, sRadius1, sRadius2
'display the results
MsgBox "Dimensions for " & sTemp & vbCr & vbCr _
& sHeight & " " & sWidth & " " &
sThickness & " " _
& sRadius1 & " " & sRadius2
'close the file
Close #nFile
'you have now retrieved all the values necessary to
draw
'the un-equal angle.
'coding for that would go here.
'we're done, so exit
Exit Sub
'end if
End If
'loop
Wend
'close the file
Close #nFile
Exit Sub
err_handler:
MsgBox "Error No " & Err.Number & " - " &
Err.Description
Err.Clear
Exit Sub
End Sub
'-------------------------------------------------------------------------------------------------------------------------
Private Sub CommandButton2_Click()
End
End Sub
'-------------------------------------------------------------------------------------------------------------------------
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
'if the user double clicks the list box
CommandButton1_Click
End Sub
'CODING ENDS HERE
Run the program. A dialog like this should appear :
Highlight a size and select "OK"
or double-click on the size. A message box should appear listing the
un-equal angles vital statistics :
Now all you need to do is draw the un-equal
angle. Hey, don't look at me, I've done enough for today and I'm now off
for a beer. Cheers!!!
Whoops, almost forgot! You probably want
the source coding. Okey dokey, just click here.
|