5

我正在尝试pre-view该字段是否recordset为空/空。

If IsNull(rs.Fields("fieldname")) = True Then ...

If IsNull(rs.Fields("fieldname")).Value = True Then ...  

if IsNull(rs.Fields("fieldName").Value) Then...

所有这些方法都会引发错误......为什么?recordset在将它的值分配给变量之前,我如何检查它是否为空。

4

5 回答 5

1

这是一种打印表格列的方法。

Dim cat

Set cat = CreateObject("ADOX.Catalog")
Set cat.ActiveConnection = db 'db is the adodb.connection object

Dim tbl
Dim clm
For Each tbl In cat.Tables
   For Each clm In tbl.Columns
      Debug.Print (clm) ' Prints the column name from the table
   Next
Next
于 2013-10-29T20:10:12.307 回答
1

如果我理解正确,您希望确保记录集中存在一个字段。如果这是正确的,您需要遍历字段以查找您正在搜索的字段,或者尝试直接访问该字段并捕获任何错误。这是一个迭代字段集合并在字段存在时返回 True 的方法。

Public Function FieldExists(ByVal rsRecSet As ADODB.Recordset, ByVal FieldName As String) As Boolean
    Dim fld As ADODB.Field
    Dim Rtn As Boolean

    If Not rsRecSet Is Nothing Then
        For Each fld In rsRecSet.Fields
            If StrComp(fld.Name, FieldName, vbTextCompare) = 0 Then
                Rtn = True
                Exit For
            End If
        Next fld
    End If

    FieldExists = Rtn

End Function
于 2013-10-29T19:35:58.157 回答
0

我正在使用AtValueAtField这样的助手

Option Explicit

Private Sub Form_Load()
    Dim rs As Recordset

    If IsEmpty(AtValue(rs, "Test")) Then
        Debug.Print "Field is Empty or non-existant"
    End If

    If LenB(C2Str(AtValue(rs, "Test"))) = 0 Then
        Debug.Print "Field is Null, Empty, empty string or non-existant"
    End If
    '-- this will never fail, even if field does not exist
    AtField(rs, "Test").Value = 42
End Sub

Public Function AtValue(rs As Recordset, Field As String) As Variant
    On Error GoTo QH
    AtValue = rs.Fields(Field).Value
    Exit Function
QH:
'    Debug.Print "Field not found: " & Field
End Function

Public Function AtField(rs As Recordset, Field As String) As ADODB.Field
    Static rsDummy      As Recordset

    On Error GoTo QH
    Set AtField = rs.Fields(Field)
    Exit Function
QH:
'    Debug.Print "Field not found: " & Field
    Set rsDummy = New Recordset
    rsDummy.Fields.Append Field, adVariant
    rsDummy.Open
    rsDummy.AddNew
    Set AtField = rsDummy.Fields(Field)
End Function

Public Function C2Str(Value As Variant) As String
    On Error GoTo QH
    C2Str = CStr(Value)
QH:
End Function

我的类型转换助手实际上正在使用VariatChangeType这样的 API(以便在所有错误设置上使用Break )

Public Function C_Str(Value As Variant) As String
    Dim vDest           As Variant

    If VarType(Value) = vbString Then
        C_Str = Value
    ElseIf VariantChangeType(vDest, Value, VARIANT_ALPHABOOL, VT_BSTR) = 0 Then
        C_Str = vDest
    End If
End Function
于 2013-10-30T19:51:08.767 回答
0

尝试IsDbNull()改用。DbNull 与 Null 不同。

编辑,只需遍历字段名称并在找到它时使用布尔值,否则使用 try catch 结构。

For Each field in rs.Fields
   if field.Name = "someFieldName" then 
      foundField = true 
  exit for
   else 
      foundField = false
   end if
next
于 2013-10-29T19:34:25.560 回答
0

rs.EOF 标志将判断 RecordSet 是否为空

If Not rs.EOF Then ..Your desired logic.. End If

于 2015-11-11T12:48:04.440 回答