هدفي والحمد لله تم تحقيقه
تم والحمد لله تعميم الفكرة بحيث يمكن استعمالها في أكثر من جدول ، وحسب الحاجة دون تقييد .
أولا انشاء المديول العام :-
Function CreateYearsRecords(sourceTableName As String, employeeFieldName As String, startDateFieldName As String, _
endDateFieldName As String, targetTableName As String, targetEmployeeFieldName As String, targetStartDateFieldName As String, targetEndDateFieldName As String, targetYearsFieldName As String)
Dim sourceRS As DAO.Recordset
Dim targetRS As DAO.Recordset
Dim recordStartDate As Date
Dim recordEndDate As Date
Dim currentYear As Integer
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE " & targetTableName & ".* FROM " & targetTableName & ";"
DoCmd.SetWarnings True
Set sourceRS = CurrentDb.OpenRecordset(sourceTableName)
Set targetRS = CurrentDb.OpenRecordset(targetTableName)
Do Until sourceRS.EOF
recordStartDate = sourceRS.Fields(startDateFieldName)
recordEndDate = sourceRS.Fields(endDateFieldName)
For currentYear = Year(recordStartDate) To Year(recordEndDate)
targetRS.AddNew
targetRS.Fields(targetEmployeeFieldName) = sourceRS.Fields(employeeFieldName)
targetRS.Fields(targetStartDateFieldName) = sourceRS.Fields(startDateFieldName)
targetRS.Fields(targetEndDateFieldName) = sourceRS.Fields(endDateFieldName)
targetRS.Fields(targetYearsFieldName) = CStr(currentYear)
targetRS.Update
Next currentYear
sourceRS.MoveNext
Loop
sourceRS.Close
targetRS.Close
Set sourceRS = Nothing
Set targetRS = Nothing
End Function
ثانياً يمكن استدعاء الدالة كما يلي :-
Call CreateYearsRecords("date1", "t1", "t2", "t3", "TEMP_DATE", "EmployeeName", "StartDate", "EndDate", "Years")
بحيث يتم ادراج البيانات في جدول قابل للتغيير واسماء الحقول فيه أيضاً .
Get Date.accdb