我使用AtValue
和AtField
助手這樣
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
來源
2013-10-30 19:51:08
wqw
嘗試'如果ISNULL(rs.Fields( 「字段名」)。值)Then' ... – Plutonix
@Plutonix沒」 t工作= \ – PlayHardGoPro
如果isnull(rs.field(NAME).value)/如果不是IsNull(rs.field(NAME).value)'應該工作。你可能想檢查一下rs或field沒有什麼奇怪的地方。如果你絕望,只需使用'var =「」&rs.Fields(「fieldname」).value'或'txtName =「」&rs.Fields(「Name」)。Value' – Plutonix