2013-10-29 50 views
3

如果recordset的字段爲空或空,我正在嘗試pre-view檢查記錄集是否爲空字段

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

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

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

所有這些方法引發錯誤...爲什麼?在將其值賦給變量之前,我如何檢查recordset是否爲空。

+3

嘗試'如果ISNULL(rs.Fields( 「字段名」)。值)Then' ... – Plutonix

+0

@Plutonix沒」 t工作= \ – PlayHardGoPro

+2

如果isnull(rs.field(NAME).value)/如果不是IsNull(rs.field(NAME).value)'應該工作。你可能想檢查一下rs或field沒有什麼奇怪的地方。如果你絕望,只需使用'var =「」&rs.Fields(「fieldname」).value'或'txtName =「」&rs.Fields(「Name」)。Value' – Plutonix

回答

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 
+1

在vb6上沒有'IsDBNull' = \ – PlayHardGoPro

+0

rs.Fields(「fieldname」)= NULL – 2013-10-29 20:53:07

+0

我寫了VB6已經有一段時間了,但我認爲這是有效的。 – 2013-10-29 20:53:24

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 
+0

我只想檢查該字段是否爲空,而不是整個記錄集; x – PlayHardGoPro

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 
+0

我發現了錯誤。我無法將它與nullValue進行比較,因爲它不爲null,實際上它甚至不存在於記錄集上,它應該搜索的字段(「fieldname」)不在記錄集上......-"-是否存在預覽它的方法? – PlayHardGoPro

+0

預覽什麼?記錄集? – Calvin

+0

看,我正在搜索的字段,例如: 'If(rs.fields(「Name」)<>「」)然後...' 但是字段名稱不存在於該記錄集上,這就是爲什麼錯誤發射......所以我需要一個爲什麼要檢查記錄集上的字段而不發出錯誤 – PlayHardGoPro

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這樣

(所以要對所有錯誤 設置工作)
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 
-1

rs.EOF標誌將告訴記錄集是否爲空

如果不RS.EOF然後 ..Your期望的邏輯.. 結束如果

+0

雖然代碼是讚賞的,但它應該總是有相應的解釋不一定是漫長的,但它是預期的。 – peterh