Avatar of SteveL13
SteveL13Flag for United States of America

asked on 

How to prevent data entry in a sub-form where Date, Time, and Doctor already exists

I'm trying to figure out how to prevent data entry in a sub-form where Date, Time, and Doctor already exists.

The user tries to select a Doctor from a combobox and if that combination already exists give the user a warning message that they have already scheduled that doctor for that date and time.

The field names are Date, Time, and Doctor.  The table the subform is bound to is tblPotenialAppointmentDatesAndTimes.  The field names in the table are AppointmentDate, AppointmentTime, and DoctorName.  Can someone help?
Microsoft Access

Avatar of undefined
Last Comment
PatHartman
Avatar of John Tsioumpris
John Tsioumpris
Flag of Greece image

In the BeforeUpdate event of the ComboBox you just pickup the value selected (along with the other Columns) and if matches the existing values simply Cancel =True.
Avatar of SteveL13
SteveL13
Flag of United States of America image

ASKER

I don't know how to "In the BeforeUpdate event of the ComboBox you just pickup the value selected (along with the other Columns) and if matches the existing values simply Cancel =True."
Avatar of John Tsioumpris
John Tsioumpris
Flag of Greece image

Private Sub YouCombo_BeforeUpdate(Cancel As Integer)
if yourcombo= me.doctorid then
Cancel=true
else
end if
End Sub

Open in new window

As for the point you can ask for help from mods
Avatar of SteveL13
SteveL13
Flag of United States of America image

ASKER

Is not working.  Here is what I have:

Private Sub Doctor_BeforeUpdate(Cancel As Integer)

    If Doctor = Me.DoctorName Then
        Cancel = True
    Else
    End If

End Sub

Open in new window

Avatar of John Tsioumpris
John Tsioumpris
Flag of Greece image

Is "working"  but probably you are comparing different fields...
The combo probably on the bound column holds the ID of the doctor  (e.g 7 -->Steve) and on the textbox it has the name  (Steve)
Probably on the 2nd Column you hold the Doctor's name so you can test the equality like this :
if Doctor.Column(1) = Me.DoctorName Then

Open in new window

Avatar of SteveL13
SteveL13
Flag of United States of America image

ASKER

Tried that.  Still doesn't work.  When I select a doctor I can't leave the field and I get no warning that the doctor is already scheduled for that date and time.
Avatar of John Tsioumpris
John Tsioumpris
Flag of Greece image

Now you got me confused:
Just put a Msgbox to indicate that you actually picked the doctor and is a match...then you can clear the selection and force the user to select again...
Avatar of SteveL13
SteveL13
Flag of United States of America image

ASKER

Tried that with...

    If Doctor.Column(1) = Me.DoctorName Then
        MsgBox "The doctor you have chosen already has a scheduled event in this time slot."
        Me.Doctor = ""
        Cancel = True
    Else
    End If

Open in new window


And no matter what doctor I choose, the form is locked until I clear the selected doctor.
Avatar of John Tsioumpris
John Tsioumpris
Flag of Greece image

Check if this helps
If Doctor.Column(1) = Me.DoctorName Then
        MsgBox "The doctor you have chosen already has a scheduled event in this time slot."        
        Cancel = True
        Me.Doctor.Undo
    Else
    End If

Open in new window

Avatar of SteveL13
SteveL13
Flag of United States of America image

ASKER

The field does clear now when I try to select a doctor that is already scheduled for the time slot.  But if I select another doctor that hasn't been scheduled for the time slot I get the same msgbox.
Avatar of John Tsioumpris
John Tsioumpris
Flag of Greece image

Something must slipped in your code....the msgbox should pop only if you have equality
Avatar of PatHartman
PatHartman
Flag of United States of America image

When you have a combination of three fields that determine existence, you can't use the CONTROL level events to do the validation since all three values are not available when the events for the first control run so your results will be incorrect.  The validation must be done in the FORM's BeforeUpdate event.

You can use a dCount() to find the existing appointment.

Private Sub Form_BeforeUpdate(Cancel As Integer)

Dim strWHERE As String
    If Me.txtAppointmentDate & "" = "" Then
        MsgBox "Appointment Date is required.", vbOKOnly
        Cancel = True
        Me.txtAppointmentDate.SetFocus
        Exit Sub
    End If
    If Me.txtAppointmentTime & "" = "" Then
        MsgBox "Appointment Time is required.", vbOKOnly
        Cancel = True
        Me.txtAppointmentTime.SetFocus
        Exit Sub
    End If
    If Me.cboDoctorName & "" = "" Then
        MsgBox "Doctor Name is required.", vbOKOnly
        Cancel = True
        Me.cboDoctorName.SetFocus
        Exit Sub
    End If
    strWHERE = "AppointmentDate = #" & Me.txtAppointmentDate & "# AND AppointmentTime = #" & Me.txtAppointmentTime & "#" And DoctorName = " & Me.cboDoctorName"
    If DCount("*", "tblPotenialAppointmentDate", strWHERE) > 0 Then
        MsgBox "This doctor is already booked for this time slot.  Please choose a different doctor or a different time/date.", vbOKOnly
        Cancel = True
        Me.cboDoctorName.SetFocus
        Exit Sub
    End If
    
End Sub

Open in new window

Avatar of SteveL13
SteveL13
Flag of United States of America image

ASKER

Pat: I'm getting an error:  "Application-defined or object-defined error".  Am using:

Dim strWHERE As String
    If Me.Date & "" = "" Then
        MsgBox "Appointment Date is required.", vbOKOnly
        Cancel = True
        Me.Date.SetFocus
        Exit Sub
    End If
    If Me.Time & "" = "" Then
        MsgBox "Appointment Time is required.", vbOKOnly
        Cancel = True
        Me.Time.SetFocus
        Exit Sub
    End If
    If Me.Doctor & "" = "" Then
        MsgBox "Doctor Name is required.", vbOKOnly
        Cancel = True
        Me.Doctor.SetFocus
        Exit Sub
    End If
    strWHERE = "AppointmentDate = #" & Forms!frmScheduleHeader.Form.Date & "# AND Forms!frmScheduleHeader.Form.Time = #" & Forms!frmScheduleHeader.Form.Time & "#" And DoctorName = " & Me.cboDoctorName"
    If DCount("*", "tblPotenialAppointmentDate", strWHERE) > 0 Then
        MsgBox "This doctor is already booked for this time slot.  Please choose a different doctor or a different time/date.", vbOKOnly
        Cancel = True
        Forms!frmScheduleHeader.Form.DoctorName.SetFocus
        Exit Sub
    End If

Open in new window

Avatar of PatHartman
PatHartman
Flag of United States of America image

Sorry Steve, I am not a compiler.  What line of code is causing the error?
Avatar of John Tsioumpris
John Tsioumpris
Flag of Greece image

Simpler

    If len(Me.Date) =0 Then

Open in new window

similar for the others
Avatar of SteveL13
SteveL13
Flag of United States of America image

ASKER

Pat:  strWHERE = "AppointmentDate = #" & Forms!frmScheduleHeader.Form.Date & "# AND Forms!frmScheduleHeader.Form.Time = #" & Forms!frmScheduleHeader.Form.Time & "#" And DoctorName = " & Me.cboDoctorName"
ASKER CERTIFIED SOLUTION
Avatar of PatHartman
PatHartman
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Microsoft Access
Microsoft Access

Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.

226K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo