Lab 7.6 – Hail to the Chief

The Spirit of ‘76

 

Goals:

    w Make a data base interface, allowing:

         X Access by name

         X Display by fields

         X Updating

         X Forward/backward movement

         X Printing

    w Master use of the frame object

 

__1. Create a data base interface like the one below.

 

 

          w Start up a new VB project, prjDB, with one form, frmDB.

          w Set up the presidential information structure.

                       X Define the type, as in previous labs.

                       X Declare prezdat to be a module level variable.

                       X Why do we want it to be module level?

          w Create a framework for displaying and updating each record.

                       X Place a frame object, frePresInfo, onto the form.

                       X Look up Frame control in Online Help.  Note, especially that you

                   are to “first draw the Frame control, and then draw the controls

inside the Frame.”

                       X Place a listbox, lbxPrez, 6 textboxes - txtFirst, txtLast, txtBirth,

txtDeath, txtTerm, and txtEvents - and 6 labels - lblFirst, lblLast,

lblBirth, lblDeath, lblTerm, and lblEvents  - inside the frame.

                        Notes: (1) You cannot do this by double clicking on the icons in

the toolbox.  You must single click on an icon, then use your

mouse to position each object within the boundaries of the

frame. (2) Do not worry about their position or size.  That will

be taken care of at run time.

                       X Set the MultiLine property for txtEvents to True.

                       X Why do we need to do this?  What would happen if we did not?

                       X While in design mode, move the frame around.  What happens

to the objects inside the frame?

          w Create the code for positioning the objects within the frame.

                       X Add a module level integer variable, recnum, and a constant,

numofprezes = 43, to the program.

                       X Place the code below into Form_Load.

Open App.Path & "\presdata" For Random As #1 Len = Len(prezdat)

 Call SetUp

 recnum = 1

 DisplayRecord (recnum)

                       X Why recnum was  set to 1?

                       X Write code for the SetUp procedure, as below.

Call SetPrezList

 frmDB.Left = 100

 frmDB.Top = 100

 frmDB.Height = 8000

 frmDB.Width = 8000

 frePresInfo.Height = 6000

 frePresInfo.Width = frePresInfo.Height + 1000

 lbxPrez.Height = 5500

 lbxPrez.Width = 2000

 txtFirst.Height = 375

 . . . {the same for the other textboxes}

 txtEvents.Height = 2300

 txtFirst.Width = 2900

 txtLast.Width = 2900

 txtBirth.Width = 750

 txtDeath.Width = 750

 txtTerm.Width = 900

 txtEvents.Width = 3000

 lbxPrez.Left = 4560

 txtFirst.Left = 1320

 . . . {the same for the other textboxes}

 lbxPrez.Top = 480

 txtFirst.Top = 480

 txtLast.Top = txtFirst.Top + 600

  . . . {similarly for the other textboxes}

 lblFirst.Top = txtFirst.Top

  . . . {the same for the other labels}

 lblFirst.Left = 360

   . . . {the same for the other labels}

 lblFirst.Height = txtFirst.Height

  . . . {the same for the other labels}

 lblFirst.Width = 900

  . . . {the same for the other labels}

 lblFirst.Caption = "First Name"

  . . . {appropriate captions for the other labels}

                       X Write code for the SetPrezList procedure, as below.

lbxPrez.AddItem "George Washington"

  . . . {similarly for the other presidents, who are listed below}

 

John Adams, Thomas Jefferson, James Madison, James Monroe, John Quincy Adams, Andrew Jackson, Martin Van Buren, William Henry Harrison, John Tyler, James K. Polk, Zachary Taylor, Millard Fillmore, Franklin Pierce, James Buchanan, Abraham Lincoln, Andrew Johnson, Ulysses S. Grant, Rutherford B. Hayes, James A. Garfield, Chester A. Arthur, Grover Cleveland, Benjamin Harrison, Grover Cleveland, William McKinley, Theodore Roosevelt, William H. Taft, Woodrow Wilson, Warren G. Harding, Calvin Coolidge, Herbert C. Hoover, Franklin D. Roosevelt, Harry S Truman, Dwight D. Eisenhower, John F. Kennedy, Lyndon B. Johnson, Richard M. Nixon, Gerald R. Ford, James E. Carter, Ronald Reagan, George Bush, William J. Clinton, George W. Bush.

{Grover Cleveland is listed twice because he served non-consecutive terms}

 

__2. Place command buttons on the form

          w Place the command buttons cmdPrev, cmdNext, cmdPrint, cmdSave,

   and cmdExit onto the form.

 

__3. Write code for displaying each record.

          w Recall the reference to procedure, DisplayRecord, in the code above.

          w The code begins as below:

Get #1, recno, prezdat

txtFirst.Text = prezdat.firstName

          w Write the rest of the procedure. 

                       X Be sure to show the president number, as seen in the example

above.

                       X What property of which object controls that?

       X What code did you write for that?

 

__4. Write code for navigating through the set of records.

          w The code for cmdPrev is given below.

recnum = OneMod(recnum - 1, numofprezes)

Call DisplayRecord(recnum)

          w Notice the call to function, OneMode.  Its code is below.

Public Function OneMod(ByVal n As Integer, ByVal modulo As Integer) As Integer

Dim temp As Integer

 temp = n Mod modulo

 If temp = 0 Then temp = modulo

 OneMod = temp

End Function

                       X What is the role of OneMod?  Why is the function needed?

          w Write code for cmdNext.

                       X What code did you write?

                       X What will happen when we try to move forward past the last

record, or backward past the first one?  Why?

          w Here is the code for the Click event of lbxPrez.

recnum = lbxPrez.ListIndex + 1

DisplayRecord (recnum)

                       X Why is 1 added to the value of ListIndex?

 

__5. Write code for updating the data in the file and for printing.

          w Import code for printing from Lab 7.5.

          w Write code for cmdSave.  There are two steps to this process.

                       X First, place the information into the structure prezdat.

                       X Then write that information into the appropriate record of the

file, presdata.

                       X What was the last line of code for this procedure?

 

__6. Finally, use the names in the list box to save some work for filling in

the presidential records.

          w Finish the code below for lbxPrez_DblClick, making all necessary

     variable declarations.

name = lbxPrez

 spaceSpot = FindLastSpace(name)

firstName = Left( . . . )

 lastName = . . .

 txtFirst.Text = . . .

 txtLast.Text = . . .

                 X What did you put in place of . . . ?

w Here is the code for FindLastSpace.

Public Function FindLastSpace(ByVal mesg As String) As Integer

Dim spot As Integer

 spot = Len(mesg)

 While Mid(mesg, spot, 1) <> " " And spot > 1

  spot = spot - 1

 Wend

 FindLastSpace = spot

End Function

                       X Explain the operation of the While loop.

                       X What is the purpose of this function?  How does it work in the

context of this program?

 

__7. Now finish up and test out the program.

          w Wrote code for the cmdExit button.

                       X What two lines of code did you write?

          w Use the lbxPrez double click event in conjunction with the Save

    button to place all president’s names into the data base.

w Fill in the missing information for 3 more presidents, saving that

    information to the data base.  That should give you information on

    a total of 6 presidents.

                       X Who are the 6 presidents that you now have information for in

your data base?

w Print out information on several presidents to test the printing

   function.