Sei sulla pagina 1di 11

frmMainMenu.

frm (06/02/2011) Page 1

1 Private Sub cmdCourse_Click()


2 frmEditCourses.Show vbModal
3 End Sub
4
5 Private Sub cmdExit_Click()
6 'Close all files
7 Close #1
8 Close #2
9 Close #3
10 'Update values to config file
11 Open App.Path & "/Values.cfg" For Output As #1
12 Write #1, LastStud, LastEnrol, LastSub
13 Close #1
14 'Close program
15 End
16 End Sub
17
18 Private Sub cmdStudent_Click()
19 frmEditStudent.Show vbModal
20 End Sub
21
22 Private Sub cmdUtilities_Click()
23 frmUtilities.Show vbModal
24 End Sub
25
26 Private Sub Form_Load()
27 Dim RecLength As Integer
28 'Open config file to get values
29 Open App.Path & "/Values.cfg" For Input As #1
30 Input #1, LastStud, LastEnrol, LastSub
31 Close #1
32 'Set record pointers to zero
33 OnStud = 0
34 OnEnrol = 0
35 OnSub = 0
36
37 'Open each direct access file
38 RecLength = LenB(Student)
39 Open App.Path & "/Student.dat" For Random As #1 Len = RecLength
40
41 RecLength = LenB(Subject)
42 Open App.Path & "/Subject.dat" For Random As #2 Len = RecLength
43
44 RecLength = LenB(Enrolment)
45 Open App.Path & "/Enrolment.dat" For Random As #3 Len = RecLength
46 End Sub
frmEditCourses.frm (06/02/2011) Page 1

1 Dim DataChanged As Boolean


2
3 Private Sub Form_Load()
4 'Empty combo box
5 Me.cboSubject.Clear
6 'Add each subject from file to combo box
7 For OnSub = 1 To LastSub
8 Get #2, OnSub, Subject
9 With Subject
10 'Put subject number at end of string - so not seen by user
11 Me.cboSubject.AddItem .SubName & Space(20) & OnSub
12 End With
13 Next OnSub
14 'If 1 or more subject, select the first one
15 If LastSub > 0 Then Me.cboSubject.ListIndex = 0
16 End Sub
17
18 Private Sub cboSubject_Click()
19 Dim Reply
20 'Data has changed - prompt to save
21 If DataChanged = True Then
22 Reply = MsgBox("Data has changed, do you want to save changes?", vbYesNo, "Warning")
23 If Reply = vbYes Then
24 'If yes then attempt to save
25 If SaveData() = False Then
26 'cannot save due to errors to exit process
27 Exit Sub
28 End If
29 End If
30 End If
31
32 'The value of the subject number is at the end of combo box text
33 OnSub = Val(Right(Me.cboSubject.Text, 6))
34 'Get the subject record and update the fields on the form
35 Get #2, OnSub, Subject
36 With Subject
37 Me.lblSubNo = OnSub
38 Me.txtSubject = .SubName
39 Me.chkA = .BlockA
40 Me.chkB = .BlockB
41 Me.chkC = .BlockC
42 Me.chkD = .BlockD
43 Me.chkE = .BlockE
44 Me.chkF = .BlockF
45 Me.chkG = .BlockG
46 End With
47 'Reset that data has changed to false
48 DataChanged = False
49 End Sub
50
51 Private Sub chkA_Click()
52 'Flag to indicate information has changed
53 DataChanged = True
54 End Sub
55
56 Private Sub chkB_Click()
57 'Flag to indicate information has changed
58 DataChanged = True
59 End Sub
60
61 Private Sub chkC_Click()
62 'Flag to indicate information has changed
63 DataChanged = True
64 End Sub
65
66 Private Sub chkD_Click()
67 'Flag to indicate information has changed
68 DataChanged = True
69 End Sub
70
71 Private Sub chkE_Click()
72 'Flag to indicate information has changed
73 DataChanged = True
74 End Sub
75
76 Private Sub chkF_Click()
77 'Flag to indicate information has changed
78 DataChanged = True
79 End Sub
80
81 Private Sub chkG_Click()
82 'Flag to indicate information has changed
83 DataChanged = True
84 End Sub
85
86 Private Sub txtSubject_Change()
87 'Flag to indicate information has changed
88 DataChanged = True
89 End Sub
90
91 Private Sub cmdAdd_Click()
92 Dim Reply
93 'Check to see if data has changed before creating new record
94 If DataChanged = True Then
95 'Changed - so prompt to save
96 Reply = MsgBox("Data has changed, do you want to save changes?", vbYesNo, "Warning")
97 If Reply = vbYes Then
98 'Save record, call function
99 If SaveData() = False Then
100 'cannot save due to errors to exit process
101 Exit Sub
102 End If
103 End If
104 End If
105
106 'Reset objects on form for add mode
frmEditCourses.frm (06/02/2011) Page 2

