3

我试图在 MS Access 中创建一个 SQL 语句,它将在表中查找空白字段。比如说,一个病人的姓氏丢失了——这个查询应该把它捡起来。

我该怎么做呢?我能够确定如何为第一条记录执行此操作,但对于整个表来说,这被证明是困难的

Dim Rst As recordSet
Dim f As Field

'Current Record set
Set Rst = CurrentDb.OpenRecordset("tblWebMeetingData")

'Holds current fields data
Dim fieldData

'With Rst
    'Do Until Rst.EOF
       For Each f In Rst.Fields
            If IsNull(f.Value) Then
                MsgBox ("Field Name: " & f.Name)
            End If
       Next
    'Loop
'End With

Rst.Close
4

3 回答 3

2

更优雅:

Dim i As String
i = "tblWebMeetingData"
Dim j As Integer
Dim rst As Recordset

' For each Field in the table
For j = 0 To CurrentDb.TableDefs(i).Fields.Count - 1
    ' Return the number of lines that are null
    Set rst = CurrentDb.OpenRecordset("SELECT count(*) FROM " & CurrentDb.TableDefs(i).Name & " WHERE " & CurrentDb.TableDefs(i).Fields(j).Name & " IS NULL")
    rst.MoveFirst
    ' Check if it's more than one
    If rst.Fields(0).Value > 0 Then
        MsgBox CurrentDb.TableDefs(i).Fields(j).Name
    End If
Next
于 2013-03-07T22:06:49.197 回答
2

MoveNext在我看来,如果您之前使用过,您的代码会做您想做的事Loop。我在这个版本中做了一些其他的小改动。

Dim Rst As DAO.recordSet
Dim f As DAO.Field
Dim db As DAO.Database

'Current Record set
Set db = CurrentDb
Set Rst = db.OpenRecordset("tblWebMeetingData", dbOpenTable, dbOpenSnapshot)

With Rst
    Do While Not .EOF
       For Each f In .Fields
            If IsNull(f.Value) Then
                MsgBox "Field Name: " & f.Name
            End If
       Next
       .MoveNext
    Loop
End With

Rst.Close

但是,在您所说的问题中,您想要“在 MS Access 中创建一个 SQL 语句,它将在表中查找空白字段”。但是,您向我们展示了检查DAO.Recordset. 我不确定你真正想要什么。

于 2013-03-07T22:09:11.907 回答
1

为什么不只是:

Public Sub CheckNull(FieldName as String)
    Dim rs as DAO.RecordSet

    Set rs = CurrentDB.OpenRecordset("SELECT IDField FROM tblMyTable WHERE " & FieldName & " Is Null")

    If rs.eof then exit sub

    Do until rs.Eof
        debug.print rs!IDField
        rs.movenext
    Loop
End Sub

这应该在代码中查找FieldName为 Null 的所有记录,然后将结果打印到您将 FieldName 作为字符串传递的即时窗口。

于 2013-03-08T14:20:57.367 回答