200 likes | 382 Views
Two Forms. Please use speaker notes for additional information!. 2 Forms. On the first form, Next will bring up the second form. On the second form, Previous will bring up the first form. 2 Forms. prSrchCr. prSrchCr. prSrchCr. First I entered the course number that I want to search for.
E N D
Two Forms Please use speaker notes for additional information!
2 Forms On the first form, Next will bring up the second form. On the second form, Previous will bring up the first form.
prSrchCr 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.
prSrchCr I entered in the new course information and clicked Add Course. 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. Note that the new course is added.
prSrchCr In this case I want to add CIS45. I enter the data and click Add Course and the course is added.
frmSrhCr A course array is set up to hold 20 courses - that is all I can handle in this program. Option Explicit Dim CourseFile As String Dim crsArray(1 To 20) As String Dim numCrs As Integer Private Sub cmdClear_Click() txtCrsNum.Text = "" txtCrsName.Text = "" txtCrsNum.SetFocus End Sub Private Sub cmdExit_Click() End End 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 #1 End Sub 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.
CISCrs.txt The records each contain one element or field which is compatible with moving the data into the array.
prSrchCr DO while loop that looks for a match. 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. 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.
frmCIS Option Explicit Dim crsArray(1 To 20) As String Dim numCrs As Integer Dim 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.Hide End 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 #1 End Sub In 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.
frmCIS If the course array is at its upper bound, no course can be added. 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 If End Sub 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.
prSrcjCr3.vbp frmSrhCr2.frm
prSrchCr3.vbp frmCIS3.frm
prSrchCr3.vbp FileHandlr2.bas
Option Explicit Private Sub cmdClear_Click() txtCrsNum.Text = "" txtCrsName.Text = "" txtCrsNum.SetFocus End Sub Private Sub cmdExit_Click() End End Sub Private Sub Form_Load() Call Load_File End Sub prSrcjCr3.vbp frmSrhCr2.frm When the call is made to Load_file it is executing Load_File which is part of FileHandlr2.bas.
prSrcjCr3.vbp frmSrhCr2.frm 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 Shows the second form.
prSrchCr3.vbp frmCIS3.frm Option 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.Hide End 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.
prSrchCr3.vbp frmCIS3.frm 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 I 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.