استبدل الدالة القديمة ، بالدالة التالية ،
Private Sub AddToTempSelected()
On Error GoTo ErrorHandler
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Dim anneeValue As Variant
Dim gradeValue As Variant
Dim wilayaValue As Variant
Dim existingWilaya As String
Dim wilayaArray As Variant
Dim wilayaExists As Boolean
Dim i As Integer
Dim currentYear As Integer
Dim previousYear As Integer
Set db = CurrentDb()
currentYear = Me.ANNEE3.Column(0)
previousYear = currentYear - 1
anneeValue = previousYear & "/" & currentYear
gradeValue = Me.GRADE3.Column(0)
wilayaValue = Trim(Me.lst_XX.Column(0))
strSQL = "SELECT wilaya FROM temp_selected " & _
"WHERE annee = '" & Replace(anneeValue, "'", "''") & "' " & _
"AND grade = '" & Replace(gradeValue, "'", "''") & "'"
Set rs = db.OpenRecordset(strSQL)
If Not rs.EOF Then
existingWilaya = Nz(rs!wilaya, "")
If existingWilaya <> "" Then
wilayaArray = Split(existingWilaya, " - ")
wilayaExists = False
For i = LBound(wilayaArray) To UBound(wilayaArray)
If Trim(wilayaArray(i)) = wilayaValue Then
wilayaExists = True
Exit For
End If
Next i
If Not wilayaExists Then
If existingWilaya <> "" Then
existingWilaya = existingWilaya & " - " & wilayaValue
Else
existingWilaya = wilayaValue
End If
strSQL = "UPDATE temp_selected SET wilaya = '" & Replace(existingWilaya, "'", "''") & "' " & _
"WHERE annee = '" & Replace(anneeValue, "'", "''") & "' " & _
"AND grade = '" & Replace(gradeValue, "'", "''") & "'"
db.Execute strSQL, dbFailOnError
End If
Else
strSQL = "UPDATE temp_selected SET wilaya = '" & Replace(wilayaValue, "'", "''") & "' " & _
"WHERE annee = '" & Replace(anneeValue, "'", "''") & "' " & _
"AND grade = '" & Replace(gradeValue, "'", "''") & "'"
db.Execute strSQL, dbFailOnError
End If
Else
strSQL = "INSERT INTO temp_selected (annee, grade, wilaya) " & _
"VALUES ('" & Replace(anneeValue, "'", "''") & "', '" & Replace(gradeValue, "'", "''") & "', '" & Replace(wilayaValue, "'", "''") & "')"
db.Execute strSQL, dbFailOnError
End If
CleanUp:
If Not rs Is Nothing Then rs.Close
Set rs = Nothing
Set db = Nothing
Exit Sub
ErrorHandler:
MsgBox "حدث خطأ أثناء محاولة تحديث البيانات", vbCritical + vbMsgBoxRight, "خطأ"
Resume CleanUp
End Sub
حيث سيتم تخزين القيمة في الجدول مباشرة بالتنسيق الذي تريده ( السنة التي تم اختيارها = 2025 ، سيتم تخزينها = 2024 / 2025 في الحقل بعد ان تم تغيير نوعه الى حقل نصي )
BASE_e.accdb