Dim strTrainer_Name as string
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim intCheck As Integer
Dim strDuration As String
Dim strCondition As String
Dim strMessage As String
Dim intResponse As Byte
Const BTWN_DATES As String = " BETWEEN [Start_Date] AND [End_Date]"
If Me.Duration = "All Day" Then
strDuration = " AND Not [Duration] Is Null"
Else
strDuration = " AND [Duration] IN ('All Day','" & Me.Duration & "')"
End If
strCondition = "(#" & Me.Start_Date & "#" & BTWN_DATES & strDuration & Trainer_Name & ")" & _
" OR " & _
"(#" & Me.End_Date & "#" & BTWN_DATES & strDuration & Trainer_Name & ")"
intCheck = DCount("*", "[Resourcing]", strCondition)
If intCheck > 0 Then
Cancel = True
strMessage = "This event clashes with" & IIf(intCheck > 1, " an ", " ") & _
"existing event" & IIf(intCheck > 1, "s", "") & _
vbNewLine & vbNewLine & _
"Do you wish to view and edit the event" & IIf(intCheck > 1, "s?", "?")
intResponse = MsgBox(strMessage, vbYesNo + vbQuestion, "Event clash do you wish to edit the existing event")
If intResponse = vbYes Then
DoCmd.OpenForm "Edit Hours", , , strCondition
End If
End If
End Sub
Dim strTrainer_Name as string
strCondition = "(#" & Me.Start_Date & "#" & BTWN_DATES & strDuration & strTrainer_Name & ")" & _
" OR " & _
"(#" & Me.End_Date & "#" & BTWN_DATES & strDuration & strTrainer_Name & ")"
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim intCheck As Integer
Dim strDuration As String
Dim strTrainer_Name As String
Dim strCondition As String
Dim strMessage As String
Dim intResponse As Byte
Const BTWN_DATES As String = " BETWEEN [Start_Date] AND [End_Date]"
If Me.Duration = "All Day" Then
strDuration = " AND Not [Duration] Is Null"
Else
strDuration = " AND [Duration] IN ('All Day','" & Me.Duration & "')"
End If
strCondition = "(#" & Me.Start_Date & "#" & BTWN_DATES & strDuration & strTrainer_Name & ")" & _
" OR " & _
"(#" & Me.End_Date & "#" & BTWN_DATES & strDuration & strTrainer_Name & ")"
intCheck = DCount("*", "[Resourcing]", strCondition)
If intCheck > 0 Then
Cancel = True
strMessage = "This event clashes with" & IIf(intCheck > 1, " an ", " ") & _
"existing event" & IIf(intCheck > 1, "s", "") & _
vbNewLine & vbNewLine & _
"Do you wish to view and edit the event" & IIf(intCheck > 1, "s?", "?")
intResponse = MsgBox(strMessage, vbYesNo + vbQuestion, "Event clash do you wish to edit the existing event")
If intResponse = vbYes Then
DoCmd.OpenForm "Edit Hours", , , strCondition
End If
End If
End Sub
Dim intCheck As Integer
Dim strDates As String
Dim strDuration As String
Dim strTrainer As String
Dim strCondition As String
Dim strMessage As String
Dim intResponse As Byte
strTrainer = "[Trainer_Name] = '" & Me.Trainer_Name & "'"
strDates = " AND [Start_Date] IN (#" & Me.Start_Date & "#,#" & Me.End_Date & "#)"
If Me.Duration = "All Day" Then
strDuration = " AND Not [Duration] Is Null"
Else
strDuration = " AND [Duration] IN ('All Day','" & Me.Duration & "')"
End If
strCondition = strTrainer & strDates & strDuration
strMessage = "This event clashes with" & IIf(intCheck [B][COLOR=red]= 1[/COLOR][/B], " an ", " ") & _
"existing event" & IIf(intCheck > 1, "s", "") & _
vbNewLine & vbNewLine & _
"Do you wish to view and edit the event" & IIf(intCheck > 1, "s?", "?")
If you're wondering go ahead and give it a try to see if it makes a difference. That's how you learn.Code:strMessage = "This event clashes with" & IIf(intCheck [B][COLOR=red]= 1[/COLOR][/B], " an ", " ") & _ "existing event" & IIf(intCheck > 1, "s", "") & _ vbNewLine & vbNewLine & _ "Do you wish to view and edit the event" & IIf(intCheck > 1, "s?", "?")
This event clashes with [COLOR="red"]an[/COLOR] existing event
This event clashes with existing event[COLOR="Red"]s[/COLOR]
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim intCheck As Integer
Dim strDates As String
Dim strDuration As String
Dim strTrainer As String
Dim strCondition As String
Dim strMessage As String
Dim intResponse As Byte
strTrainer = "[Trainer_Name] = '" & Me.Trainer_Name & "'"
strDates = " AND [Start_Date] IN (#" & Me.Start_Date & "#,#" & Me.End_Date & "#)"
If Me.Duration = "All Day" Then
strDuration = " AND Not [Duration] Is Null"
Else
strDuration = " AND [Duration] IN ('All Day','" & Me.Duration & "')"
End If
strCondition = strTrainer & strDates & strDuration
intCheck = DCount("*", "[Resourcing]", strCondition)
If intCheck > 0 Then
Cancel = True
strMessage = "This event clashes with" & IIf(intCheck = 1, " an ", " ") & _
"existing event" & IIf(intCheck > 1, "s", "") & _
vbNewLine & vbNewLine & _
"Do you wish to view and edit the event" & IIf(intCheck > 1, "s?", "?")
intResponse = MsgBox(strMessage, vbYesNo + vbQuestion, "Event clash do you wish to edit the existing event")
If intResponse = vbYes Then
DoCmd.OpenForm "Edit Hours", , , strCondition
End If
End If
End Sub
I need to see the code.The records are saved using the 'Create Records button which has code behind it which may be clashing.
Private Sub Create_multiple_records_Click()
Dim strSQL As String
'As you are entering the first record manually with the end date the sql only addes the dates greater than the first date
'This only works where all fields are filled in - you should probably build in a check to see if the Trainer and the Date are accounted for already
'dlookup("Trainer_Name", "Resourcing", "Trainer_Name = " & [Forms]![Resourcing]![Cmb_Trainer_Name] & " and StartDate = " & [the dates])
'If you can omit data from a field then you will have to breakdown the sql statement to omit the data if null - example for the field "Activity" - you would also remove the validation for Null Field
'Validate fields are not Null
If IsNull(Me.Start_Date) Then
MsgBox "Start Date Missing", vbOKOnly + vbCritical, "Error"
Me.Start_Date.SetFocus
ElseIf IsNull(Me.End_Date) Then
MsgBox "End Date Missing", vbOKOnly + vbCritical, "Error"
Me.End_Date.SetFocus
ElseIf IsNull(Me.Cmb_Activity) Then
MsgBox "Activity Missing", vbOKOnly + vbCritical, "Error"
Me.Cmb_Activity.SetFocus
ElseIf IsNull(Me.Cmb_Duration) Then
MsgBox "Duration Missing", vbOKOnly + vbCritical, "Error"
Me.Cmb_Duration.SetFocus
Else
'build the SQL Statement
strSQL = "INSERT INTO Resourcing ( Start_Date, Duration, Project_Title, Trainer_Name, Team"
If Not IsNull(Me.Cmb_Activity) Then
strSQL = strSQL & ", Activity"
End If
strSQL = strSQL & ", Training_Type ) "
strSQL = strSQL & " SELECT Dates.Work_Dates, [Forms]![Resourcing]![Cmb_Duration] AS Expr1, [Forms]![Resourcing]![Cmb_Project_Title] AS Expr2, [Forms]![Resourcing]![Cmb_Trainer_Name] AS Expr3, [Forms]![Resourcing]![Cmb_Team] AS Expr4"
If Not IsNull(Me.Cmb_Activity) Then
strSQL = strSQL & " , [Forms]![Resourcing]![Cmb_Activity] AS Expr5"
End If
strSQL = strSQL & " , [Forms]![Resourcing]![Cmb_Training_Type] AS Expr6 "
strSQL = strSQL & " FROM Dates WHERE (((Dates.Work_Dates)>[Forms]![Resourcing]![Start_Date] And (Dates.Work_Dates)<=[Forms]![Resourcing]![End_Date]))"
'Run the SQL Statement
DoCmd.RunSQL (strSQL)
End If
'To show the project title description in the activity box:
Activity = Activity & ": " & [Forms]![Resourcing]![Project_Title]
'To go to a new record
Me.Requery
DoCmd.GoToRecord , , acNewRec
'To requery the resourcing form and refresh the Edit Hours form
If CurrentProject.AllForms("2014 Resources").IsLoaded Then
[Forms]![2014 Resources].Requery
If CurrentProject.AllForms("Edit Hours").IsLoaded Then
[Forms]![Edit Hours].Refresh
End If
End If
End Sub
Private Sub New_Record_Click()
On Error GoTo Err_New_Record_Click
DoCmd.GoToRecord , , acNewRec
Exit_New_Record_Click:
Exit Sub
Err_New_Record_Click:
MsgBox Err.Description
Resume Exit_New_Record_Click
End Sub
Cancel =
Private Sub Create_multiple_records_warn_about_duplicates_Click()
Dim intCheck As Integer
Dim strSQL As String
Dim strDates As String
Dim strDuration As String
Dim strTrainer As String
Dim strCondition As String
Dim strMessage As String
Dim intResponse As Byte
strTrainer = "[Trainer_Name] = '" & Me.Trainer_Name & "'"
strDates = " AND [Start_Date] IN (#" & Me.Start_Date & "#,#" & Me.End_Date & "#)"
If Me.Duration = "All Day" Then
strDuration = " AND Not [Duration] Is Null"
Else
strDuration = " AND [Duration] IN ('All Day','" & Me.Duration & "')"
End If
strCondition = strTrainer & strDates & strDuration
intCheck = DCount("*", "[Resourcing]", strCondition)
If intCheck > 0 Then
Cancel = True
strMessage = "This event clashes with" & IIf(intCheck = 1, " an ", " ") & _
"existing event" & IIf(intCheck > 1, "s", "") & _
vbNewLine & vbNewLine & _
"Do you wish to view and edit the event" & IIf(intCheck > 1, "s?", "?")
intResponse = MsgBox(strMessage, vbYesNo + vbQuestion, "Event clash do you wish to edit the existing event")
If intResponse = vbYes Then
DoCmd.OpenForm "Edit Hours", , , strCondition
ElseIf IsNull(Me.Start_Date) Then
MsgBox "Start Date Missing", vbOKOnly + vbCritical, "Error"
Me.Start_Date.SetFocus
ElseIf IsNull(Me.End_Date) Then
MsgBox "End Date Missing", vbOKOnly + vbCritical, "Error"
Me.End_Date.SetFocus
ElseIf IsNull(Me.Cmb_Activity) Then
MsgBox "Activity Missing", vbOKOnly + vbCritical, "Error"
Me.Cmb_Activity.SetFocus
ElseIf IsNull(Me.Cmb_Duration) Then
MsgBox "Duration Missing", vbOKOnly + vbCritical, "Error"
Me.Cmb_Duration.SetFocus
Else
strSQL = "INSERT INTO Resourcing ( Start_Date, Duration, Project_Title, Trainer_Name, Team"
If Not IsNull(Me.Cmb_Activity) Then
strSQL = strSQL & ", Activity"
End If
End If
End If
strSQL = strSQL & ", Training_Type ) "
strSQL = strSQL & " SELECT Dates.Work_Dates, [Forms]![Resourcing]![Cmb_Duration] AS Expr1, [Forms]![Resourcing]![Cmb_Project_Title] AS Expr2, [Forms]![Resourcing]![Cmb_Trainer_Name] AS Expr3, [Forms]![Resourcing]![Cmb_Team] AS Expr4"
If Not IsNull(Me.Cmb_Activity) Then
strSQL = strSQL & " , [Forms]![Resourcing]![Cmb_Activity] AS Expr5"
End If
strSQL = strSQL & " , [Forms]![Resourcing]![Cmb_Training_Type] AS Expr6 "
strSQL = strSQL & " FROM Dates WHERE (((Dates.Work_Dates)>[Forms]![Resourcing]![Start_Date] And (Dates.Work_Dates)<=[Forms]![Resourcing]![End_Date]))"
DoCmd.RunSQL (strSQL)
End If
Activity = Activity & ": " & [Forms]![Resourcing]![Project_Title]
Me.Requery
DoCmd.GoToRecord , , acNewRec
If CurrentProject.AllForms("2014 Resources").IsLoaded Then
[Forms]![2014 Resources].Requery
If CurrentProject.AllForms("Edit Hours").IsLoaded Then
[Forms]![Edit Hours].Refresh
End If
End If
End Sub