two forms please use speaker notes for additional information!

20
Two Forms Please use speaker notes for additional information!

Upload: vanessa-brown

Post on 14-Jan-2016

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Two Forms Please use speaker notes for additional information!

Two Forms

Please use speaker notes for additional information!

Page 2: Two Forms Please use speaker notes for additional information!

2 Forms2 Forms

On the first form, Next will bring up the second form.

On the second form, Previous will bring up the first form.

Page 3: Two Forms Please use speaker notes for additional information!

2 Forms2 Forms

Page 4: Two Forms Please use speaker notes for additional information!

prSrchCrprSrchCr

Page 5: Two Forms Please use speaker notes for additional information!

prSrchCrprSrchCr

Page 6: Two Forms Please use speaker notes for additional information!

prSrchCrprSrchCr

First I entered the course number that I want to search for.

The retrieval is successful and comes back with the name of the course.

Page 7: Two Forms Please use speaker notes for additional information!

prSrchCrprSrchCr

This time when I entered CIS67 and clicked retrieve, the course name came back with Invalid Course Number and a message box screen with option Yes/No appeared asking me whether I want to add a course. I responded Yes.

I entered in the new course information and clicked Add Course.

Note that the new course is added.

Page 8: Two Forms Please use speaker notes for additional information!

prSrchCrprSrchCr

In this case I want to add CIS45. I enter the data and click Add Course and the course is added.

Page 9: Two Forms Please use speaker notes for additional information!

frmSrhCrfrmSrhCr

Option ExplicitDim CourseFile As StringDim crsArray(1 To 20) As StringDim numCrs As Integer

Private Sub cmdClear_Click() txtCrsNum.Text = "" txtCrsName.Text = "" txtCrsNum.SetFocus End Sub

Private Sub cmdExit_Click() EndEnd Sub

Private Sub Form_Load() Dim ptr As Integer CourseFile = App.Path & "\CISCrs.txt" ptr = 1 Open CourseFile For Input As #1 Do While Not EOF(1) And ptr < (UBound(crsArray) + 1) Input #1, crsArray(ptr) ptr = ptr + 1 Loop numCrs = ptr - 1 Close #1End Sub

A course array is set up to hold 20 courses - that is all I can handle in this program.

When I load the form, I open the file that contains the course code and course name and move the information into the array.

The DO loop will continue while it is not EOF and the ptr is less than the upper bound of the array + 1. In this case that would be 21 so as long as the ptr is less than that I can add to the array.

Page 10: Two Forms Please use speaker notes for additional information!

CISCrs.txtCISCrs.txt

The records each contain one element or field which is compatible with moving the data into the array.

Page 11: Two Forms Please use speaker notes for additional information!

prSrchCrprSrchCr

Private Sub cmdRetrieve_Click() Dim wrkInd As String, ptr As Integer Dim AddCrsResp As Integer wrkInd = "N" ptr = 1 Do While wrkInd = "N" And ptr < (UBound(crsArray) + 1) If UCase(txtCrsNum.Text) = UCase(Left(crsArray(ptr), 5)) Then wrkInd = "Y" Else ptr = ptr + 1 End If Loop If wrkInd = "Y" Then txtCrsName.Text = Mid(crsArray(ptr), 7) cmdClear.SetFocus Else txtCrsName.Text = "Invalid Course Number" AddCrsResp = MsgBox("Add Course?", vbYesNo) If AddCrsResp = vbYes Then Rem frmCIS.Show vbModal, Me frmCIS.Show vbModal Call Form_Load End If cmdClear_Click End If

End Sub

Set up an indicator and ptr to search the array.

DO while loop that looks for a match.

This puts up a box that gives a yes/no choice - vbYesNo. If they respond yes (vbYes) then the next form is shown.

The next form is shown. By using vbModal, the user has to make a response or do something to exit the form.

After the processing is accomplished, I call Form_Load to load the information into the array.

Page 12: Two Forms Please use speaker notes for additional information!

Option ExplicitDim crsArray(1 To 20) As StringDim numCrs As IntegerDim CourseFile As String

Private Sub cmdDisp_Click() Dim ptr As Integer picArrayDisp.Cls For ptr = 1 To numCrs picArrayDisp.Print crsArray(ptr) Next ptr txtNewCrsNum.SetFocus End Sub

Private Sub CmdStop_Click() txtNewCrsNum.Text = "" txtNewCrsName.Text = "" Me.HideEnd Sub

Private Sub Form_Load() CourseFile = App.Path & "\CISCrs.txt" Open CourseFile For Input As #1 numCrs = 0 Do Until EOF(1) Or numCrs = UBound(crsArray) numCrs = numCrs + 1 Input #1, crsArray(numCrs) Loop Close #1End Sub

frmCISfrmCISIn this program, I have associated an array with each form so they are not required to pass information between forms. In the form load, this array is filled.

This code simply displays the courses in the picture box being used for that purpose.

If the user clicks stop, the text boxes for course number/code and name are set to null and the form is hidden using Me.Hide.

Page 13: Two Forms Please use speaker notes for additional information!

