Problem with DLookup

  • Follow


Hi there,

I have a table which has the following structure from which I want to 
extract some information:

--------------------------------------------------------------------
| LGID | LeaveGroupStYr | LeaveGroupEndYr | LeaveDays |
--------------------------------------------------------------------
|    1  |         0               |              2           |      10       |
|    2  |         2               |              5           |      12       |
|    3  |         5               |             99          |      16       |
-------------------------------------------------------------------

I am coding for a command button to get the LeaveDays if the ServiceYears 
falls between the LeaveGroupStYr and LeaveGroupStYr

Private Sub cmdCalculateLeave_Click()

Dim ServiceYears As Double

    ServiceYears = (Date - Forms![Leave Entry Form]!DateHired) / 365.25
    Me!LeaveEntitlement = DLookup("LeaveDays", "Leave Group", _
             "LeaveGroupStYr >= " & ServiceYears & _
             " AND LeaveGroupEndYr < " & ServiceYears)
   
End Sub

I tested out that if I have:
DLookup("LeaveDays", "Leave Group", "LeaveGroupStYr >= " & ServiceYears & _
             " AND LeaveGroupEndYr < " & ServiceYears)
Result: NULL.

However, if either of the following, i get results, in terms of numeric value:
DLookup("LeaveDays", "Leave Group", "LeaveGroupStYr >= " & ServiceYears)
Result: 16
DLookup("LeaveDays", "Leave Group", "LeaveGroupEndYr < " & ServiceYears)
Result: 10

Can anyone help me as I can't get the record I want.
0
Reply Utf 11/1/2007 4:47:03 AM

Hi Kelvin,

To calculate date differences, you should use the DateDiff() function.

The reason you weren't getting the result you were looking for is that the 
gT and LT signs are reversed.

Try this:

'------------------------------------
Private Sub cmdCalculateLeave_Click()
    Dim ServiceYears As Double
    Dim sCriteria As String

    ServiceYears = DateDiff("yyyy", CDate(Me.DateHired), Date)
    sCriteria = "LeaveGroupStYr <= " & ServiceYears & " AND LeaveGroupEndYr 
> " & ServiceYears

    '    Me.Service = ServiceYears    ' test control to see years diff

    Me!LeaveEntitlement = DLookup("LeaveDays", "Leave Group", sCriteria)
End Sub
'------------------------------------

I used a variable for the criteria so I could see the criteria string when 
stepping thru the code.


HTH
-- 
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


"Kelvin Leong" wrote:

> Hi there,
> 
> I have a table which has the following structure from which I want to 
> extract some information:
> 
> --------------------------------------------------------------------
> | LGID | LeaveGroupStYr | LeaveGroupEndYr | LeaveDays |
> --------------------------------------------------------------------
> |    1  |         0               |              2           |      10       |
> |    2  |         2               |              5           |      12       |
> |    3  |         5               |             99          |      16       |
> -------------------------------------------------------------------
> 
> I am coding for a command button to get the LeaveDays if the ServiceYears 
> falls between the LeaveGroupStYr and LeaveGroupStYr
> 
> Private Sub cmdCalculateLeave_Click()
> 
> Dim ServiceYears As Double
> 
>     ServiceYears = (Date - Forms![Leave Entry Form]!DateHired) / 365.25
>     Me!LeaveEntitlement = DLookup("LeaveDays", "Leave Group", _
>              "LeaveGroupStYr >= " & ServiceYears & _
>              " AND LeaveGroupEndYr < " & ServiceYears)
>    
> End Sub
> 
> I tested out that if I have:
> DLookup("LeaveDays", "Leave Group", "LeaveGroupStYr >= " & ServiceYears & _
>              " AND LeaveGroupEndYr < " & ServiceYears)
> Result: NULL.
> 
> However, if either of the following, i get results, in terms of numeric value:
> DLookup("LeaveDays", "Leave Group", "LeaveGroupStYr >= " & ServiceYears)
> Result: 16
> DLookup("LeaveDays", "Leave Group", "LeaveGroupEndYr < " & ServiceYears)
> Result: 10
> 
> Can anyone help me as I can't get the record I want.
0
Reply Utf 11/1/2007 6:18:01 AM


Hi Steve,

Thank you very much. It works.

"Steve Sanford" wrote:

