A quick DLookup question

NigelShaw

Registered User.
Local time
Today, 21:35
Joined
Jan 11, 2008
Messages
1,575
Hi,

just a quickie,

i need to lookup a date in a table (MonthlyTbl) which is set out like 28/07/2008

when the code starts, i need to get the current month only

strCurrDte = format(now(),"whatever the format is for the month")

i then need to check the date in the table against the current month but i dont know how to do it. i have tried-

strDteChkCurrDte = DLookup("ProcessDate"),"MonthlyTbl",strCurrDte)

but it doesnt seem to work. my thought was

Lookup the date in the process field in the monthlyTbl Table and check it against strCurrDte

i need this as i cannot make an entry in the table if there is one existing from the current period. am i nearly right with my syntax?


regs,

ns
 
I just spent all day playing with the domain aggregate functions. My work is at the office, otherwise I'd be able to give you an exact example. In the meantime, the syntax you need is either this...

strDteChkCurrDte = DLookup("ProcessDate","MonthlyTbl","[DateFieldGoesHere] = #" & strCurrDte & "#")

or this...

strDteChkCurrDte = DLookup("ProcessDate","MonthlyTbl","[DateFieldGoesHere] = " & strCurrDte)

Notes:
1. Removed the ")" from the "ProcessData" param.
2. Think of the criteria param. as an SQL statement after the "WHERE" portion; and it all goes in quotes, unless you need to jump out to stuff a variable in there, as I did with the && symbols.
 
Try:
Code:
= DLookup("ProcessDate","MonthlyTbl",DateDiff("m",date(),ProcessDate) = 0)
 
Hi guys,

i have tried both options but nothing seems to be working so i will try and give a greater detail with an example-

i need to check between 2 dates every month.
17th of previous month & 16th of current month

if the detail is completed, a record is entered with a date say 14th of current month.

then, someone else not looking properly also tries to complete the detail. i need to stop double entry data so,

i am trying to

1.check the current month
2.check that month against the table records MonthlyTbl
3. if there is a record in the table with the same month, exit otherwise, make the record.

current code ( though it changed a lot recently ! )

Public Sub CompleteMonth()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim intX As Integer
Dim strCurrPerd As String
Dim strChkCurrPerd As String
strCurrPerd = Format(Now(), "m")

'check for record in the table
strChkCurrPerd = Nz(DLookup("ProcessDate", "MonthlyTbl", strCurrPerd))

'if record is there, exit.
If strChkCurrPerd = strCurrPerd Then
MsgBox ("You have already completed this period!")
Exit Sub
Else

'if record is not there, check for current activity. if activity is pending, exit
intX = DCount("*", "FullPaymentPendingQry")
If intX <> 0 Then
MsgBox ("You already have Payments in the period." & vbCrLf & "You cannot register a completed month with Pending Payments"), vbOKOnly,
Exit Sub
Else

'otherwise process the completed month
MsgBox ("You have chosen to process a completed month for this period"), vbOKOnly,

'place the resposibility on the user
If MsgBox("Do you agree that you have completed ALL requirements for this period?", vbYesNo), = vbYes Then
MsgBox ("You will now complete the month in your records")vbOKOnly,

'create the record
Set db = CurrentDb
Set rs = db.OpenRecordset("MonthlyTbl", dbOpenDynaset)
rs.AddNew
rs!ProcessDate.Value = Date
rs!Completed.Value = True
rs.Update
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing

'advise completion
MsgBox (" your month is complete available for printing via reports"), vbOKOnly,
End If
End If
End If
End Sub

the difficulty is getting the current date to look up the table record. also, the between dates has just been added. i looked at a "between" scenario but havent quite figured that one out yet but working on it. something like-

'set the dd date
strStartDte = "17"

'set the dd date
strEndDte = "16"

'set current month
strCurrMnth = Format(Now(), "m")

'set previous month by month - 1 ( month???? but its probably wrong! )
strPrvMnth = Format(Now(), "m") - 1

'set current year
strCurrYr = Format(Now(), "yy")

'build Current month
strBldCurrMnth = strStartDte & "/" & strCurrMnth & "/" & strCurrYr

'build Previous Month
strBldPrvMnth = strEndDte & "/" & strPrvMnth & "/" & strCurrYr

'Build Period
strPeriod = Between strBldPrvMnth & strCurrMnth

like i say, its probably completely wrong and never likely to work but it gives me a starting point and hopefully, an insight to anyone who feels generous enough to offer advice.

in the meantime,

thanks for you help so far :)