Private Sub cmdAddCrs_Click() Dim wrkhold As String, newhold As String Dim ptr As Integer, ct As Integer If numCrs = UBound(crsArray) Then MsgBox "Array is full", vbOKOnly, "Error" Else newhold = UCase(Left(txtNewCrsNum.Text, 3)) _ & Right(txtNewCrsNum.Text, 2) _ & " " & txtNewCrsName.Text For ptr = 1 To numCrs If Left(crsArray(ptr), 5) >= Left(newhold, 5) Then wrkhold = crsArray(ptr) crsArray(ptr) = newhold newhold = wrkhold End If Next ptr numCrs = numCrs + 1 crsArray(numCrs) = newhold Open CourseFile For Output As #1 ptr = 0 Do Until ptr = numCrs ptr = ptr + 1 Write #1, crsArray(ptr) Loop Close #1 Call cmdDisp_Click End IfEnd Sub

frmCISfrmCISIf the course array is at its upper bound, no course can be added.

Establishes course code/number and name as a string in the hold area.

Goes through the array and inserts the add in the appropriate place.

Writes the changed array to the disk file. When we go back to the original form we will process the Call to Form_Load which will fill the array on the original form with the data from the file.Calls the

display routine.

Page 14: Two Forms Please use speaker notes for additional information!

prSrcjCr3.vbp

frmSrhCr2.frm

prSrcjCr3.vbp

frmSrhCr2.frm

Page 15: Two Forms Please use speaker notes for additional information!

prSrchCr3.vbp

frmCIS3.frm

prSrchCr3.vbp

frmCIS3.frm

Page 16: Two Forms Please use speaker notes for additional information!

prSrchCr3.vbp

FileHandlr2.bas

prSrchCr3.vbp

FileHandlr2.bas

Page 17: Two Forms Please use speaker notes for additional information!

prSrcjCr3.vbp

frmSrhCr2.frm

prSrcjCr3.vbp

frmSrhCr2.frm

Option Explicit

Private Sub cmdClear_Click() txtCrsNum.Text = "" txtCrsName.Text = "" txtCrsNum.SetFocusEnd Sub

Private Sub cmdExit_Click() EndEnd Sub

Private Sub Form_Load() Call Load_FileEnd Sub

When the call is made to Load_file it is executing Load_File which is part of FileHandlr2.bas.

Page 18: Two Forms Please use speaker notes for additional information!

Private Sub cmdRetrieve_Click() Dim wrkInd As String, ptr As Integer Dim AddCrsResp As Integer wrkInd = "N" ptr = 1 Do While wrkInd = "N" And ptr < (UBound(crsArray) + 1) If UCase(txtCrsNum.Text) = UCase(Left(crsArray(ptr), 5)) Then wrkInd = "Y" Else ptr = ptr + 1 End If Loop If wrkInd = "Y" Then txtCrsName.Text = Mid(crsArray(ptr), 7) cmdClear.SetFocus Else txtCrsName.Text = "Invalid Course Number" AddCrsResp = MsgBox("Add Course?", vbYesNo) If AddCrsResp = vbYes Then frmCIS2.Show vbModal, Me End If cmdClear_Click End If

End Sub

prSrcjCr3.vbp

frmSrhCr2.frm

prSrcjCr3.vbp

frmSrhCr2.frm

Shows the second form.

Page 19: Two Forms Please use speaker notes for additional information!

prSrchCr3.vbp

frmCIS3.frm

prSrchCr3.vbp

frmCIS3.frmOption Explicit

Private Sub cmdDisp_Click() Dim ptr As Integer txtCrsNames.Text = "" For ptr = 1 To UBound(crsArray) txtCrsNames.Text = txtCrsNames.Text _ & crsArray(ptr) & vbCrLf Next ptr txtNewCrsNum.SetFocus End Sub

Private Sub CmdStop_Click() Call Clear_Text Me.HideEnd Sub

Private Sub Clear_Text() txtNewCrsNum.Text = "" txtNewCrsName.Text = ""

End Sub

Private Sub Form_Activate() txtNewCrsNum.Text = frmSrhCr2!txtCrsNum.Text End Sub

When the form is activated, the form will receive the txtCrsNu.text from the fromSrhCr2 and store it in txtNewCrsNum.text. This is a way of passing data between forms.

Page 20: Two Forms Please use speaker notes for additional information!

Private Sub cmdAddCrs_Click() Dim wrkhold As String, newhold As String Dim ptr As Integer, ct As Integer Dim ErrFlg As Boolean ErrFlg = False If txtNewCrsNum.Text = "" _ Or txtNewCrsName.Text = "" Then ErrFlg = True Else newhold = UCase(Left(txtNewCrsNum.Text, 3)) _ & Right(txtNewCrsNum.Text, 2) _ & " " & txtNewCrsName.Text For ptr = 1 To UBound(crsArray) If Left(crsArray(ptr), 5) > Left(newhold, 5) Then wrkhold = crsArray(ptr) crsArray(ptr) = newhold newhold = wrkhold Else If Left(crsArray(ptr), 5) = Left(newhold, 5) Then MsgBox "Course already exists", vbOKOnly, "Error" ptr = UBound(crsArray) ErrFlg = True End If End If Next ptr End If If ErrFlg = False Then ReDim Preserve crsArray(UBound(crsArray) + 1) crsArray(UBound(crsArray)) = newhold Call Write_File Call cmdDisp_Click End If Call Clear_Text txtNewCrsNum.SetFocus End Sub

prSrchCr3.vbp

frmCIS3.frm

prSrchCr3.vbp

frmCIS3.frmI have set up an error flag (ErrFlg) that will be used to test conditions.

If I successfully added an element to the array. The last step was completed in this code. Then I called the Write_file which is in FileHandlr2.bas. This routine will write the updated array to the file.