107 Me.cboSubject.Visible = False


108 Me.cmdSave.Visible = True
109 Me.cmdCancel.Visible = True
110 Me.cmdAdd.Visible = False
111 'Go to the position last record plus 1
112 OnSub = LastSub + 1
113 'Initialise form values for new record
114 Me.lblSubNo = Format(OnSub, "000000")
115 Me.txtSubject = ""
116 Me.chkA = False
117 Me.chkB = False
118 Me.chkC = False
119 Me.chkD = False
120 Me.chkE = False
121 Me.chkF = False
122 Me.chkG = False
123 End Sub
124
125 Private Sub cmdCancel_Click()
126 'Reset objects on the form
127 Me.cmdAdd.Visible = True
128 Me.cmdSave.Visible = False
129 Me.cmdCancel.Visible = False
130 DataChanged = False
131 'Go to first record in the combo box
132 Me.cboSubject.Visible = True
133 Me.cboSubject.ListIndex = 0
134 'Use the event that the combo box was clicked to refresh the form
135 Call cboSubject_Click
136 End Sub
137
138 Private Sub cmdExit_Click()
139 Dim Reply
140 'Check to see if any changes made
141 If DataChanged = True Then
142 'Prompt user to save changes
143 Reply = MsgBox("Data has changed, do you want to save changes?", vbYesNo, "Warning")
144 If Reply = vbYes Then
145 'If they want to save, call SaveData function
146 If SaveData() = False Then
147 'Save errors, so do not process any further
148 Exit Sub
149 End If
150 End If
151 End If
152 DataChanged = False
153 'Close form
154 Unload Me
155 End Sub
156
157 Private Sub cmdSave_Click()
158 'Call SaveData function
159 If SaveData() = False Then
160 'Errors, so do not run rest of procedure code
161 Exit Sub
162 End If
163 'Reset objects on form from Add mode to normal mode
164 Me.cmdAdd.Visible = True
165 Me.cmdSave.Visible = False
166 Me.cmdCancel.Visible = False
167 'Increment end of file counter
168 LastSub = LastSub + 1
169 'Move focus to last (new) record
170 OnSub = LastSub
171 'Add new item to combo box
172 Me.cboSubject.AddItem Subject.SubName & Space(20) & OnSub
173 'Select new item in combo box
174 Me.cboSubject.Text = Subject.SubName & Space(20) & OnSub
175 'Set combo box visible again
176 Me.cboSubject.Visible = True
177 End Sub
178
179 Private Function SaveData() As Boolean
180 Dim CheckSum As Byte
181 'Clear last error message
182 Me.lblErrorBlock.Visible = False
183 Me.lblErrorSub.Visible = False
184 'Validate first
185 CheckSum = chkA + chkB + chkC + chkD + chkE + chkF + chkG
186 If CheckSum = 0 Then
187 'Set error message visible
188 Me.lblErrorBlock.Visible = True
189 'Set return value to indicate not saved
190 SaveData = False
191 'Do not continue process
192 Exit Function
193 End If
194 If Len(Trim(Me.txtSubject)) = 0 Then
195 'Set error message visible
196 Me.lblErrorSub.Visible = True
197 'Set return value to indicate not saved
198 SaveData = False
199 'Do not continue process
200 Exit Function
201 End If
202 'Valid data, so move data from form into record
203 With Subject
204 .BlockA = Me.chkA
205 .BlockB = Me.chkB
206 .BlockC = Me.chkC
207 .BlockD = Me.chkD
208 .BlockE = Me.chkE
209 .BlockF = Me.chkF
210 .BlockG = Me.chkG
211 .SubName = Me.txtSubject
212 .SubNo = OnSub
frmEditCourses.frm (06/02/2011) Page 3

