Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Forms / November 2007

Tip: Looking for answers? Try searching our database.

Checking for duplicate entries in subform

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
omar.norton@gmail.com - 02 Nov 2007 15:03 GMT
Hi all,

I've been trying to approach this problem several ways for some time
now and to no avail.

I am making a database that generates staff rotas. There is a button
you can click on the main rota form to bring up an "edit staff" form
(frmEditStaff) that includes a subform with a list of holidays that
the staff member has booked off in advance. The subform gets it's
data
from a table called "tblBookedHols" - which contains a unique ID
autonumber (the primary key - which isn't really used), a field
containing the staff member's name ("FullName"), a field for a
description of the holiday ([Description] - which may be left null)
and a field containing the holiday date ("HolDate").

When you enter the holiday date in the subform, the staff member's
name is automatically inserted into a hidden textbox that updates the
database.

I am trying to set it up so that if you type in a duplicate holiday
date for any one staff member then a message box will pop up
explaining this and delete the duplicate entry. I tried doing this
with a DLookup in the AfterUpdate event - which usually came up with
the "you cancelled the previous action error" so I tried using
recordsets instead (which I didn't want to do as I don't really
understand them!). I managed to come up with the following code but
this doesn't seem to do anything - no message boxes, no errors, no
nothing... I've also tried putting the code in the BeforeUpdate,
AfterUpdate, BeforeInsert and AfterInsert events which doesn't seem
to
make any difference.

As well as not getting the message box I am not sure how to program
it
to delete the new duplicate record. The code (which is for the
subform) is typed below:

Option Compare Database

Private Sub txtHolDate_AfterUpdate()

Me.txtFullName = Forms!frmEditStaff![FullName]

Dim rs As ADODB.Recordset
Dim sSQL As String

Set rs = New ADODB.Recordset

sSQL = "SELECT * FROM tblBookedHols WHERE [HolDate] = " &
Me.txtHolDate & _
      " AND [FullName] = '" & Me.txtFullName & "'"
rs.Open sSQL, CurrentProject.Connection, adOpenStatic

If rs.RecordCount = 0 Then
'if recordcount = 0 there are no duplicates
Else
'else there are records with same data and an alert is given
MsgBox "This holiday has already been booked for " & Me.txtFullName,
vbOKOnly, "Duplicate Enrty"

'Here is where the code would go to delete the duplicate record

End If
rs.Close
Set rs = Nothing

End Sub

Thank you in advance!
benyod79 - 02 Nov 2007 15:53 GMT
In your tblBookedHols, make FullName and HolDate the PrimaryKey. This
combination will not allow the same FullName and HolDate to be entered twice.

To make a multi-field PrimaryKey, hold the shift key down while selecting the
fields, then right-click and choose PrimaryKey.

If this doesn't fit your needs, let me know as there are other ways around
this too.

>Hi all,
>
[quoted text clipped - 66 lines]
>
>Thank you in advance!
omar.norton@gmail.com - 02 Nov 2007 16:03 GMT
> In your tblBookedHols, make FullName and HolDate the PrimaryKey. This
> combination will not allow the same FullName and HolDate to be entered twice.
[quoted text clipped - 81 lines]
>
> - Show quoted text -

That did it - thanks!
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.