質問

I'm trying to pre-view if a field of the recordset is empty/null or not.

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

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

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

All of these methods fires up an error... Why? How may I check if the recordset is null before I assign it's value to a variable.

役に立ちましたか?

解決 3

Try using IsDbNull() instead. DbNull is different than Null.

Edit, just loop through the field names and have a boolean if it found it, otherwise use a try catch structure.

For Each field in rs.Fields
   if field.Name = "someFieldName" then 
      foundField = true 
  exit for
   else 
      foundField = false
   end if
next

他のヒント

If I understand correctly, you want to ensure that a field exists in the recordset. If that is correct, you need to either iterate the fields looking for the field you are searching for, or try to directly access the field and trap any errors. Here is a method that iterates the field collection and returns True if the field exists.

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

Here is a way to print out the columns of a table.

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

I'm using AtValue and AtField helpers like this

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

My type-casting helpers are actually using VariatChangeType API (so to work with Break on all errors setting) like this

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

rs.EOF flag will tell whether RecordSet is Empty or not

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top