213 End With


214 'Put record back into file
215 Put #2, OnSub, Subject
216 'Reset control flags
217 DataChanged = False
218 'Set return value to indicate saved
219 SaveData = True
220 End Function
221
frmEditStudent.frm (06/02/2011) Page 1

1 Dim DataChanged As Boolean


2
3 Private Sub cmdAdd_Click()
4 Dim Reply
5 'Check to see if data has changed before creating new record
6 If DataChanged = True Then
7 'Changed - so prompt to save
8 Reply = MsgBox("Data has changed, do you want to save changes?", vbYesNo, "Warning")
9 If Reply = vbYes Then
10 'Save record, call function
11 If SaveData() = False Then
12 'cannot save due to errors to exit process
13 Exit Sub
14 End If
15 End If
16 End If
17
18 DataChanged = True
19 'Reset objects on form for add mode
20 Me.cboStudentNo.Visible = False
21 Me.cmdSave.Visible = True
22 Me.cmdCancel.Visible = True
23 Me.cmdAdd.Visible = False
24 'Go to the position last record plus 1
25 OnStud = LastStud + 1
26 'Initialise form values for new record
27 Me.lblStudNo = Format(OnStud, "000000")
28 Me.txtStudID = ""
29 Me.txtSurname = ""
30 Me.txtForename = ""
31 Me.txtStreet = ""
32 Me.txtDistrict = ""
33 Me.txtTown = "Shrewsbury"
34 Me.txtCounty = "Shropshire"
35 Me.txtPostcode = ""
36 End Sub
37
38 Private Sub cmdCancel_Click()
39 'Reset objects on the form
40 Me.cmdAdd.Visible = True
41 Me.cmdSave.Visible = False
42 Me.cmdCancel.Visible = False
43 DataChanged = False
44 'Go to first record in the combo box
45 Me.cboStudentNo.Visible = True
46 Me.cboStudentNo.ListIndex = 0
47 'Use the event that the combo box was clicked to refresh the form
48 Call cboStudentNo_Click
49 End Sub
50
51 Private Sub cmdExit_Click()
52 Unload Me
53 End Sub
54
55 Private Sub Form_Load()
56 'Empty combo box
57 Me.cboStudentNo.Clear
58 'For each student in file add to combo box
59 For OnStud = 1 To LastStud
60 Get #1, OnStud, Student
61 With Student
62 'Form combo list from surname and forename,
63 ' with their student number at far right - away from user view
64 Me.cboStudentNo.AddItem UCase(Trim(.Surname)) & " " & .Forename & Space(20) & OnStud
65 End With
66 Next OnStud
67 'If at least one item in list, select first item
68 If LastStud <> 0 Then Me.cboStudentNo.ListIndex = 0
69 'Set variable to monitor changes in data to false
70 DataChanged = False
71 End Sub
72
73 Private Sub cboStudentNo_Click()
74 Dim Reply
75 'Data has changed - prompt to save
76 If DataChanged = True Then
77 Reply = MsgBox("Data has changed, do you want to save changes?", vbYesNo, "Warning")
78 If Reply = vbYes Then
79 'If yes then attempt to save
80 If SaveData() = False Then
81 'cannot save due to errors to exit process
82
83 Exit Sub
84 End If
85 End If
86 End If
87
88 'Store value of student selected
89 OnStud = Val(Right(Me.cboStudentNo, 6))
90 'Get that record from file and store in Student
91 Get #1, OnStud, Student
92 'Populate the form with the record values
93 With Student
94 Me.lblStudNo = OnStud
95 Me.txtStudID = .StudID
96 Me.txtSurname = .Surname
97 Me.txtForename = .Forename
98 Me.txtStreet = .Street
99 Me.txtDistrict = .District
100 Me.txtTown = .Town
101 Me.txtCounty = .County
102 Me.txtPostcode = .Postcode
103 End With
104 'Reset data change flag to false
105 DataChanged = False
106 End Sub
frmEditStudent.frm (06/02/2011) Page 2

