Avatar billede kimsand Nybegynder
16. juli 2014 - 12:35 Der er 18 kommentarer og
1 løsning

Probleme med Dcount

Hej igen eksperten....

Jeg har et problem med Dcount, hvor jeg har noget kode der skal checke om et lokale kan bookes på i et valgt tidsrum.

Hvis den finder en record der opfylder de indtastede kriterier, så er lokalet booket indenfor tidsrummet, og kan derfor ikke bookes igen.

Der er flere dcounts, som skal lede på forskellige kriterier, for at imødekomme at man laver forskellige kombinationer af start og slut tid for bookingen. Jeg er ikke færdig med alle de forskellige "Where" kriterier endnu.

Problemet er at selvom jeg bruger kriterier der burde udløse en record, så sker det ikke. Jeg har checket ved at bruge kriterierne i en forespørgsel, og den rigtige record kommer op. Kan nogen af jer se hvad der er forkert i min Dcount ?


Her er koden:


Dim frakl As Date, tilkl As Date, dato As Date

frakl = [Forms]![FM_book_lokale]![tidfra]
tilkl = [Forms]![FM_book_lokale]![tidtil]
dato = [Forms]![FM_book_lokale]![dato]

where1 = "[DT_rum].[IDrum]= " & [Forms]![FM_book_lokale]![IDrum] & " AND [DT_tilbudskalender].[dato] = #" & dato & "# AND [DT_tilbudskalender].[starttid] <= #" & frakl & "# AND [DT_tilbudskalender].[sluttid] >= #" & tilkl & "#"
where2 = "[DT_rum].[IDrum]= " & [Forms]![FM_book_lokale]![IDrum] & " AND [DT_tilbudskalender].[dato] = #" & dato & "# AND [DT_tilbudskalender].[starttid] > #" & frakl & "# AND [DT_tilbudskalender].[sluttid] < #" & tilkl & "#"

If DCount("*", "F_rum_paa_dato_og_tid", where1) > 0 Or DCount("*", "F_rum_paa_dato_og_tid", where2) > 0 Then
    P = MsgBox("Rummet er booket i det angivne tidsrum", vbOKOnly)
    Exit Sub
Else
    'her skal der være en update forespørgsel som opretter bookingen med IDrum, dato, tidfra, tidtil, tilbudstype(mødebooking) og IDmedarbejder
    P = MsgBox("rummet kan bookes i det angivne tidsrum", vbOKOnly)
End If
Avatar billede kimsand Nybegynder
16. juli 2014 - 13:05 #1
Når jeg debugger, kan jeg se

where1 : "[DT_rum].[IDrum]= 1 AND [DT_tilbudskalender].[dato] = #14-07-2014# AND [DT_tilbudskalender].[starttid] <= #10:00:00# AND [DT_tilbudskalender].[sluttid] >= #11:00:00# 

where2 : "[DT_rum].[IDrum]= 1 AND [DT_tilbudskalender].[dato] = #14-07-2014# AND [DT_tilbudskalender].[starttid] > #10:00:00# AND [DT_tilbudskalender].[sluttid] < #11:00:00#             

Så det mener jeg jo er rigtigt, alle kriterierne bliver sat ind de rigtige steder.
Avatar billede terry Ekspert
16. juli 2014 - 13:26 #2
Try formatting your dates either
YYYY/MM/DD or MM/DD/YYYY
Avatar billede terry Ekspert
16. juli 2014 - 13:34 #3
"Jeg har checket ved at bruge kriterierne i en forespørgsel,"

Try making a query and include the where clause you show above

What is the result?
Avatar billede terry Ekspert
16. juli 2014 - 13:42 #4
You could also try using DateValue to ensure that you are only comparing a date and not a time too


AND DateValue([DT_tilbudskalender].[dato]) = #2014/07/14#
Avatar billede kimsand Nybegynder
16. juli 2014 - 14:32 #5
Hey Terry.... TY for your reply :)

"Try making a query and include the where clause you show above

What is the result? "

I did that, and it gave me the exact record as it was supposed to :)

I tried formatting the dates to mm/dd/yyyy, but the first date it didnt format, but the second it did... That was probably the most strange thing ive experienced in VBA ever, cause it was 2 unformatted textboxes in a form, that i took the values from in the same way, and put them into 2 variables that was DATE types... really weird.

Im gonna try the datevalue thing you wrote right now :)
Avatar billede kimsand Nybegynder
16. juli 2014 - 14:33 #6
is there a timevalue() too ?
Avatar billede kimsand Nybegynder
16. juli 2014 - 14:36 #7
oh there was :)

I just tried the datevalue and timevalue and it didnt work.
Avatar billede kimsand Nybegynder
16. juli 2014 - 14:51 #8
when i format dato2=format(dato,"mm-dd-yyyy") it doesnt change anything.

