Programming Part 2
Today we look at variables to hold characters (strings) and how to store and retrieve information via simple flat file storage.
This will be a very simple address form, but with changes and modifications you can turn it into purchase orders, sales orders, invoices, credit memos all sorts of forms. For ease and compatibility with VB we will keep all information string (or character) based.
|
If you'll remember from last time, characters are represented by numbers from the old telegraph system of ASCII codes. The computer system does this automatically for you when it sees any information contained inside of quotation marks ( " ").
We start off by learning a new keyword: REM which stands for REMember or REMarks. It is an aside written in by the programmer so they can REMemeber what that part of the code was supposed to do. Here we will use the REM statement in our code to tell you what is going on. You don't have to write out the REM lines if you don't want to, they are only here to advise you the details about areas in the program!
First, we do this in Quick or Power BASIC (QB):
REM below clears the screen -- it can be upper or lower case
CLS
TOP:
REM below is our main menu
PRINT " "
PRINT " "
PRINT "1 = Make a new name file"
PRINT "2 = Open an exiting name file"
PRINT "3 = Show a directory of name files"
PRINT "4 = Quit program"
PRINT ""
REM below is our prompt to the end user to get information
INPUT "What task number would you like to do"; M
CLS
REM below is processes that information from the user to do a task
IF M = 1 THEN GOSUB NU:
IF M = 2 THEN GOSUB OP:
IF M = 3 THEN GOSUB DR:
IF M = 4 THEN END
REM below loops up back to the top if no number is picked
GOTO TOP:
REM below are our sub routines for doing tasks
NU:
REM this one creates a new name and file for that name
INPUT "What is the last name"; L$
INPUT "What is the first name"; F$
INPUT "What is the address"; A$
INPUT "What is the city"; C$
INPUT "What is the state"; S$
INPUT "What is the post code"; Z$
INPUT "What is the area code"; AC$
INPUT "What is the phone number"; P$
CLS
REM below creates a file name FLN$ from the first 8 characters of the
REM last name and adds an extension of .ADR to that file name
FLN$ = LEFT$(L$, 8) + ".adr"
REM below creates a disk film and sends our data to that file
OPEN FLN$ FOR OUTPUT AS #1
PRINT #1, L$
PRINT #1, F$
PRINT #1, A$
PRINT #1, C$
PRINT #1, S$
PRINT #1, Z$
PRINT #1, AC$
PRINT #1, P$
CLOSE #1
CLS
REM below loops us back to the top of the program
RETURN TOP:
OP:
REM below opens a current file by first showing us a directory of files
FILES "*.adr"
INPUT "What is the exact file name from above to open"; FLN$
OPEN FLN$ FOR INPUT AS #1
INPUT #1, L$
INPUT #1, F$
INPUT #1, A$
INPUT #1, C$
INPUT #1, S$
INPUT #1, Z$
INPUT #1, AC$
INPUT #1, P$
CLOSE #1
CLS
LOCATE 5, 5
PRINT F$; " "; L$
LOCATE 6, 5
PRINT A$
LOCATE 7, 5
PRINT C$; ", "; S$; " "; Z$
LOCATE 8, 5
PRINT "("; AC$; ") "; P$
RETURN TOP:
DR:
REM below displays on our screen all files with the extension .ADR
CLS
PRINT " "
PRINT ""
FILES "*.adr"
PRINT ""
RETURN TOP:
What this program does is to create small, separate files based on the last names of people and using a suffix created by us, the programmers, to identify our files from other files on the system. The information is stacked into the file one item after another. If no data exists for a particular parameter a blank or null space is inserted to take up file space.
We can see our files with option #3. We can write new files with option #1 and see any existing files, including all file names with #2. The final option 4 terminates the program. Should we use any other number we loop back to the top.
Option selection is made using the traditional IF something THEN do something concept of programming, which is an older method with limitations (some BASICs can only handle a few dozen IF THEN routines at one time), but this method does serve to demonstrate how program flow is maintained.
Our separate routines are kept in a SUB area denoted with a programmer defined name, followed by a colon. In this program those routines are NU: for new name, OP: to open and exiting name DR: for the directory of files and TOP: to designate where the top of our program code is.
When BASIC encounters a GOSUB routine the program goes down the list of code looking for either a line number (very old BASIC used line numbers for each line of code) or labels (such as OP: or NU:). Once it reaches the proper label it beings executing the code lines following that label until it reaches a RETURN keyword. The use of just RETURN would send us back to the originating line and the program would continue on the next line of code down. We can also designate a line number or label to which we can RETURN, in this case the TOP: of the program by using: RETURN TOP:
At the bottom of our IF THEN routines we have a GOTO TOP: line which is primarily a part of old, original BASIC and is now frowned on -- the unconditional GOTO, nonetheless lifelong BASIC programmers tend to use this line a lot in their programs. Educators feel it makes for hard to follow, stringy code.
The new keyword we learned here are:
FILES - which when used alone displays all files in a given area of the drive when used with a character parameter such as ".adr" displays just the files found in the immediate area with the extension .ADR.
OPEN - which is the Microsoft routine for opening a disk file. The file can be opened for OUTPUT (putting data into the file) or INPUT (getting data out of the file). This is called a sequential file. Also supported is APPEND which adds new information to an existing file. OUTPUT wipes all current data in the file and replaces it with new data. We are only dealing with OUTPUT and INPUT in this series. Another file format is also supported called RANDOM, which is a more complicated type of file that allows you to put data into the file in discrete records, like a data based. Again, we are not using that type of file here, today. Since you may have more than one file open, all files have to have a number so the computer can keep track of them. Hence the form: OPEN "filename" FOR type AS number. Filename is any 8 characters, with an optional period and up to 3 additional extension letters. Type is either OUTPUT, INPUT, APPEND or RANDOM. Number is any valid integer from 1 to as high as your system will go. Some systems are limited to 127 files, others to 256, others to 512, some to 32767. Consult the instructions in your type of BASIC for more details.
Close #number - This is used to close an OPEN file. All files must be closed. Use of CLOSE with no number will close and terminate all files created and opened in BASIC at any point in time (they will not, however, close other files on your system opened with another program). Close #1 would close the first file, while Close #8 would close the 8th file.
GOSUB name or number - This tells the computer to move to the designated line number or label and continue running the code. Such as: GOSUB NU:
RETURN - used with GOSUB to send the program flow back to the original line that stared the GOSUB or to a designated line, such as: GOSUB TOP:
GOTO name or number - This tells the program to arbitrarily GOTO some designated area of code. The program will search the entire stack of code looking for that designation, such as: GOTO TOP:
LEFT$(string,places) - This tells the program to extract a new string from an existing string a designated number of places starting at the left side. We use it here to conform our program for QB which limits file names to 8 characters. We look at the last name string (L$) and select the first 8 characters found on the left side and use this to create a new string (FLN$) which we use as our automatic file nane. We could have also manually created a file name using INPUT "What do you want to name this file";FLN$ This, however, is an automated feature we built into the program and used LEFT$() to make sure we didn't have a file name that was too long for older BASIC. There is also a RIGHT$() and MID$() set of functions for taking strings apart from the right side or from within the string at a designated place. We will learn more about there uses later n the series.
IF something THEN something -- This is a conditional test. The computer looks to see IF two things are or are not valid. In this case we look to see IF the menu selections variable ( M ) is a specific value (1, 2, 3 or 4 in this program). Based on this information the computer either determines that this is TRUE or FALSE. If it is TRUE anything after the THEN statement will be executed. If it is FALSE it continues going down the stack of code, all the way to the bottom if necessary. There are better, newer methods of handling this type of menu option we will learn later in this series.
REM -- Remarks or remember, an aside from the programmer so we know what is going on. It is an option. You don't have to type or enter the REM lines in code you see in books to make the program work. These are just there to help you understand the code.
The same project in VB:
Select NEW PROJECT from the FILE menu and a blank form appears. Drag over enough labels (the big letter A on the tool bar) for:
First Name
Last Name
Address
City
State
Post Code
Area Code
Phone Number
Then drag over Text Boxes (the white rectangle with the small letters "ab" inside), place and size these next to each label. You should also clean out the Text property of each box so they are blank at start-up.
Drag over two Command buttons. Change the caption on Command1 to read: Open. Then change the caption on Command2 to read: Save.
Drag over a Common Dialog Box -- you may have to put your mouse pointer on the tool bar and click the right mouse button to get the extra tools (Components) window and find this item on that list. Once the ICON for a Common Dialog box is in the tool bar, click on it and then put one somewhere on the form. This item will not show when the form is run. (A Common Dialog box is one of the standard Windows objects used by all Windows programs to access color selectors, files and fonts. This object must be included in your distribution packages for VB program.)
Click on the Save command button (command2) and when the program code box appears in the SUB_CLICK() area place the following code:
FLN$= Text2.Text + ".adr"
OPEN FLN$ FOR OUTPUT AS #1
PRINT #1,Text1.Text
PRINT #1,Text2.Text
PRINT #1,Text3.Text
PRINT #1,Text4.Text
PRINT #1,Text5.Text
PRINT #1,Text6.Text
PRINT #1,Text7.Text
PRINT #1,Text8.Text
Close #1
Text1.Text=""
Text2.Text=""
Text3.Text=""
Text4.Text=""
Text5.Text=""
Text6.Text=""
Text7.Text=""
Text8.Text=""
Next, click on the Open command button (Command1) and in the SUB_CLICK() area type in this code:
CommonDialog1.Filter = " Addresses | *.adr" CommonDialog1.ShowOpen
Open CommonDialog1.Filename for INPUT As #1
INPUT #1,F$
INPUT #1,L$
INPUT #1, A$
INPUT #1, C$
INPUT #1, S$
INPUT #1,Z$
INPUT #1, AC$
INPUT #1, P$
Close #1
Text1.Text=F$
Text2.Text=L$
Text3.Text=A$
Text4.Text=C$
Text5.Text=S$
Text6.Text=Z$
Text7.Text= AC$
Text8.Text=P$
That's all the code you need to do the same project in VB! The CommonDialog ShowOpen box replaces the FILES statement from QB. We used the same variables. The actual file OPEN and CLOSE was the same as with QB (in QB we use an INPUT to get the file name from the user, in VB the user selected a name from a list in the File Box and then we used the CommonDialog1.Filename parameter as the name to open our file). We also added a process to clear the various text boxes after entering information and saving a new file, which works like the CLS function in QB.
|