向表中添加行会导致之前正在运行的代码出错

时间:2018-05-29 16:31:58

标签: vba ms-access

我的代码提示用户输入" RAMsize"并将值存储在表中。另一个字段" Likelihood",不能大于" RAMsize"。我让它工作,如果" RAMsize"大于"可能性"它会设置"可能性"为了避免冲突,这是正常的,没有问题。然后我在Likelihood表中添加了更多行,并且验证规则错误再次出现。我还没有在VBA工作很长时间,所以我不确定如何使代码适用于表的所有行。

有问题的守则:

Option Compare Database

Private Sub Command0_Click()

Dim RAMsize As Variant
Dim Lhood As Integer
Dim db As Database
Dim rs As Recordset

Set db = CurrentDb
Set rs = db.OpenRecordset("tLikelihood")


'Promts user to enter desired matrix dimensions
RAMsize = InputBox("Please enter your desired matrix dimensions", "Matrix Dimensions")


'Ends process if user clicks 'cancel' or gives no input
If RAMsize = "" Then
    MsgBox "Dimension selection was canceled", vbOKOnly

    GoTo Exit_DimSelect
End If

'Checks if RAMsize causes conflict with validation rule in tLikelihood
RAMsize = CInt(RAMsize)

Lhood = rs!Likelihood

If RAMsize < Lhood Then

    rs.Edit
    rs!Likelihood = 0
    rs.Update

End If


'Saves the user input for RAMsize in the tRAMSize table for later use
Set rs = db.OpenRecordset("tRAMSize")


rs.Edit
rs!Size = RAMsize
rs.Update

感谢任何帮助

1 个答案:

答案 0 :(得分:1)

如果我理解您要正确执行的操作,如果您将其放在行RAMsize = CInt(RAMsize)

之后,则可以使用此功能
db.Execute "UPDATE tLikelihood SET Likelihood = 0 WHERE Likelihood > " & RAMsize

然后删除所有内容。这将影响表中可能性>的所有记录。您提示用户输入的值。