و خود فرم :
2.png
1.png
Option Compare Database
Option Explicit
Private Sub CalcAverage()
Const count_all = 8
Dim sum_all As Double
Dim count_non_zero As Long
Dim Value As Variant
sum_all = 0
count_non_zero = 0
If TB_Number_1 <> 0 Then
count_non_zero = count_non_zero + 1
sum_all = sum_all + TB_Number_1
End If
If TB_Number_2 <> 0 Then
count_non_zero = count_non_zero + 1
sum_all = sum_all + TB_Number_2
End If
If TB_Number_3 <> 0 Then
count_non_zero = count_non_zero + 1
sum_all = sum_all + TB_Number_3
End If
If TB_Number_4 <> 0 Then
count_non_zero = count_non_zero + 1
sum_all = sum_all + TB_Number_4
End If
If TB_Number_5 <> 0 Then
count_non_zero = count_non_zero + 1
sum_all = sum_all + TB_Number_5
End If
If TB_Number_6 <> 0 Then
count_non_zero = count_non_zero + 1
sum_all = sum_all + TB_Number_6
End If
If TB_Number_7 <> 0 Then
count_non_zero = count_non_zero + 1
sum_all = sum_all + TB_Number_7
End If
If TB_Number_8 <> 0 Then
count_non_zero = count_non_zero + 1
sum_all = sum_all + TB_Number_8
End If
Me.TB_COUNT_ALL = count_all
Me.TB_SUM_ALL = sum_all
Me.TB_AVERAGE_ALL = Round(sum_all / count_all, 3)
Me.TB_COUNT_NON_ZERO = count_non_zero
If count_non_zero > 0 Then
Me.TB_AVERAGE_NON_ZERO = Round(sum_all / count_non_zero, 3)
Else
Me.TB_AVERAGE_NON_ZERO = "Division By 0 !"
End If
End Sub
Private Sub BTN_CLEAR_ALL_Click()
Me.TB_COUNT_ALL = 8
Me.TB_SUM_ALL = 0
Me.TB_AVERAGE_ALL = 0
Me.TB_COUNT_NON_ZERO = 0
Me.TB_AVERAGE_NON_ZERO = Null
Me.TB_Number_1 = Null
Me.TB_Number_2 = Null
Me.TB_Number_3 = Null
Me.TB_Number_4 = Null
Me.TB_Number_5 = Null
Me.TB_Number_6 = Null
Me.TB_Number_7 = Null
Me.TB_Number_8 = Null
End Sub
Private Sub Form_Load()
BTN_CLEAR_ALL_Click
End Sub
Private Sub TB_Number_1_BeforeUpdate(Cancel As Integer)
If (Not NumberIsValid(Me.TB_Number_1.Text)) Then
MsgBox _
prompt:="Enter a number between 0 and 100," & vbCrLf & "or leave the field blank (means 0)", _
buttons:=vbCritical, _
title:=Me.LBL_Number_1.caption
Cancel = True
End If
End Sub
Private Sub TB_Number_1_Exit(Cancel As Integer)
If Trim(Me.TB_Number_1.Text) = "" Then
Me.TB_Number_1 = 0
Else
Me.TB_Number_1 = CDbl(Me.TB_Number_1)
End If
CalcAverage
End Sub
Private Sub TB_Number_1_KeyPress(KeyAscii As Integer)
If Not KeyIsValid(KeyAscii) Then KeyAscii = 0
End Sub
Private Sub TB_Number_2_BeforeUpdate(Cancel As Integer)
If (Not NumberIsValid(Me.TB_Number_2.Text)) Then
MsgBox _
prompt:="Enter a number between 0 and 100," & vbCrLf & "or leave the field blank (means 0)", _
buttons:=vbCritical, _
title:=Me.LBL_Number_2.caption
Cancel = True
End If
End Sub
Private Sub TB_Number_2_Exit(Cancel As Integer)
If Trim(Me.TB_Number_2.Text) = "" Then
Me.TB_Number_2 = 0
Else
Me.TB_Number_2 = CDbl(Me.TB_Number_2)
End If
CalcAverage
End Sub
Private Sub TB_Number_2_KeyPress(KeyAscii As Integer)
If Not KeyIsValid(KeyAscii) Then KeyAscii = 0
End Sub
Private Sub TB_Number_3_BeforeUpdate(Cancel As Integer)
If (Not NumberIsValid(Me.TB_Number_3.Text)) Then
MsgBox _
prompt:="Enter a number between 0 and 100," & vbCrLf & "or leave the field blank (means 0)", _
buttons:=vbCritical, _
title:=Me.LBL_Number_3.caption
Cancel = True
End If
End Sub
Private Sub TB_Number_3_Exit(Cancel As Integer)
If Trim(Me.TB_Number_3.Text) = "" Then
Me.TB_Number_3 = 0
Else
Me.TB_Number_3 = CDbl(Me.TB_Number_3)
End If
CalcAverage
End Sub
Private Sub TB_Number_3_KeyPress(KeyAscii As Integer)
If Not KeyIsValid(KeyAscii) Then KeyAscii = 0
End Sub
Private Sub TB_Number_4_BeforeUpdate(Cancel As Integer)
If (Not NumberIsValid(Me.TB_Number_4.Text)) Then
MsgBox _
prompt:="Enter a number between 0 and 100," & vbCrLf & "or leave the field blank (means 0)", _
buttons:=vbCritical, _
title:=Me.LBL_Number_4.caption
Cancel = True
End If
End Sub
Private Sub TB_Number_4_Exit(Cancel As Integer)
If Trim(Me.TB_Number_4.Text) = "" Then
Me.TB_Number_4 = 0
Else
Me.TB_Number_4 = CDbl(Me.TB_Number_4)
End If
CalcAverage
End Sub
Private Sub TB_Number_4_KeyPress(KeyAscii As Integer)
If Not KeyIsValid(KeyAscii) Then KeyAscii = 0
End Sub
Private Sub TB_Number_5_BeforeUpdate(Cancel As Integer)
If (Not NumberIsValid(Me.TB_Number_5.Text)) Then
MsgBox _
prompt:="Enter a number between 0 and 100," & vbCrLf & "or leave the field blank (means 0)", _
buttons:=vbCritical, _
title:=Me.LBL_Number_5.caption
Cancel = True
End If
End Sub
Private Sub TB_Number_5_Exit(Cancel As Integer)
If Trim(Me.TB_Number_5.Text) = "" Then
Me.TB_Number_5 = 0
Else
Me.TB_Number_5 = CDbl(Me.TB_Number_5)
End If
CalcAverage
End Sub
Private Sub TB_Number_5_KeyPress(KeyAscii As Integer)
If Not KeyIsValid(KeyAscii) Then KeyAscii = 0
End Sub
Private Sub TB_Number_6_BeforeUpdate(Cancel As Integer)
If (Not NumberIsValid(Me.TB_Number_6.Text)) Then
MsgBox _
prompt:="Enter a number between 0 and 100," & vbCrLf & "or leave the field blank (means 0)", _
buttons:=vbCritical, _
title:=Me.LBL_Number_6.caption
Cancel = True
End If
End Sub
Private Sub TB_Number_6_Exit(Cancel As Integer)
If Trim(Me.TB_Number_6.Text) = "" Then
Me.TB_Number_6 = 0
Else
Me.TB_Number_6 = CDbl(Me.TB_Number_6)
End If
CalcAverage
End Sub
Private Sub TB_Number_6_KeyPress(KeyAscii As Integer)
If Not KeyIsValid(KeyAscii) Then KeyAscii = 0
End Sub
Private Sub TB_Number_7_BeforeUpdate(Cancel As Integer)
If (Not NumberIsValid(Me.TB_Number_7.Text)) Then
MsgBox _
prompt:="Enter a number between 0 and 100," & vbCrLf & "or leave the field blank (means 0)", _
buttons:=vbCritical, _
title:=Me.LBL_Number_7.caption
Cancel = True
End If
End Sub
Private Sub TB_Number_7_Exit(Cancel As Integer)
If Trim(Me.TB_Number_7.Text) = "" Then
Me.TB_Number_7 = 0
Else
Me.TB_Number_7 = CDbl(Me.TB_Number_7)
End If
CalcAverage
End Sub
Private Sub TB_Number_7_KeyPress(KeyAscii As Integer)
If Not KeyIsValid(KeyAscii) Then KeyAscii = 0
End Sub
Private Sub TB_Number_8_BeforeUpdate(Cancel As Integer)
If (Not NumberIsValid(Me.TB_Number_8.Text)) Then
MsgBox _
prompt:="Enter a number between 0 and 100," & vbCrLf & "or leave the field blank (means 0)", _
buttons:=vbCritical, _
title:=Me.LBL_Number_8.caption
Cancel = True
End If
End Sub
Private Sub TB_Number_8_Exit(Cancel As Integer)
If Trim(Me.TB_Number_8.Text) = "" Then
Me.TB_Number_8 = 0
Else
Me.TB_Number_8 = CDbl(Me.TB_Number_8)
End If
CalcAverage
End Sub
Private Sub TB_Number_8_KeyPress(KeyAscii As Integer)
If Not KeyIsValid(KeyAscii) Then KeyAscii = 0
End Sub