4. More Exploration of the Visual Basic Toolbox
Exercise 4: Student Database Input Screen
You did so well with last week�s assignment that, now, a school
wants you to develop the beginning structure of an input screen for
its students. The required input information is:
- Student Name
- Student Grade (1 through 6)
- Student Sex (Male or Female)
- Student Date of Birth (Month, Day, Year)
- Student Picture (Assume they can be loaded as bitmap files)
Set up the screen so that only the Name needs to be typed; all
other inputs should be set with option buttons, scroll bars, and common
dialog boxes. When a screen of information is complete, display the
summarized profile in a message box. This profile message box should
resemble this:
Note the student�s age must be computed from the input birth
date - watch out for pitfalls in doing the computation. The student�s
picture does not appear in the profile, only on the input screen.
My Solution:
Form:
Properties:
Form frmStudent:
BorderStyle = 1- Fixed Single
Caption = Student Profile
CommandButton cmdLoad:
Frame Frame3:
Caption = Picture
FontName = MS Sans Serif
FontBold = True
FontSize = 9.75
FontItalic = True
Image imgStudent:
BorderStyle = 1 - Fixed Single
Stretch = True
CommandButton cmdExit:
CommandButton cmdNew:
CommandButton cmdShow:
Frame Frame4:
Caption = Grade Level
FontName = MS Sans Serif
FontBold = True
FontSize = 9.75
FontItalic = True
OptionButton optLevel:
Caption = Grade 6
Index = 5
OptionButton optLevel:
Caption = Grade 5
Index = 4
OptionButton optLevel:
Caption = Grade 4
Index = 3
OptionButton optLevel:
Caption = Grade 3
Index = 2
OptionButton optLevel:
Caption = Grade 2
Index = 1
OptionButton optLevel:
Caption = Grade 1
Index = 0
Frame Frame2:
Caption = Sex
FontName = MS Sans Serif
FontBold = True
FontSize = 9.75
FontItalic = True
OptionButton optSex:
Caption = Female
Index = 1
OptionButton optSex:
Frame Frame1:
Caption = Date of Birth
FontName = MS Sans Serif
FontBold = True
FontSize = 9.75
FontItalic = True
VScrollBar vsbYear:
Max = 1800
Min = 2100
Value = 1960
VScrollBar vsbDay:
Max = 1
Min = 31
Value = 1
VScrollBar vsbMonth:
Max = 1
Min = 12
Value = 1
Label lblYear:
Alignment = 2 - Center
BackColor = &H00FFFFFF& (White)
BorderStyle = 1 - Fixed Single
FontName = MS Sans Serif
FontSize = 10.8
Label lblDay:
Alignment = 2 - Center
BackColor = &H00FFFFFF& (White)
BorderStyle = 1 - Fixed Single
FontName = MS Sans Serif
FontSize = 10.8
Label lblMonth:
Alignment = 2 - Center
BackColor = &H00FFFFFF& (White)
BorderStyle = 1 - Fixed Single
FontName = MS Sans Serif
FontSize = 10.8
TextBox txtName:
FontName = MS Sans Serif
FontSize = 10.8
CommonDialog cdlBox:
Filter = Bitmaps (*.bmp)|*.bmp
Label Label1:
Caption = Name
FontName = MS Sans Serif
FontBold = True
FontSize = 9.75
FontItalic = True
Code:
General Declarations:
Option Explicit
Dim Months(12) As String
Dim Days(12) As Integer
Dim Grade As String
cmdExit Click Event:
Private Sub cmdExit_Click()
End Sub
cmdLoad Click Event:
Private Sub cmdLoad_Click()
cdlbox.ShowOpen
imgStudent.Picture = LoadPicture(cdlbox.filename)
End Sub
cmdNew Click Event:
Private Sub cmdNew_Click()
'Blank out name and picture
txtName.Text = ""
imgStudent.Picture = LoadPicture("")
End Sub
cmdShow Click Event:
Private Sub cmdShow_Click()
Dim Is_Leap As Integer
Dim Msg As String, Age As Integer, Pronoun As String
Dim M As Integer, D As Integer, Y As Integer
'Check for leap year and if February is current month
If vsbMonth.Value = 2 And ((vsbYear.Value Mod 4 = 0 And vsbYear.Value Mod 100 <> 0) Or vsbYear.Value Mod 400 = 0) Then
Else
End If
'Check to make sure current day doesn't exceed number of days in month
If vsbDay.Value > Days(vsbMonth.Value) + Is_Leap Then
MsgBox "Only" + Str(Days(vsbMonth.Value) + Is_Leap) + " days in " + Months(vsbMonth.Value), vbOKOnly + vbCritical, "Invalid Birth Date"
Exit Sub
End If
'Get current date to compute age
M = Val(Format(Now, "mm"))
D = Val(Format(Now, "dd"))
Y = Val(Format(Now, "yyyy"))
Age = Y - vsbYear
If vsbMonth.Value > M Or (vsbMonth.Value = M And vsbDay > D) Then Age = Age - 1
'Check for valid age
If Age < 0 Then
MsgBox "Birth date is before current date.", vbOKOnly + vbCritical, "Invalid Birth Date"
Exit Sub
End If
'Check to make sure name entered
If txtName.Text = "" Then
MsgBox "The profile requires a name.", vbOKOnly + vbCritical, "No Name Entered"
Exit Sub
End If
'Put together student profile message
Msg = txtName.Text + " is a student in the " + Grade + " grade." + vbCr
If optSex(0).Value = True Then Pronoun = "He " Else Pronoun = "She "
Msg = Msg + Pronoun + " is" + Str(Age) + " years old." + vbCr
MsgBox Msg, vbOKOnly, "Student Profile"
End Sub
Form Load Event:
Private Sub Form_Load()
'Set arrays for dates and initialize labels
Months(1) = "January": Days(1) = 31
Months(2) = "February": Days(2) = 28
Months(3) = "March": Days(3) = 31
Months(4) = "April": Days(4) = 30
Months(5) = "May": Days(5) = 31
Months(6) = "June": Days(6) = 30
Months(7) = "July": Days(7) = 31
Months(8) = "August": Days(8) = 31
Months(9) = "September": Days(9) = 30
Months(10) = "October": Days(10) = 31
Months(11) = "November": Days(11) = 30
Months(12) = "December": Days(12) = 31
lblMonth.Caption = Months(vsbMonth.Value)
lblDay.Caption = Str(vsbDay.Value)
lblYear.Caption = Str(vsbYear.Value)
Grade = "first"
End Sub
optLevel Click Event:
Private Sub optLevel_Click(Index As Integer)
Select Case Index
Case 0
Case 1
Case 2
Case 3
Case 4
Case 5
End Select
End Sub
vsbDay Change Event:
Private Sub vsbDay_Change()
lblDay.Caption = Str(vsbDay.Value)
End Sub
vsbMonth Change Event:
Private Sub vsbMonth_Change()
lblMonth.Caption = Months(vsbMonth.Value)
End Sub
vsbYear Change Event:
Private Sub vsbYear_Change()
lblYear.Caption = Str(vsbYear.Value)
End Sub
Counter Hit
This Homepage is special brought to you by CK Tan