1

我需要在 2 个单独的记录集上测试 2 个不同的条件。我不擅长 VBA,我有一个非常基本的代码,我只需要使用 If Then 语法的帮助。这是我的代码:

Private Sub SaveRecord_Click()

    '****  add a new record  ****
    Dim db As Database, rs1 As Recordset, rs2 As Recordset

    Set db = CurrentDb
    Set rs1 = db.OpenRecordset("ExpAsset", DB_OPEN_DYNASET)
    Set rs2 = db.OpenRecordset("LogTest", DB_OPEN_DYNASET)

'****  Following code is updating the tables in the ExpAsset table with new information     from the form****
If rs1.NoMatch Then
        rs1.AddNew
        rs1("User") = Me!Location
        rs1("Type") = Me!Type
        rs1("Model") = Me!MODEL
        rs1("Asset_ID") = Me!Asset_ID
        rs1("Serial_Number") = Me!Serial
        rs1.Update
Else
        MsgBox "Serial Number: " & Me!Serial & " already exists.", 48, "ERROR!"
        Me!Serial.SetFocus

End If
'****  Following code is creating a log in Logtest table with information provided in the form****
If rs2.NoMatch Then
        rs2.AddNew
        rs2("Asset_Type") = Me!Type
        rs2("Transfer_Type") = "New purchase"
        rs2("Description") = Me!DESCRIPTION
        rs2("DELIVERED_TO") = Me!Location
        rs2("DELIVERED_BY") = Me!DeliveredBy
        rs2("RECEIVED_BY") = Me!Receiver
        rs2("RECEIVED_DATE") = Me!Date
        rs2.Update

        MsgBox "Part information has been updated in the database!"

        'clear the controls to add more customers
        Call ClearControls
Else
        MsgBox "Asset ID: " & Me!Asset_ID & " already exists.", 48, "ERROR!"
        Me!Asset_ID.SetFocus
End If

    rs1.Close
    rs2.Close
    db.Close

End Sub

我知道 If Then Else 语法不正确,我需要检查两个条件,序列号。和资产 ID。

4

2 回答 2

1

检查Recordset.NoMatch 属性的 Access 联机帮助主题:

指示是否使用Seek方法或Find方法之一找到了特定记录(仅限 Microsoft Access 工作区)。

但是在您的代码中,您正在打开一个记录集,但不使用搜索或查找。在那种情况下,你没有要求匹配任何东西,所以每次都会.NoMatchFalse逻辑是这样的……

If rs1.NoMatch Then
    ' this code will never run
Else
    ' this code runs every time
End If

您可以使用DCount来确定是否ExpAsset包含给定的Asset_ID值。

DCount("*", "ExpAsset", "Asset_ID = " & Me!Asset_ID) ' if Asset_ID is numeric
DCount("*", "ExpAsset", "Asset_ID = '" & Me!Asset_ID & "'") ' if Asset_ID is text

一旦你有了一个工作DCount表达式,你就可以使用这样的逻辑......

If DCount("*", "ExpAsset", "Asset_ID = " & Me!Asset_ID) = 0 Then
    ' Asset_ID not present in table -> add it
Else
    ' inform user Asset_ID already present in table
End If
于 2013-05-26T14:19:15.797 回答
1

你是对的 HansUp,我的代码很愚蠢,我在发布后才意识到没有标准可以测试。以下是正确的代码,我对其进行了测试并且可以正常工作:)

Private Sub SaveRecord_Click()


    '****  add a new record  ****
    Dim db As Database, rs1 As Recordset, rs2 As Recordset, Criteria As String, Criteria2 As String

    Set db = CurrentDb
    Set rs1 = db.OpenRecordset("ExpAsset", DB_OPEN_DYNASET)
    Set rs2 = db.OpenRecordset("LogTest", DB_OPEN_DYNASET)

    Criteria = "[serial_number]='" & Me!Serial & "'"
    Criteria2 = "[Asset_ID]='" & Me!Asset_ID & "'"
'****  Following code is updating the tables in the ExpAsset table with new information from the form****
rs1.FindFirst Criteria
If rs1.NoMatch Then
    rs1.FindFirst Criteria2
    If rs1.NoMatch Then
        rs1.AddNew
        rs1("User") = Me!Location
        rs1("Type") = Me!Type
        rs1("Model") = Me!MODEL
        rs1("Asset_ID") = Me!Asset_ID
        rs1("Serial_Number") = Me!Serial
        rs1.Update

'****  Following code is creating a log in Logtest table with information provided in the form****
     rs2.AddNew
        rs2("Asset_Type") = Me!Type
        rs2("Transfer_Type") = "New purchase"
        rs2("Description") = Me!DESCRIPTION
        rs2("DELIVERED_TO") = Me!Location
        rs2("DELIVERED_BY") = Me!DeliveredBy
        rs2("RECEIVED_BY") = Me!Receiver
        rs2("RECEIVED_DATE") = Me!Date
        rs2.Update

        MsgBox "Part information has been updated in the database!"
    'clear the controls to add more customers
        Call ClearControls

    Else
        MsgBox "Asset_ID: " & Me!Asset_ID & " already exists.", 48, "ERROR!"
        Me!Asset_ID.SetFocus
    End If
Else
        MsgBox "Serial Number: " & Me!Serial & " already exists.", 48, "ERROR!"
        Me!Serial.SetFocus

End If

    rs1.Close
    rs2.Close
    db.Close


End Sub
于 2013-05-26T14:57:07.147 回答