107
108 Private Sub cmdSave_Click()
109 'Call SaveData function
110 If SaveData() = False Then
111 'Errors, so do not run rest of procedure code
112 Exit Sub
113 End If
114 'Reset objects on form from Add mode to normal mode
115 Me.cmdAdd.Visible = True
116 Me.cmdSave.Visible = False
117 Me.cmdCancel.Visible = False
118 'Increment end of file counter
119 LastStud = LastStud + 1
120 'Move focus to last (new) record
121 OnStud = LastStud
122 'Add new item to combo box
123 Me.cboStudentNo.AddItem UCase(Trim(Student.Surname)) & " " & Student.Forename & Space(20) & LastStud
124 'Select new item in combo box
125 Me.cboStudentNo.Text = UCase(Trim(Student.Surname)) & " " & Student.Forename & Space(20) & LastStud
126 'Set combo box visible again
127 Me.cboStudentNo.Visible = True
128 'Reset data change flag to false
129 DataChanged = False
130 Call cboStudentNo_Click
131 End Sub
132
133 Private Function SaveData() As Boolean
134 Dim N As Byte
135 'Clear previous error message
136 For N = 0 To 7
137 Me.lblError(N).Visible = False
138 Next N
139
140 'Validate fields
141 '===============
142 'StudID
143 If IsStudID(Me.txtStudID) = False Then
144 Me.txtStudID.SetFocus
145 SaveData = False
146 Me.lblError(0).Visible = True
147 Exit Function
148 End If
149 'Surname
150 If IsName(Me.txtSurname) = False Then
151 Me.txtSurname.SetFocus
152 SaveData = False
153 Me.lblError(1).Visible = True
154 Exit Function
155 End If
156 'Forename
157 If IsName(Me.txtForename) = False Then
158 Me.txtForename.SetFocus
159 SaveData = False
160 Me.lblError(2).Visible = True
161 Exit Function
162 End If
163 'Street
164 If Len(Trim(Me.txtStreet)) = 0 Then
165 Me.txtStreet.SetFocus
166 SaveData = False
167 Me.lblError(3).Visible = True
168 Exit Function
169 End If
170 'Distict
171 If IsWords(txtDistrict) = False And Len(Trim(InString)) > 0 Then
172 Me.txtForename.SetFocus
173 SaveData = False
174 Me.lblError(4).Visible = True
175 Exit Function
176 End If
177 'Town
178 If IsWords(Me.txtTown) = False Then
179 Me.txtTown.SetFocus
180 SaveData = False
181 Me.lblError(5).Visible = True
182 Exit Function
183 End If
184 'County
185 Me.txtCounty = Trim(txtCounty)
186 If IsName(Me.txtCounty) = False Then
187 Me.txtCounty.SetFocus
188 SaveData = False
189 Me.lblError(6).Visible = True
190 Exit Function
191 End If
192 'Postcode
193 If IsPostCode(Me.txtPostcode) = False Then
194 Me.txtPostcode.SetFocus
195 SaveData = False
196 Me.lblError(7).Visible = True
197 Exit Function
198 End If
199 SaveData = True
200
201 'If it gets here then it is valid - so save record
202
203 'Copy form values into record
204 With Student
205 .StudNo = OnStud
206 .StudID = Me.txtStudID
207 .Surname = Me.txtSurname
208 .Forename = Me.txtForename
209 .Street = Me.txtStreet
210 .District = Me.txtDistrict
211 .Town = Me.txtTown
212 .County = Me.txtCounty
frmEditStudent.frm (06/02/2011) Page 3

213 .Postcode = Me.txtPostcode


