【问题标题】:Set Access table fields to a value when you do not know the field names当您不知道字段名称时,将 Access 表字段设置为一个值
【发布时间】:2013-08-02 16:15:30
【问题描述】:

我有一个包含多个字段的访问表。我想通读所有行、所有字段,如果该字段包含 NULL,则设置默认值“”。我正在尝试使用字段定义通读该表,但无法找到该字段的现有值。

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

【问题讨论】:

  • 问题出在哪里?错误信息?如果是这样,哪一行会触发它?
  • 在线 fldVal = fld.value 我得到一个错误:3219 Invalid operation
  • 如果您 Dim fldVal As String 则无法为其分配 Null 值。试试Dim fldVal As Variant
  • 我将 def 更改为变体,但仍然无法正常工作。如果我只是尝试使用 fldVal = fld.name 获取字段名称,它工作正常。
  • 我需要先阅读表格吗?也许它没有显示实际的数据值,因为我没有读入记录。

标签: ms-access field


【解决方案1】:

我认为这可能会为 PERSONS 表做你想要的。

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

【讨论】:

    【解决方案2】:

    改变

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-22
      • 1970-01-01
      • 1970-01-01
      • 2014-09-09
      相关资源
      最近更新 更多