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

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 に答える