214 End With
215 'Put record back into file
216 Put #1, OnStud, Student
217 DataChanged = False
218 End Function
219
220 Private Sub txtCounty_Change()
221 DataChanged = True
222 End Sub
223
224 Private Sub txtDistrict_Change()
225 DataChanged = True
226 End Sub
227
228 Private Sub txtForename_Change()
229 DataChanged = True
230 End Sub
231
232 Private Sub txtPostcode_Change()
233 DataChanged = True
234 End Sub
235
236 Private Sub txtStreet_Change()
237 DataChanged = True
238 End Sub
239
240 Private Sub txtStudID_Change()
241 DataChanged = True
242 End Sub
243
244 Private Sub txtSurname_Change()
245 DataChanged = True
246 End Sub
247
248 Private Sub txtTown_Change()
249 DataChanged = True
250 End Sub
frmUtilities.frm (06/02/2011) Page 1

1 Private Sub cmdBackup_Click()


2 Dim N As Long
3 'This assumes the three files are open
4
5 'Open backup file for Values and write record, then close
6 Open App.Path & "/Backup/Values.cfg" For Output As #4
7 Write #4, LastStud, LastEnrol, LastSub
8 Close #4
9
10 'Open backup file for student, write each record as variable length (csv), then close
11 Open App.Path & "/backup/Students.txt" For Output As #4
12 For N = 1 To LastStud
13 Get #1, N, Student
14 With Student
15 Write #4, .StudNo, Trim(.StudID), Trim(.Surname), Trim(.Forename), Trim(.Title), _
16 Trim(.Street), Trim(.District), Trim(.Town), Trim(.County), Trim(.Postcode)
17 End With
18 Next N
19 Close #4
20
21 'Open backup file for subjects, write each record as variable length (csv), then close
22 Open App.Path & "/backup/Subjects.txt" For Output As #4
23 For N = 1 To LastSub
24 Get #2, N, Subject
25 With Subject
26 Write #4, .SubNo, Trim(.SubName), .BlockA, .BlockB, .BlockC, .BlockD, .BlockE, .BlockF, .BlockG
27 End With
28 Next N
29 Close #4
30
31 'Open backup file for enrolment, write each record as variable length (csv), then close
32 Open App.Path & "/backup/Enrolment.txt" For Output As #4
33 For N = 1 To LastEnrol
34 Get #3, N, Enrolment
35 With Enrolment
36 Write #4, .EnrolNo, .StudNo, .SubNo, Block, .StartDate, .EndDate
37 End With
38 Next N
39 Close #4
40 MsgBox "Backup files succesfully", vbInformation, "Message"
41 Unload Me
42 End Sub
43
44 Private Sub cmdExit_Click()
45 Unload Me
46 End Sub
47
48 Private Sub cmdRestore_Click()
49 Dim RecLength As Integer
50
51 'Close each file, then delete files
52 Close #1
53 Close #2
54 Close #3
55 Kill App.Path & "/Student.dat"
56 Kill App.Path & "/Subject.dat"
57 Kill App.Path & "/Enrolment.dat"
58 Kill App.Path & "/Values.cfg"
59
60 'Read in backup of basic Values, store in global variables
61 Open App.Path & "/Backup/Values.cfg" For Input As #1
62 Input #1, LastStud, LastEnrol, LastSub
63 Close #1
64 'Write global variables, and over-write main Values file
65 Open App.Path & "/Values.cfg" For Output As #1
66 Write #1, LastStud, LastEnrol, LastSub
67 Close #1
68
69 'Set record position variables to before start of file
70 OnStud = 0
71 OnEnrol = 0
72 OnSub = 0
73
74 'Set length of fixed record
75 RecLength = LenB(Student)
76 'Open direct access file - created so empty
77 Open App.Path & "/Student.dat" For Random As #1 Len = RecLength
78 'Open csv file for input
79 Open App.Path & "/Backup/students.txt" For Input As #4
80 'Read each record from csv, copy to student record, then put in direct access file
81 For OnStud = 1 To LastStud
82 With Student
83 Input #4, .StudNo, .StudID, .Surname, .Forename, .Title, .Street, _
84 .District, .Town, .County, .Postcode
85 Put #1, .StudNo, Student
86 End With
87 Next OnStud
88 Close #4
89 'Close csv file
90
91 'Set length of fixed record
92 RecLength = LenB(Subject)
93 'Open direct access file - created so empty
94 Open App.Path & "/Subject.dat" For Random As #2 Len = RecLength
95 'Open csv file for input
96 Open App.Path & "/Backup/subjects.txt" For Input As #4
97 'Read each record from csv, copy to subject record, then put in direct access file
98 For OnSub = 1 To LastSub
99 With Subject
100 Input #4, .SubNo, .SubName, .BlockA, .BlockB, .BlockC, .BlockD, .BlockE, .BlockF, .BlockG
101 Put #2, .SubNo, Subject
102 End With
103 Next OnSub
104 Close #4
105 'Close csv file
106
frmUtilities.frm (06/02/2011) Page 2

