我有一个Repoerteq表,其中REQ_NUM作为ID,另一列名为“REQ_department”。 REQ_department有诸如“财务”之类的侮辱价值。
我想让VBA查看部门,然后为REQ_NUM设置前缀格式 例如,部门是财务,那么它将id为“FIN 000”
以下代码是我目前管理的,但它仍然无法正常工作
Option Compare Database
Function GetData() As String
Dim db As Database
Dim Rrs As DAO.Recordset
Dim RSQL As String
Dim RepData As String
Dim RepDep As String
'TO open connection to current Access DB
Set db = CurrentDb()
'TO create SQL statement and retrieve value from ReportReq table
RSQL = "select * from ReportReq"
Set Rrs = db.OpenRecordset(RSQL)
'Retrieve value if data is found
If Rrs.EOF = False Then
RepData = Rrs("REQ_NUM")
RepDep = Rrs("Req_department")
Else
RepData = "Not found"
RepDep = "Not found"
End If
Lrs.Close
Set Lrs = Nothing
GetData = RepData
If ReqDep = "finance" Then
Range("REQ_NUM") = Format$("FIN", REQ_NUM)
End If
End Function
答案 0 :(得分:0)
您必须更改对Format()
函数的调用,该函数会尝试根据格式字符串格式化数字或日期。此外,您还在使用未定义的变量REQ_NUM。
If ReqDep = "finance" Then
Range("REQ_NUM") = "FIN " & Format$("000", CLng(RepData))
' ^^^^^^ ^^^^^^^^^^^^^^^^^^^^
End If