Hi,
i have written a function using vba in one of my form but i want it to be available for use in other forms i created too. How do i do that?
The function is shown below.
Public Function GetShift(StaffID As Long, InputDate As Date) As String
On Error GoTo Err_GetShift
' Declare variables
Dim StartDate As Date
Dim ShiftPatternID As Long
Dim ShiftPattern As String
Dim CycleDays As Long
Dim ChangeCnt1 As Long
Dim ChangeCnt2 As Long
Dim ChangeCnt3 As Long
Dim ChangeCnt4 As Long
Dim IDate As Date
Dim ToDate As Date
IDate = Format(InputDate, "mm/dd/yyyy")
'Declare recordset - speed optimisation
Dim rs As Recordset
Dim strSQL As String
strSQL = "SELECT * FROM [tbl_staffShiftPattern],[tbl_shiftPattern] " _
& "WHERE tbl_staffShiftPattern.ShiftPatternID = tbl_ShiftPattern.Index " _
& "AND tbl_staffShiftPattern.StaffID = " & StaffID & ""
Set rs = CurrentDb.OpenRecordset(strSQL)
StartDate = rs!StartDate
ShiftPatternID = rs!ShiftPatternID
ShiftPattern = rs!Pattern
CycleDays = rs!CycleDays
ChangeCnt1 = DCount("[Index]", "tbl_shiftChangeMain", "[RequestorName]= " & StaffID & " AND [FromDate]= #" & IDate & "#")
ChangeCnt2 = DCount("[Index]", "tbl_shiftChangeMain", "[RequestorName]= " & StaffID & " AND [ToDate]= #" & IDate & "#")
ChangeCnt3 = DCount("[Index]", "tbl_shiftChangeSub", "[Name]= " & StaffID & " AND [FromDate]= #" & IDate & "#")
ChangeCnt4 = DCount("[Index]", "tbl_shiftChangeSub", "[Name]= " & StaffID & " AND [ToDate]= #" & IDate & "#")
If InputDate < StartDate Then
GetShift = "Err"
ElseIf ChangeCnt1 = 1 And ChangeCnt2 = 0 Then
ToDate = DLookup("[ToDate]", "tbl_shiftChangeMain", "[RequestorName]= " & StaffID & " AND [FromDate]= #" & IDate & "#")
ToDate = Format(ToDate, "mm/dd/yyyy")
GetShift = Mid(ShiftPattern, (DateDiff("d", StartDate, Format(ToDate, "dd/mm/yyyy")) Mod CycleDays) + 1, 1) + "*"
ElseIf ChangeCnt2 = 1 Then
GetShift = CStr(DLookup("[ToShift]", "tbl_shiftChangeMain", "[RequestorName] = " & StaffID & " AND [ToDate]= #" & IDate & "#")) + "*"
ElseIf ChangeCnt3 = 1 And ChangeCnt4 = 0 Then
ToDate = DLookup("[ToDate]", "tbl_shiftChangeSub", "[Name]= " & StaffID & " AND [FromDate]= #" & IDate & "#")
ToDate = Format(ToDate, "mm/dd/yyyy")
GetShift = Mid(ShiftPattern, (DateDiff("d", StartDate, Format(ToDate, "dd/mm/yyyy")) Mod CycleDays) + 1, 1) + "*"
ElseIf ChangeCnt4 = 1 Then
GetShift = CStr(DLookup("[ToShift]", "tbl_shiftChangeSub", "[Name] = " & StaffID & " AND [ToDate]= #" & IDate & "#")) + "*"
Else
GetShift = Mid(ShiftPattern, (DateDiff("d", StartDate, Format(InputDate, "dd/mm/yyyy")) Mod CycleDays) + 1, 1)
End If
Exit_GetShift:
Set rs = Nothing 'Deassign all objects.
Exit Function
Err_GetShift:
'Error handler here.
Resume Exit_GetShift
End Function
i have written a function using vba in one of my form but i want it to be available for use in other forms i created too. How do i do that?
The function is shown below.
Public Function GetShift(StaffID As Long, InputDate As Date) As String
On Error GoTo Err_GetShift
' Declare variables
Dim StartDate As Date
Dim ShiftPatternID As Long
Dim ShiftPattern As String
Dim CycleDays As Long
Dim ChangeCnt1 As Long
Dim ChangeCnt2 As Long
Dim ChangeCnt3 As Long
Dim ChangeCnt4 As Long
Dim IDate As Date
Dim ToDate As Date
IDate = Format(InputDate, "mm/dd/yyyy")
'Declare recordset - speed optimisation
Dim rs As Recordset
Dim strSQL As String
strSQL = "SELECT * FROM [tbl_staffShiftPattern],[tbl_shiftPattern] " _
& "WHERE tbl_staffShiftPattern.ShiftPatternID = tbl_ShiftPattern.Index " _
& "AND tbl_staffShiftPattern.StaffID = " & StaffID & ""
Set rs = CurrentDb.OpenRecordset(strSQL)
StartDate = rs!StartDate
ShiftPatternID = rs!ShiftPatternID
ShiftPattern = rs!Pattern
CycleDays = rs!CycleDays
ChangeCnt1 = DCount("[Index]", "tbl_shiftChangeMain", "[RequestorName]= " & StaffID & " AND [FromDate]= #" & IDate & "#")
ChangeCnt2 = DCount("[Index]", "tbl_shiftChangeMain", "[RequestorName]= " & StaffID & " AND [ToDate]= #" & IDate & "#")
ChangeCnt3 = DCount("[Index]", "tbl_shiftChangeSub", "[Name]= " & StaffID & " AND [FromDate]= #" & IDate & "#")
ChangeCnt4 = DCount("[Index]", "tbl_shiftChangeSub", "[Name]= " & StaffID & " AND [ToDate]= #" & IDate & "#")
If InputDate < StartDate Then
GetShift = "Err"
ElseIf ChangeCnt1 = 1 And ChangeCnt2 = 0 Then
ToDate = DLookup("[ToDate]", "tbl_shiftChangeMain", "[RequestorName]= " & StaffID & " AND [FromDate]= #" & IDate & "#")
ToDate = Format(ToDate, "mm/dd/yyyy")
GetShift = Mid(ShiftPattern, (DateDiff("d", StartDate, Format(ToDate, "dd/mm/yyyy")) Mod CycleDays) + 1, 1) + "*"
ElseIf ChangeCnt2 = 1 Then
GetShift = CStr(DLookup("[ToShift]", "tbl_shiftChangeMain", "[RequestorName] = " & StaffID & " AND [ToDate]= #" & IDate & "#")) + "*"
ElseIf ChangeCnt3 = 1 And ChangeCnt4 = 0 Then
ToDate = DLookup("[ToDate]", "tbl_shiftChangeSub", "[Name]= " & StaffID & " AND [FromDate]= #" & IDate & "#")
ToDate = Format(ToDate, "mm/dd/yyyy")
GetShift = Mid(ShiftPattern, (DateDiff("d", StartDate, Format(ToDate, "dd/mm/yyyy")) Mod CycleDays) + 1, 1) + "*"
ElseIf ChangeCnt4 = 1 Then
GetShift = CStr(DLookup("[ToShift]", "tbl_shiftChangeSub", "[Name] = " & StaffID & " AND [ToDate]= #" & IDate & "#")) + "*"
Else
GetShift = Mid(ShiftPattern, (DateDiff("d", StartDate, Format(InputDate, "dd/mm/yyyy")) Mod CycleDays) + 1, 1)
End If
Exit_GetShift:
Set rs = Nothing 'Deassign all objects.
Exit Function
Err_GetShift:
'Error handler here.
Resume Exit_GetShift
End Function