如何将日期字段设置为空值

时间:2019-01-12 16:35:01

标签: ms-access access-vba

我正在尝试将date的值重置为null,并将weight的值重置为null。

我尝试了以下代码,但仅将权重设置为0,而日期未设置为null:

Private Sub Form_Load()
    Me.Text42.Value = ""

    Dim i As Integer
    Dim Db6 As Database
    Dim Rs6 As Recordset
    Dim Trn6 As String
    Set Db6 = CurrentDb
    Set Rs6 = Db6.OpenRecordset("GoatMasterTable")

    Do While Not Rs6.EOF
        If Rs6.Fields("Recentweight") > 0 Then
            Rs6.Edit
            Rs6.Fields("RecentWeight") = 0
            Rs6.Fields("RecentWeightDate") = """" Or IsNull(Rs6!RecentWeightDate)
            Rs6.Update
        End If
        Rs6.MoveNext
    Loop

    Rs6.Close
    Set Rs6 = Nothing
    Db6.Close
End Sub

2 个答案:

答案 0 :(得分:1)

由于您不执行依赖记录的计算,而只是将每条记录设置为相同的值,因此一种更简单的方法是简单地执行一条SQL语句,例如:

update GoatMasterTable set RecentWeight = 0, RecentWeightDate = Null where RecentWeight > 0

这样,您的功能可能会变为:

Private Sub Form_Load()
    Me.Text42 = ""
    With CurrentDb
        .Execute "update GoatMasterTable set RecentWeight = 0, RecentWeightDate = Null where RecentWeight > 0"
    End With
End Sub

您还需要确保RecentWeightDate中的GoatMasterTable字段允许使用空值。

答案 1 :(得分:0)

您需要做的就是将日期字段设置为 Null

Rs6.Fields("RecentWeight").Value = 0
Rs6.Fields("RecentWeightDate").Value = Null

当然,表的字段 RecentWeightDate 必须允许 Null 值。

相关问题