2013-05-26 63 views
1

我需要在2個獨立的記錄集上測試2個不同的條件。我對VBA不太好,我有一個非常基本的代碼,我只需要If Then語法的幫助。這是我的代碼:If Then else on 2 separate recordssets

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。

回答

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 
+0

請您編輯我的工作代碼,我旁邊有沒有VBA的知識,所有我這裏是從互聯網上學習。此外,我正在搜索2件事情,Serial_Number和Asset_ID。 – user1687929

1

檢查訪問在線幫助主題Recordset.NoMatch物業

表示特定的記錄是否被使用尋求方法或之一查找方法發現(Microsoft Access中只有工作區)。

但是,在您的代碼中,您打開一個記錄集,但不使用seek或find。在這種情況下,你並沒有要求匹配任何東西,所以.NoMatch每次都會是False。其中的邏輯是類似這樣的...

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