Access DB: Preventing duplicates! arrrh

OK guys, this is my** last question** to you on Access. I’ve done plenty of research on this one and wrote a script , they only thing is that it only does half the job.

With some help from the internet, i coded up a script that performs a check on duplicate values.

My database has a Room Bookings form which consists of the room name, periods and booking date (BDate) fields. I need to prevent a duplicate on the same date, period and room.

The only problem with my code is that it only does a check on one of the fields e.g. BDate although i select a different period or room it still comes up with an error message saying that booking already exists? This means it is only checking the duplicate value of 1 field and not a combination of fields.

Here is my code:



Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strSearch As String
Dim varKey As Variant

strSearch = "BDate = #" & Me.BDate & "# And Period1 = """ & Me.Period1 & """And Period2 = """ & Me.Period2 & """And Period3 = """ & Me.Period3 & """And Period4 = """ & Me.Period4 & """And Period5 = """ & Me.Period5 & """And Period6 = """ & Me.Period6 & """And Lunch = """ & Me.Lunch & """And After_School = """ & Me.After_School & """"
varKey = DLookup("Booking_ID", "Furtherbookings", strSearch)
If Not IsNull(varKey) Then
If MsgBox("Booking already exists Booking ID: " & varKey & ". Do you wish to continue to create a new record?", vbYesNoCancel) = vbYes Then
Else
Cancel = True
Me.Undo
End If
End If


End Sub



Can any1 identify where i am going wrong?

I’d really appreciate any links to help/guidance/tutorials etc etc to resolve this problem!

Re: Access DB: Preventing duplicates! arrrh

Composite key? I’m not sure if you can create one in Access …

What’s period1 et al? sorry bro, have to spend some time on the code … something that I cant’ do at the moment.

Re: Access DB: Preventing duplicates! arrrh

I can use a composite key but wont work as data is coming from two tables?

I have a form which has a date, room and periods fields (periods 1-6) each which either contain an X if it isn’t booked and the period number if it is booked.

Re: Access DB: Preventing duplicates! arrrh

Do you have allergies to queries, or you like to give yourself the hardest time possible? which one is it chanda? Its so easy if you do it with query. I am confused here, I don’t u/s what you are doing really? your field names are not making any sense to me at all. I will solve it by query if you want me to.

I suggest, change the fields name right now, otherwise later on you wont u/s ur own database.

Re: Access DB: Preventing duplicates! arrrh

or .. you can create a query to check if the values entered by the user exist ... on the submit button, create a 'onMouseClick' event to execute the query. Hmmm ... but how would you check the result of the query? You'll have to use VB for that I guess.

Wait, you are taking input from the user and then checking if a duplicate exists? That will be quite irritating for the user ... consider the case where most of the rooms are booked. So if the user wants to find a vacant room, he'll have to go about guessing which room is available? So if there are a total of 10 rooms and 8 are booked then the user will enter the details 8 times (in a worst case scenario). That is a problem bro ... you need to rethink the design ..

I hope I didn't mess things up and got them right?

Re: Access DB: Preventing duplicates! arrrh

:rotfl: you are very cute :stuck_out_tongue:

Re: Access DB: Preventing duplicates! arrrh

^ okay … I wasn’t copying you PD .. I wasn’t … it was a coincidence that we were replying at around the same time :hoonh:

Re: Access DB: Preventing duplicates! arrrh

I know its possible with a simple search query but my end user prefers a msg box to appear when she types in a date and selects the periods and room that she requires to tell her that it is already booked.

Sol how is that possible with a query? :confused:

Re: Access DB: Preventing duplicates! arrrh

thats the way the end user prefers it i’m afraid.

Re: Access DB: Preventing duplicates! arrrh

omg yes its possible in query, how would it be possible, well create a query, then goto ur form and in events use upload events, so the moment form uploads it will ask you this question or show you how many rooms are available. Aik kaam kero, tell her to hire me :smiley: I wont charge her much but she would have to share her hotel with me :smiley: I should give everything for free :wink:

Re: Access DB: Preventing duplicates! arrrh

:hehe: i prefer working with queries than programming vb scripts and have no allergy wots so ever with them at all. :hehe:

Re: Access DB: Preventing duplicates! arrrh

Yeah I Know, your time shows 1:30 and mine is 1:28, but still I forgive you :smiley:

Re: Access DB: Preventing duplicates! arrrh

hmm … what can I say.

Oh yes .. tell her:

that’s bad design cause I say so … I AM THE SOFTWARE ENGINEE, NOT YOU!!

Re: Access DB: Preventing duplicates! arrrh

BTW it’s not a hotel, it’s a school computer centre. :slight_smile:

upload events? i’ll have to refer to my database…can u provide more detail on what you are trying to say here?

Re: Access DB: Preventing duplicates! arrrh

I dont know how u software engineers cope with all the stress of scripting…just creating a database has given me white hairs!

Just out of curiousity, Do software engineers do scripting from the top of their heads or do they reuse code and change it?

Re: Access DB: Preventing duplicates! arrrh

how hmm thats a very good question:

since you are not pro at this, I wont write down anything for you to copy it, I will give you lil home work. How about going in query section click on a wizard and look for duplicate query wizard and use your table and fields and try to manage it on ur own. This is just the beginning, hopefully u will make me proud :slight_smile:

Re: Access DB: Preventing duplicates! arrrh

I’ve done that! and thats quite straight forward, but it doesn’t help me fix the code that i’ve put together.

Re: Access DB: Preventing duplicates! arrrh

Hopefully u will make me proud :hehe: sounds like … ‘mom’ … seriously … :rotfl:

Re: Access DB: Preventing duplicates! arrrh

okay then computers are mine and she can have the room. Then why were you talking about the rooms if its not a hotel just a comp lounge, you are such dumbo.

First thing first, deal with the query, and I will help you create a search engine too :p, that lady whoever is asking you do stuff, she doesn’t know how database works, and thats what i hate in clients, they don’t know anything yet they think they know everything. I love ma boss, he just show me the stuff he wants me to do, we discuss it and then its mine, he has no say in it, unless he wants me to do few more changes :slight_smile:

Re: Access DB: Preventing duplicates! arrrh

okay now type your sql query here. I wanna see what you actually did in it?

Nos shut up, he is my student rite now :stuck_out_tongue: this is first in life I am teaching someone something, don’t ruin it :stuck_out_tongue: gareeb ke pait pe laat na maro yaar, yeh ghareeb mar jae gee:p