Nigel
 
For starters, you NEVER want to turn dates into strings. Once you do that, they act like strings rather than dates. That means that 01/16/2008 is BEFORE 12/17/2007 rather than AFTER because 01 is less than 12 when doing a string comparison.

Dim BldCurDT as Date
Dim BldPrevDT as Date
BldPrevDT = CDate(Month(DateAdd("m", -1, Date())) & "/17/" & Year(DateAdd("m",-1,Date())
this subtracts 1 month from the current date and extracts the month part and uses the same technique to extract the year since subtracting 1 from the current month will result in a year change when going from January to December. It then concatenates 17 as the day part of the date to get the 17th of the previous month.

BldCurDT = CDate(Month(Date()) & "/16/ & Year(Date())
This just extracts the month and year from the current date and concatenates them with 16 to find the current date.
 
Last edited:
Dim BldCurDT as Date
Dim BldPrevDT as Date
BldPrevDT = CDate(Month(DateAdd("m", -1, Date())) & "/17/" & Year(DateAdd("m",-1,Date())
this subtracts 1 month from the current date and extracts the month part and uses the same technique to extract the year since subtracting 1 from the current month will result in a year change when going from January to December. It then concatenates 17 as the day part of the date to get the 17th of the previous month.

BldCurDT = CDate(Month(Date()) & "/16/ & Year(Date())
This just extracts the month and year from the current date and concatenates them with 16 to find the current date.

Hi Pat,

im being a bit thick here today! how would i integrate this with my above code? is it

my above code should check the table for a date entry to match which i know is not right.
your code will check the difference between 2 dates.
i need to look at the current date as date of action, and check the date entry to see if it falls in the period 17th to the following 16th but im unsure of the syntax.

any suggestions?


many thanks,

Nigel
 
Hi,

Ok, so would i be able to check this using Between? for example-

Dim BldCurDT as Date
Dim BldPrevDT as Date

BldPrevDT = CDate(Month(DateAdd("m", -1, Date())) & "/17/" & Year(DateAdd("m",-1,Date())))
BldCurDT = CDate(Month(Date()) & "/16/ & Year(Date())

DteCurrPerd = Between BldPrevDT & BldCurDt

IF Nz(DLookup("ProcessDate", "MonthlyTbl", DteCurrPerd)) Then
MsgBox("Already Completed")
Exit Sub
Else

ProcessPeriod

End If


NS
 
Yes except you need to change this line -

DteCurrPerd = Between BldPrevDT & BldCurDt

to

DteCurrPerd = "Between #" & BldPrevDT & "# AND #" & BldCurDt & "#"
 
Hi Pat,

i keep getting an error on this line

BldCurDT = CDate(Month(Date()) & "/16/ & Year(Date())"

the error is ")" is missing but where ever i put it, i get an error. could you advise at all?

many thanks,

Nigel
 
Aren't you missing a quote after /16/? ...And remove the one at the end of the line. See below.

PHP:
BldCurDT = CDate(Month(Date()) & "/16/" & Year(Date())
 
Hi asather,

thanks for that. i will check this out now and return the result. Also,
Thanks Pat for the original advice, i dont think i would have got those hashes!!

many thanks on this guys

Nigel
 
Hi,

ok, i figured this out by placing the code in the immediate window

CDate(Month(DateAdd("m", -1, Date)) & "/5/" & Year(DateAdd("m", -1, Date)))
gives back a return value of 07/05/2008

CDate (Month(Date) &"/4/" & Year(Date))
gives back a value of 08/04/2008

i am getting an error with This-

DteCurrPerd = "Between #" & BldPrevDT & "# AND #" & BldCurDT & "#"

Also one final thing, how would i change the date format from
mm/dd/yyyy to dd/mm/yyyy

many thanks,


Nigel
 
Would this work?

BldPrevDT = DateSerial(Year(Date), Month(Date) -1, 5)
BldCurDT = DateSerial(Year(Date), Month(Date), 4)

DteCurrPerd = "Between #" & BldPrevDT & "# AND #" & BldCurDT "#"

If Nz(DLookup("ProcessDate", "MonthlyTbl", DteCurrPerd)) Then
MsgBox(" You have already processed this month")
Exit Sub
Else

'code to complete the processed month here

End If


in the immediate window, it works but does it take care of the end of year like Pats code?

regs,

Nigel
 
Substitute hard-coded dates and test in the immediate window.

Access uses the PC's regional settings to determine the default date format. The only time you need to worry about the format when working with dates is when feeding a date to SQL. SQL only recognizes strings in US date format. As long as you stick to working only with date/time fields rather than strings, you won't have any problem with dates. It is only when you convert the date to a string in code before trying to work with it that you run into issues. For display purposes, Access should default to whatever your regional settings specify. Alternatively, you can set the format at the control level on a form.

Remember - it doesn't matter how a date is formatted for display - internally, dates are stored as double precision numbers. That's why date arithmetic is so easy.
 
Hi Pat,

thanks for that. on the point of the two types of code, yours would be better as it considers the Dec / Jan as commented by you whereas the other code i suspect doesnt as it formats the current month and the previous month - 1 thus only takintg into account the previous month not a previous year to... ( vv confusing to say the least! phew )

in respect of the error, your "Between" section reads correct and i can see it is a build up to create a full string but for some reason, there is an error. im not sure what it is because the code is issued from a Ribbon so issues a macro first to start the function. i only get a macro error not a vba one.

could it be at all the varialbe i am setting the between code to? would this need to be a variant to store more data as it is curently storred as a date?

everything reads right but something doesnt work.

regs,

Nigel
 
Last edited:
OK, Getting better :)

Hi,

getting there slowly. still have problems but no errors:)

Code-

Public Sub CompletePeriod()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim intX As Integer
Dim strCurrPerd As String
Dim strChkCurrPerd As String
Dim BldCurDT As Date
Dim BldPrevDT As Date
Dim DteCurrPerd As Date

BldPrevDT = CDate(Month(DateAdd("m", -1, Date)) & "/5/" & Year(DateAdd("m", -1, Date)))

BldCurDT = CDate(Month(Date) & "/4/" & Year(Date))

DteCurrPerd = Nz(DLookup("ProcessDate", "MonthlyTbl"))

If DteCurrPerd = "Between #" & BldCurDT & "# AND #" & BldPrevDT & "#" Then

MsgBox ("You have already completed this period!")
Exit Sub
Else
MsgBox ("You are ready to complete this period")
End If
Exit Sub

Something is still not right though and i think it is the way the Dlookup is working. i am trying to get the code to check ProcessedDate with DteCurrPerd. if the date is in the period, msgbox period completed. if the date is NOT in the period, msgbox complete period.

nothing is working and i have changed this around so many times now.



Regs,

nigel
 
Ok,

i think the problem is the dlookup. i dont seem to have the correct syntax or i might have a parametre missing though i only have 3 fields in the table-
ProcessID (PK, Autonumber)
ProcessDate(Date)
Completed (Yes/No)

i have altered my syntax to include the full string instead of setting the variable-

If DLookup("processDate", "MonthlyTbl", "Between #" & CDate(Month(DateAdd("m", -1, Date)) & "/5/" & Year(DateAdd("m", -1, Date))) & "# AND #" & CDate(Month(Date) & "/4/" & Year(Date)) & "#") Then

MsgBox ("Period Complete")
Exit Sub
Else
MsgBox("Period Not Complete")
Exit Sub
End IF

could someone just advise me on the correct syntax for my DLookup as i think its wrong. This is where my problem is as i keep getting errors on this line.

Regards,

Nigel
 
am I missing something in the dlookup? I thought you could lookup "field", "table", criteria which is what I'm doing.

If date from table = between date1 and date2 then etc

Is there something wrong here as I can't see it

Thanks

Nigel
 
So what am i doing wrong?

Hi,

am i asking this in wrong manner? i am not asking anyone to specifically do this for me, just offer a little advice. at least i am offering suggestions and attempts back for the good of the forum if they are of any use which i think is more than the "Help Me!!!!!!!!!" people!

i have tried every which way to get this working but i just cant get it right. if i use SQL , isnt this changing it to a string thus dates wont calculate?
i have tried adding the variables to the DLookup but somehow, its not working and i get errors.
i have tried adding the complete string to the dlookup but i get errors on the first #
if i un quote the Between, i get errors
if fact, whatever i do i get errors.

i have change the Dim Variable As Date to Variant and back agai but nothing. i have searched the forum and google but this topic seems non existant

my books dont explain between dates but i would have thought it was sometihng Access could handle quite easily.
i have tried this with a query but when i use CDate, the query errors...

Cant anyone offer a little guidance, point in direction of? i can figure it out if knew which way to go.


Regs,

Nigel

ps ( sorry for the rant:( )
 
Don't have a specific answer other than to try the simplified route first, go back to the query route and play around with the criteria, also I don't think DLookups handle no records very well
 

Users who are viewing this thread

Back
Top Bottom