[公开函数的SQL语句

问题描述 投票:-1回答:1

早安,我有一个查询,使用以下命令根据工作日为我提供当前员工费率:

(Select Top 1 T1.Rate FROM tblERates as T1 WHERE T1.EMPID = tblPayroll.EMPID And T1.EffectiveDate <= tblPayroll.WorkDay ORDER BY T1.EMPID asc, T1.EffectiveDate desc)

现在该项目正在增长,我需要能够在不同的表单/报表上返回Employee Rate,因此我想将此sql转换为Public Function。

我已经尝试过:


Public Function fncERate(EID As Integer, WD As Date) As Double

    Dim intERate As Double
    Dim intWD As String

    intWD = "#" & Format(WD, "m/d/yyyy") & "#"

    intERate = "SELECT TOP 1 Rate" _
                & "FROM tblERates" _
                & "WHERE EMPID = EID And EffectiveDate <= intWD" _
                & "ORDER BY EMPID asc;"
    fncERate = intERate

End Function

但是我收到“类型不匹配错误”

vba ms-access access-vba
1个回答
0
投票

[稍作修改后,我想到了这个:

Public Function fncERate(EID As Integer, WD As Date) As Double


    Dim intERate As String
    Dim intWD As String
    Dim intEID As Integer


    intWD = "#" & Format(WD, "m/d/yyyy") & "#"

    intERate = "SELECT TOP 1 [Rate]" & _
                " FROM tblERates" & _
                " WHERE [EMPID] = " & EID & " And [EffectiveDate] <= " & intWD & " " & _
                " ORDER BY [EMPID] asc;"

    With CurrentDb.OpenRecordset(intERate)
        If Not (.BOF And .EOF) Then
            fncERate = .Fields(0)
        End If
    End With


End Function
© www.soinside.com 2019 - 2024. All rights reserved.