107 'Set length of fixed record


108 RecLength = LenB(Enrolment)
109 'Open direct access file - created so empty
110 Open App.Path & "/Enrolment.dat" For Random As #3 Len = RecLength
111 'Open csv file for input
112 Open App.Path & "/Backup/Enrolment.txt" For Input As #4
113 'Read each record from csv, copy to enrolment record, then put in direct access file
114 For OnEnrol = 1 To LastEnrol
115 With Enrolment
116 Input #4, .EnrolNo, .StudNo, .SubNo, .Block, .StartDate, .EndDate
117 Put #3, .EnrolNo, Enrolment
118 End With
119 Next OnEnrol
120 Close #4
121 'Close csv file
122 MsgBox "Restored files succesfully", vbInformation, "Message"
123 Unload Me
124 End Sub
Module1.bas (06/02/2011) Page 1

1 Public Type StudRecord


2 StudNo As Long 'KEY - autonumber starting at 1
3 StudID As String * 7 'Unique external Student ID number
4 Surname As String * 30 'Surname of customer
5 Forename As String * 25 'First name of customer
6 Title As String * 5 'Title, e.g. Mr/Miss/Ms/Mrs/Rev
7 Street As String * 40 'First line of address
8 District As String * 25 'Locality, e.g. village
9 Town As String * 30 'Town
10 County As String * 25 'County
11 Postcode As String * 7 'Postcode
12 End Type
13
14 Public Type SubjectRecord
15 SubNo As Long 'KEY - autonumber starting at 1
16 SubName As String * 30 'Subject name
17 BlockA As Byte 'Number of classes in block A
18 BlockB As Byte ' B
19 BlockC As Byte ' C
20 BlockD As Byte ' D
21 BlockE As Byte ' E
22 BlockF As Byte ' F
23 BlockG As Byte ' G
24 End Type
25
26 Public Type EnrolmentRec
27 EnrolNo As Long 'KEY - autonumber starting at 1
28 StudNo As Long 'Foreign key from Student file
29 SubNo As Long 'Foreign key from Subject file
30 Block As String * 1 'Blocked allocated to
31 StartDate As Date 'Date started
32 EndDate As Date 'Date finished - blank means active
33 End Type
34
35 'Single record for each one the files above
36 Public Student As StudRecord
37 Public Subject As SubjectRecord
38 Public Enrolment As EnrolmentRec
39
40 'Position of current record in each one of the files
41 Public OnStud As Long
42 Public OnSub As Long
43 Public OnEnrol As Long
44
45 'Position of last record in each file
46 Public LastStud As Long
47 Public LastSub As Long
48 Public LastEnrol As Long
49
50 'Function to test if all characters in a string are alphabetic
51 Public Function IsAlpha(ByVal InString As String) As Boolean
52 Dim N As Integer
53 Dim Char As String * 1
54 'Remove leadning and trailing spaces
55 InString = UCase(Trim(InString))
56 If Len(InString) = 0 Then
57 IsAlpha = False
58 Exit Function
59 End If
60 'For each character in the string
61 For N = 1 To Len(InString)
62 Char = Mid(InString, N, 1)
63 If Not (Char >= "A" And Char <= "Z") Then
64 'Not A to Z, so return False and exit function
65 IsAlpha = False
66 Exit Function
67 End If
68 Next N
69 'Only gets here if valid, so return true
70 IsAlpha = True
71 End Function
72
73 'Function to test if letters or hyphen only
74 Public Function IsName(ByVal InString As String) As Boolean
75 Dim N As Integer
76 Dim Char As String * 1
77 'Remove leadning and trailing spaces
78 InString = UCase(Trim(InString))
79 If Len(InString) = 0 Then
80 IsName = False
81 Exit Function
82 End If
83 'For each character in the string
84 For N = 1 To Len(InString)
85 Char = Mid(InString, N, 1)
86 If Not ((Char >= "A" And Char <= "Z") Or Char = "-") Then
87 'Not A to Z or hyphen, so return False and exit function
88 IsName = False
89 Exit Function
90 End If
91 Next N
92 'Only gets here if valid, so return true
93 IsName = True
94 End Function
95
96 'Function to test if letters and space
97 Public Function IsWords(ByVal InString As String) As Boolean
98 Dim N As Integer
99 Dim Char As String * 1
100 'Remove leadning and trailing spaces
101 InString = UCase(Trim(InString))
102 If Len(InString) = 0 Then
103 IsWords = False
104 Exit Function
105 End If
106 'For each character in the string
Module1.bas (06/02/2011) Page 2

