5

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.

5
  • 4
    try If IsNull(rs.Fields("fieldname").Value) Then ... Oct 29, 2013 at 19:30
  • @Plutonix Didn't work =\ Oct 29, 2013 at 19:33
  • 3
    if isnull(rs.field(NAME).value) / if not IsNull(rs.field(NAME).value) should work. You might want to check that there isnt something odd with the rs or field. If you get desperate, just use var = "" & rs.Fields("fieldname").Value or txtName = "" & rs.Fields("Name").Value Oct 29, 2013 at 20:45
  • @Plutonix I found out the error. I can't compare it to a nullValue because it's not null, actually it does not even exists on the recordset, the field it was supposed to search("fieldname") is not on the recordset... -.-" Is there a way to preview it ? Oct 30, 2013 at 10:30
  • fields(x) is the other way to access the data (such as in a For/Next loop); in which you can test for fields(x).Name Oct 30, 2013 at 12:47

5 Answers 5

1

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
1
  • I want to check only if that field is empty or not, not the whole recordset ;x Oct 29, 2013 at 19:43
1

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
4
  • I found out the error. I can't compare it to a nullValue because it's not null, actually it does not even exists on the recordset, the field it was supposed to search("fieldname") is not on the recordset... -.-" Is there a way to preview it ? Oct 30, 2013 at 10:31
  • Preview what? The recordset?
    – Calvin
    Oct 30, 2013 at 14:19
  • Look, the field i'm searching, e.g: If (rs.fields("Name") <> "") Then ... But the field Name does NOT exists on that recordset. That's why the error is firing... so I need a why to check that field on the recordset without firing an error. Oct 30, 2013 at 16:15
  • I edited my answer with a not so elegant solution to get column names. You could use this initially to store a list of them and then check if the name is in the list. There could be a more elegant solution, but this is all I've found for now.
    – Calvin
    Oct 30, 2013 at 20:07
0

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
8
  • 2
    There is no IsDBNull on vb6 =\ Oct 29, 2013 at 19:52
  • rs.Fields("fieldname") = Null
    – user2687130
    Oct 29, 2013 at 20:53
  • It's been awhile since I've written VB6 but I think that's valid.
    – user2687130
    Oct 29, 2013 at 20:53
  • I found out the error. I can't compare it to a nullValue because it's not null, actually it does not even exists on the recordset, the field it was supposed to search("fieldname") is not on the recordset... -.-" Is there a way to preview it ? Oct 30, 2013 at 10:30
  • 2
    @trey-morris not in VB6 :)
    – dummy
    Oct 30, 2013 at 16:04
0

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
0

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

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

1
  • Although the code is appreciated, it should always have an accompanying explanation. This doesn't have to be long, but it is expected.
    – peterh
    Nov 11, 2015 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.