Access VBA 代码中的字段显示为空

Field in Access VBA code showing as Empty

我的任务是更改字段 "TimeAfterClass"(双精度)以生成一个包含唯一数字的新字段 "NewTimeAfterClass"(也是双精度)。我创建了一个按钮来完成此操作,它不会产生任何错误,但它也不会更新 NewTimeAfterClass 字段,当我将鼠标悬停在 [TimeAfterClass] 上时,它显示为空。关于为什么会发生这种情况的任何建议?这是代码:

Private Sub Command0_Click()
Dim CurrentTimeAfterClass As Double
Dim NewTimeAfterClass As Double
Dim strSQL As String
Dim increment As Double
increment = 0.01
'Dim db As DAO.Database
Dim rs As ADODB.Recordset

'Set db = CurrentDb
Set rs = New ADODB.Recordset
With rs
    .Open "Scores", CurrentProject.Connection, adOpenDynamic, adLockBatchOptimistic
    .MoveFirst
    While Not .EOF
    CurrentTimeAfterClass = [TimeAfterClass]
    CurrentIncrement = 0
    CurrentIncrement = increment
        NewTimeAfterClass = CurrentTimeAfterClass + CurrentIncrement
            If CurrentIncrement > 0 Then
            CurrentIncrement = CurrentIncrement * (-1)
            Else
            CurrentIncrement = CurrentIncrement * (-1) + increment
            End If
    .Update
    .MoveNext
    Wend
End With

rs.Close
Set rs = Nothing
End Sub

不确定你想用 increment 做什么,所以我只能假设你知道你在做什么。

要使更新工作,试试这个 - 也许在备份数据库上??

Private Sub Command0_Click()
    Dim CurrentTimeAfterClass As Double

    ' You don't need this if you're updating the field directly
    'Dim NewTimeAfterClass As Double

    Dim strSQL As String
    Dim increment As Double
    increment = 0.01

    'Dim db As DAO.Database
    Dim rs As ADODB.Recordset

    'Set db = CurrentDb

    Set rs = New ADODB.Recordset
    With rs
        .Open "Scores", CurrentProject.Connection, adOpenDynamic, adLockBatchOptimistic
        .MoveFirst
        While Not .EOF
        CurrentTimeAfterClass = !TimeAfterClass
        CurrentIncrement = 0
        CurrentIncrement = increment

        !NewTimeAfterClass = CurrentTimeAfterClass + CurrentIncrement
        If CurrentIncrement > 0 Then
            CurrentIncrement = CurrentIncrement * (-1)
        Else
            CurrentIncrement = CurrentIncrement * (-1) + increment
        End If

        .Update
        .MoveNext
        Wend
    End With

    rs.Close
    Set rs = Nothing
End Sub