vba - 公用函数的 SQL 语句
问题描述
我有一个查询,根据工作日为我提供当前员工率:
(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)
该项目正在增长。我需要在不同的表单/报告上返回员工率,所以我想将此 SQL 转换为公共函数。
我试过这个:
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
我收到“类型不匹配错误”。
解决方案
经过一番修改后,我想出了这个:
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
推荐阅读
- haproxy - 具有后端状态的 HAPROXY 初始化 - DOWN
- javascript - iframe多级嵌套时cookie丢失在同一主机
- python-3.x - 是否有资源可以显示与我的键盘相关的所有键盘键码?
- java - 判断两棵二叉树是否相似
- node.js - Discord.js 如何让我的机器人在添加到服务器时通知我?
- javascript - 如何仅使用 JavaScript 制作数字掩码?
- javascript - 为什么刷新页面时本地存储被删除?
- amazon-web-services - AWS 管理的区块链超级账本结构如何加入开源超级账本结构通道并查询链码?
- sqlite - Flutter - 使用来自 sqlite 数据库的建议自动完成文本字段
- r - 错误:当我想在 R 中的 facet_wrap 中进行注释时,美学必须是长度 1 或与数据 (1) 相同