But when i choose format(dato,"mm-dd-yyyy") as a watch, it shows the date in the right order with month first..

really weird.
Avatar billede kimsand Nybegynder
16. juli 2014 - 15:03 #9
When i run this SQL on the DB it gives me 1 record which is correct

SELECT DT_rum.IDrum, DT_tilbudskalender.dato, DT_tilbudskalender.starttid, DT_tilbudskalender.sluttid

FROM DT_rum INNER JOIN DT_tilbudskalender ON DT_rum.IDrum = DT_tilbudskalender.IDrum

WHERE (((DT_rum.IDrum)=1) AND ((DT_tilbudskalender.dato)=DateValue(#7/14/2014#)) AND ((DT_tilbudskalender.starttid)<=TimeValue(#12/30/1899 10:0:0#)) AND ((DT_tilbudskalender.sluttid)>=TimeValue(#12/30/1899 11:0:0#)));

and it works even if i do the date as 14/7/2014
Avatar billede terry Ekspert
16. juli 2014 - 15:20 #10
is it possible for me to see the dB?

ekspertenATsanthell.dk
AT = @
Avatar billede terry Ekspert
16. juli 2014 - 15:32 #11
In the SQL you gave 15:03:11| #9

You are selecting on two tables with INNER JOIN, but in the Dlookup you  select from F_rum_paa_dato_og_tid which I guess is a query.

So you should try making anew query like this

SELECT count(*) FROM F_rum_paa_dato_og_tid
WHERE (((DT_rum.IDrum)=1) AND ((DT_tilbudskalender.dato)=DateValue(#7/14/2014#)) AND ((DT_tilbudskalender.starttid)<=TimeValue(#12/30/1899 10:0:0#)) AND ((DT_tilbudskalender.sluttid)>=TimeValue(#12/30/1899 11:0:0#)));
Avatar billede kimsand Nybegynder
16. juli 2014 - 15:59 #12
hmm now it sometimes works :)

I deleted the query, and created a new with the same name and kriterias...

i think i have to go home now :)
Avatar billede kimsand Nybegynder
16. juli 2014 - 16:33 #13
the query at #9 where ive tried the kriterias IS the F_rum_paa_dato_og_tid query...

I cant send the DB, unfortunately, it contains sensitive data, but thankyou for the offer !

im giving up for today..

basically im asking the DB

if there is a record where an entered starttime is between or equal to a starttime AND endtime in the db

if there is a record where an entered endtime is between or equal to a startime AND endtime in the db

if there is a record where and entered starttime is before or equal to a starttime in the db AND and entered endtime is equal to or after an endtime in the DB.

when i ask that in the query with entered criterias, it gives me the right result, but its like it doesnt work with Dcount....

I have to quit for today... my head is turning the wrong way.

TY Terry for your effort, ill be giving it a try again tomorrow.
Avatar billede terry Ekspert
16. juli 2014 - 19:26 #14
"hmm now it sometimes works :) "

Now how can that be? I would expect it to work or not work, unless something else is have influence on the result, but I don't see what that could be.

Is it possible to send a db with the tables and queries you have, and juts have some test data?

Its going to be a little difficult to see what the problem is otherwise. I only have until tomorrow evening then I'm going to be away for a while.
Avatar billede kimsand Nybegynder
16. juli 2014 - 21:00 #15
Hey Terry, it is so nice of you to offer that....

I will make a test DB, for future situations, but i cant do it now, as i have to much work before i go on holiday friday...

If you are going on a holiday too, then have a nice trip.. :)

Sincerely Kim
Avatar billede kimsand Nybegynder
16. juli 2014 - 21:01 #16
and yeah... im very puzzeled too what is the matter....

I seem to have problems when disecting the logic behind the queries sometimes, so its probably me that is doing something very strange...
Avatar billede terry Ekspert
16. juli 2014 - 21:18 #17
Yes I'm also going on holiday Friday :-)
You have a good one too.

BR
Terry
Avatar billede kimsand Nybegynder
17. juli 2014 - 08:54 #18
Oh maan..

This is embarrasing......

In my query i already had set up criteria which was taken from the form...

ONTOP OF THAT

I set criteria in the Dcount.....

The result was....

i did Dcount on a recordset which had been selected on criteria, which made it a mighty SMALL recordset... sometimes 1 record sometimes 0 :)

IM SORRY to waste your effort like this Terry.. I am a newb, and doing stupid mistakes all the time..

Sincerely Kim

throw an answer, and ill send the points your way !!!
Avatar billede terry Ekspert
17. juli 2014 - 19:05 #19
No problem Kim, we all make mistakes at some stage its part of the learning process.

Here comes an answer, have  a good holiday.

BR
Terry
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester