我正在尝试编写一个VBA函数,该函数使用SELECT查询从Access表中获取值.这是代码:
Function getTableValue(uniqueID As Long, tableName As String, _
idField As String, tableField As String) As Variant
Dim db As Database
Dim rs As Recordset
Dim selectSQL As String
selectSQL = "select * from " & tableName & " where " & idField & "=" & uniqueID
Set db = OpenDatabase(dbPath)
Set rs = db.OpenRecordset(selectSQL)
If rs.RecordCount > 0 Then
rs.MoveLast
rs.MoveFirst
getTableValue = rs.Fields(tableField)
End If
End Function
Run Code Online (Sandbox Code Playgroud)
如果指定的字段tableField是日期/时间类型且字段为空,则getTableValue()返回"无效使用空"错误.我认为让函数返回Variant将允许我返回Null值; 我该怎么做来处理这些空日期?
事实证明函数的Null值被传递给Date/Time变量.我通过使用以下内容修复了这个问题:
dim myDate as date
dim myVar as variant
myVar=getTableValue(idNum,"thisTable","idField", "valueField")
if isNull(myVar) then
myDate=0
else
myDate=myVar
endif
Run Code Online (Sandbox Code Playgroud)
虽然它不检查是否myVar返回Date,但其valueField值的类型为Date/Time,所以没关系.
| 归档时间: |
|
| 查看次数: |
16285 次 |
| 最近记录: |