> Hi Kelvin,
> 
> To calculate date differences, you should use the DateDiff() function.
> 
> The reason you weren't getting the result you were looking for is that the 
> gT and LT signs are reversed.
> 
> Try this:
> 
> '------------------------------------
> Private Sub cmdCalculateLeave_Click()
>     Dim ServiceYears As Double
>     Dim sCriteria As String
> 
>     ServiceYears = DateDiff("yyyy", CDate(Me.DateHired), Date)
>     sCriteria = "LeaveGroupStYr <= " & ServiceYears & " AND LeaveGroupEndYr 
> > " & ServiceYears
> 
>     '    Me.Service = ServiceYears    ' test control to see years diff
> 
>     Me!LeaveEntitlement = DLookup("LeaveDays", "Leave Group", sCriteria)
> End Sub
> '------------------------------------
> 
> I used a variable for the criteria so I could see the criteria string when 
> stepping thru the code.
> 
> 
> HTH
> -- 
> Steve S
> --------------------------------
> "Veni, Vidi, Velcro"
> (I came; I saw; I stuck around.)
> 
> 
> "Kelvin Leong" wrote:
> 
> > Hi there,
> > 
> > I have a table which has the following structure from which I want to 
> > extract some information:
> > 
> > --------------------------------------------------------------------
> > | LGID | LeaveGroupStYr | LeaveGroupEndYr | LeaveDays |
> > --------------------------------------------------------------------
> > |    1  |         0               |              2           |      10       |
> > |    2  |         2               |              5           |      12       |
> > |    3  |         5               |             99          |      16       |
> > -------------------------------------------------------------------
> > 
> > I am coding for a command button to get the LeaveDays if the ServiceYears 
> > falls between the LeaveGroupStYr and LeaveGroupStYr
> > 
> > Private Sub cmdCalculateLeave_Click()
> > 
> > Dim ServiceYears As Double
> > 
> >     ServiceYears = (Date - Forms![Leave Entry Form]!DateHired) / 365.25
> >     Me!LeaveEntitlement = DLookup("LeaveDays", "Leave Group", _
> >              "LeaveGroupStYr >= " & ServiceYears & _
> >              " AND LeaveGroupEndYr < " & ServiceYears)
> >    
> > End Sub
> > 
> > I tested out that if I have:
> > DLookup("LeaveDays", "Leave Group", "LeaveGroupStYr >= " & ServiceYears & _
> >              " AND LeaveGroupEndYr < " & ServiceYears)
> > Result: NULL.
> > 
> > However, if either of the following, i get results, in terms of numeric value:
> > DLookup("LeaveDays", "Leave Group", "LeaveGroupStYr >= " & ServiceYears)
> > Result: 16
> > DLookup("LeaveDays", "Leave Group", "LeaveGroupEndYr < " & ServiceYears)
> > Result: 10
> > 
> > Can anyone help me as I can't get the record I want.
0
Reply Utf 11/1/2007 7:12:03 AM

You're very welcome.

-- 
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


"Kelvin Leong" wrote:

> Hi Steve,
> 
> Thank you very much. It works.
> 
> "Steve Sanford" wrote:
> 
> > Hi Kelvin,
> > 
> > To calculate date differences, you should use the DateDiff() function.
> > 
> > The reason you weren't getting the result you were looking for is that the 
> > gT and LT signs are reversed.
> > 
> > Try this:
> > 
> > '------------------------------------
> > Private Sub cmdCalculateLeave_Click()
> >     Dim ServiceYears As Double
> >     Dim sCriteria As String
> > 
> >     ServiceYears = DateDiff("yyyy", CDate(Me.DateHired), Date)
> >     sCriteria = "LeaveGroupStYr <= " & ServiceYears & " AND LeaveGroupEndYr 
> > > " & ServiceYears
> > 
> >     '    Me.Service = ServiceYears    ' test control to see years diff
> > 
> >     Me!LeaveEntitlement = DLookup("LeaveDays", "Leave Group", sCriteria)
> > End Sub
> > '------------------------------------
> > 
> > I used a variable for the criteria so I could see the criteria string when 
> > stepping thru the code.
> > 
> > 
> > HTH
> > -- 
> > Steve S
> > --------------------------------
> > "Veni, Vidi, Velcro"
> > (I came; I saw; I stuck around.)
> > 
> > 
> > "Kelvin Leong" wrote:
> > 
> > > Hi there,
> > > 
> > > I have a table which has the following structure from which I want to 
> > > extract some information:
> > > 
> > > --------------------------------------------------------------------
> > > | LGID | LeaveGroupStYr | LeaveGroupEndYr | LeaveDays |
> > > --------------------------------------------------------------------
> > > |    1  |         0               |              2           |      10       |
> > > |    2  |         2               |              5           |      12       |
> > > |    3  |         5               |             99          |      16       |
> > > -------------------------------------------------------------------
> > > 
> > > I am coding for a command button to get the LeaveDays if the ServiceYears 
> > > falls between the LeaveGroupStYr and LeaveGroupStYr
> > > 
> > > Private Sub cmdCalculateLeave_Click()
> > > 
> > > Dim ServiceYears As Double
> > > 
> > >     ServiceYears = (Date - Forms![Leave Entry Form]!DateHired) / 365.25
> > >     Me!LeaveEntitlement = DLookup("LeaveDays", "Leave Group", _
> > >              "LeaveGroupStYr >= " & ServiceYears & _
> > >              " AND LeaveGroupEndYr < " & ServiceYears)
> > >    
> > > End Sub
> > > 
> > > I tested out that if I have:
> > > DLookup("LeaveDays", "Leave Group", "LeaveGroupStYr >= " & ServiceYears & _
> > >              " AND LeaveGroupEndYr < " & ServiceYears)
> > > Result: NULL.
> > > 
> > > However, if either of the following, i get results, in terms of numeric value:
> > > DLookup("LeaveDays", "Leave Group", "LeaveGroupStYr >= " & ServiceYears)
> > > Result: 16
> > > DLookup("LeaveDays", "Leave Group", "LeaveGroupEndYr < " & ServiceYears)
> > > Result: 10
> > > 
> > > Can anyone help me as I can't get the record I want.
0
Reply Utf 11/1/2007 7:23:02 AM

3 Replies
187 Views

(page loaded in 0.094 seconds)

Similiar Articles:
















7/21/2012 12:58:17 AM


Reply: