AccessAllAreas
Member
- Local time
- Today, 05:36
- Joined
- Jul 20, 2020
- Messages
- 42
Hello Experts
Firstly, I have used the answers given on this site to answer many questions I have had already. So a big thank you for all help already given.
I have trued to fund answers to this already and have tried two or three different solutions, adapted to fit my needs, but with no joy! Hence, as I have been trying to learn VBA for access, I thought it best to ask a question rather than stumble blindly on!
My Goal: On a bound popup form, I wish to avoid any records being recorded which are duplicate to any single record already saved in the table that has an identical date to the one now trying to be entered.
The table:
Four fields in the table:
ID (Autonumber) (Primary Key)
Type (Number)
Date (Date/Time) (Format - dd/mm/yyyy) (Required - Yes) (Validation Rule - <= Now()
Attendance
The form:
Main form with a command button that opens a popup form.
There are three controls: Type (combo box), Date (with date selector), formatted as dd/mm/yyyy, and Attendance (text box, accepting only integer values).
Below is the VBA code that I have currently. This has been mashed together after having tried at least two different solutions. I have tried to get the correct syntax, but believe I am missing something fundemental now.
The issue is in the second block of code, starting NewMeetingAttendanceDateRecord.
As you can see from the REM'D out Dim statements at the top of the code, I have tried different variable types (string, Date, and Variant).
I have tried running through th code step by step, using the locals window. But no matter which way I go, I get one of a variety of runtime errors: 13 (type mismatch), 3075, 3045, 2645.
I am not very experienced with VBA. Hence, this plea to the experts!
Thank you in advance for your time and wisdom.
Firstly, I have used the answers given on this site to answer many questions I have had already. So a big thank you for all help already given.
I have trued to fund answers to this already and have tried two or three different solutions, adapted to fit my needs, but with no joy! Hence, as I have been trying to learn VBA for access, I thought it best to ask a question rather than stumble blindly on!
My Goal: On a bound popup form, I wish to avoid any records being recorded which are duplicate to any single record already saved in the table that has an identical date to the one now trying to be entered.
The table:
Four fields in the table:
ID (Autonumber) (Primary Key)
Type (Number)
Date (Date/Time) (Format - dd/mm/yyyy) (Required - Yes) (Validation Rule - <= Now()
Attendance
The form:
Main form with a command button that opens a popup form.
There are three controls: Type (combo box), Date (with date selector), formatted as dd/mm/yyyy, and Attendance (text box, accepting only integer values).
Below is the VBA code that I have currently. This has been mashed together after having tried at least two different solutions. I have tried to get the correct syntax, but believe I am missing something fundemental now.
Code:
Private Sub MeetingDate_BeforeUpdate(Cancel As Integer)
' Note: MeetingType 1 = Management; MeetingType 2 = Staff
' Note: 1 = Monday, 2 = Tuesday, 3 = Wednesday, 4 = Thursday, 5 = Friday, 6 = Saturday, 7 = Sunday
Dim NewMeetingAttendanceDateRecord As Date
' Dim strNewMeetingAttendanceDateCriteria As String
' Dim strNewMeetingAttendanceDateCriteria As Date
Dim strNewMeetingAttendanceDateCriteria As Variant
Dim iWeekNo As Integer
iWeekNo = Weekday([MeetingDate], vbMonday)
If Me.MeetingType = 1 And (iWeekNo = 6 Or iWeekNo = 7) Then
MsgBox "Management Meetings occur between Monday and Friday." _
& vbCrLf & "The date you have entered corresponds to either a Saturday or a Sunday." _
& vbCrLf & "Please alter your date to one equivalent to a Monday through Friday.", vbInformation, _
"Check Your Meeting Type and Date"
Cancel = True
Me.MeetingDate.Undo
Exit Sub
Else: Me.MeetingAttendance.Enabled = True
End If
If Me.MeetingType = 2 And (iWeekNo = 1 Or iWeekNo = 2 Or iWeekNo = 3 Or iWeekNo = 4 Or iWeekNo = 5) Then
MsgBox "Staff Meetings occur at the weekend." _
& vbCrLf & "The date you have entered corresponds to a weekday." _
& vbCrLf & "Please alter your date to one equivalent to either a Saturday or a Sunday.", vbInformation, _
"Check Your Meeting Type and Date"
Cancel = True
Me.MeetingDate.Undo
Exit Sub
Else: Me.MeetingAttendance.Enabled = True
End If
NewMeetingAttendanceDateRecord = Me.MeetingDate.Value
strNewMeetingAttendanceDateCriteria = "[MeetingDate] = #" & NewMeetingAttendanceDateRecord & "# "
' strNewMeetingAttendanceDateCriteria = "[MeetingDate] = # & NewMeetingAttendanceDateRecord & #"
If Me.MeetingDate = DLookup("[MeetingDate]", "Tab_Meeting_Attendance", "([MeetingDate] = #NewMeetingAttendanceDateRecord#)") Then
MsgBox "This meeting date, " & NewMeetingAttendanceDateRecord & ", has already been entered into the database." & vbCrLf & vbCrLf & _
"Please check selected date again." & vbCrLf & vbCrLf & _
"If you are certain it is correct, pleace check existing reports to search for duplicate entry.", _
vbInformation, "Duplicate Meeting Date Found"
Cancel = True
Me.MeetingDate.Undo
Exit Sub
Else: Me.MeetingAttendance.Enabled = True
End If
End Sub
The issue is in the second block of code, starting NewMeetingAttendanceDateRecord.
As you can see from the REM'D out Dim statements at the top of the code, I have tried different variable types (string, Date, and Variant).
I have tried running through th code step by step, using the locals window. But no matter which way I go, I get one of a variety of runtime errors: 13 (type mismatch), 3075, 3045, 2645.
I am not very experienced with VBA. Hence, this plea to the experts!
Thank you in advance for your time and wisdom.