107 For N = 1 To Len(InString)


108 Char = Mid(InString, N, 1)
109 'Not A to Z or space, so return False and exit function
110 If Not ((Char >= "A" And Char <= "Z") Or Char = " ") Then
111 'Not A to Z or space, so return False and exit function
112 IsWords = False
113 Exit Function
114 End If
115 Next N
116 'Only gets here if valid, so return true
117 IsWords = True
118 End Function
119
120 'Function to test if valid postcode, i.e. LL9?9LL format
121 Public Function IsPostCode(ByVal InString As String) As Boolean
122 Dim N As Integer
123 Dim Char As String * 1
124 'Remove leadning and trailing spaces
125 InString = UCase(Trim(InString))
126 'If length of string is 6, assume missed middle space, so insert this
127 If Len(InString) = 6 Then
128 InString = Left(InString, 3) & " " & Right(InString, 3)
129 End If
130 'If length of string not 7 then error
131 If Len(InString) <> 7 Then
132 'Not 7 characters, so return False and exit function
133 IsPostCode = False
134 Exit Function
135 End If
136 'Test first two digits alpha, 3rd digit numeric, 4th and 5th numeric (caters for space),
137 ' and last two digits alpha, return false if error, true otherwise.
138 If IsAlpha(Left(InString, 2)) = False Or IsNumeric(Mid(InString, 3, 1)) = False _
139 Or IsNumeric(Mid(InString, 4, 2)) = False Or IsAlpha(Right(InString, 2)) = False Then
140 IsPostCode = False
141 Else
142 IsPostCode = True
143 End If
144 End Function
145
146 'Function to test if valid student ID, i.e. 99-9999 format
147 Public Function IsStudID(ByVal InString As String) As Boolean
148 Dim N As Integer
149 Dim Char As String * 1
150 'Remove leadning and trailing spaces
151 InString = Trim(InString)
152 'If length of string is 6, assume missed middle hyhen, so insert this
153 If Len(InString) = 6 Then
154 InString = Left(InString, 2) & "-" & Right(InString, 4)
155 End If
156 'If length of string not 7 then error
157 If Len(InString) <> 7 Then
158 'Not 7 characters, so return False and exit function
159 IsStudID = False
160 Exit Function
161 End If
162 'Test first two digits numeric, 3rd digit a "-", and last four digits numeric,
163 ' return false if error, true otherwise.
164 If IsNumeric(Left(InString, 2)) = False Or IsNumeric(Right(InString, 4)) = False _
165 Or Mid(InString, 3, 1) <> "-" Then
166 IsStudID = False
167 Else
168 IsStudID = True
169 End If
170 End Function
171

Potrebbero piacerti anche