2013-08-02 59 views
1

我有一個包含多個字段的訪問表。我想通讀所有行,所有字段,如果該字段包含NULL,請設置默認值「」。我正嘗試使用字段定義來讀取表格,但無法找到該字段的現有值。當您不知道字段名稱時將Access表字段設置爲值

Dim fld As DAO.Field 
Dim t As DAO.TableDef 
Dim fldVal as string (test variable to see if I can find value of field) 

    For Each t In cd.TableDefs 

    If t.Name = "PERSONS" Then 
    For Each fld In t.Fields 
    Just trying to find the existing value here 
     fldVal = fld.Value 


     If fld.Type = 10 And IsNull(fld) = True Then 
     fld.Value = "" 
     End If 

    Next 
End If 
Next 
+0

問題出在哪裏?錯誤信息?如果是這樣,哪一行觸發它? – HansUp

+0

在線fldVal = fld.value我得到一個錯誤:3219無效的操作 – Lele

+1

如果你把Dim fldVal作爲字符串,那麼你不能給它賦一個Null值。嘗試'Dim fldVal As Variant'。 –

回答

1

我想這可能就是你所想要的效果人士表。

Dim cd As DAO.Database 
Dim fld As DAO.Field 
Dim t As DAO.TableDef 
Dim rs As DAO.Recordset 
Dim fldVal As Variant '(test variable to see if I can find value of field) 

Set cd = CurrentDb 
For Each t In cd.TableDefs 
    If t.Name = "PERSONS" Then 
     Set rs = cd.OpenRecordset(t.Name, dbOpenTable) 
     Do While Not rs.EOF 
      For Each fld In rs.Fields 
       'Just trying to find the existing value here 
       fldVal = fld.value 

       If (fld.Type = dbText Or fld.Type = dbMemo) _ 
         And IsNull(fld.value) = True Then 
        fld.value = "" 
       End If 
      Next fld 
     rs.MoveNext 
     Loop 
    End If 
Next t 
0

變化

If fld.Type = 10 And IsNull(fld) = True Then 

If fld.Type = 10 And IsNull(fld.Value) = True Then 
相關問題