Vba If-Then-Else在两个单独的记录集上
我需要在两个单独的记录集上测试两个不同的条件。我不擅长VBA,我有一个非常基本的代码,我只是需要帮助,如果然后语法。这是我的密码:Vba If-Then-Else在两个单独的记录集上,vba,ms-access,if-statement,ms-access-2010,Vba,Ms Access,If Statement,Ms Access 2010,我需要在两个单独的记录集上测试两个不同的条件。我不擅长VBA,我有一个非常基本的代码,我只是需要帮助,如果然后语法。这是我的密码: 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("Exp
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。检查Recordset.NoMatch属性的Access online帮助主题: 指示是使用Seek方法还是使用Find方法之一(仅限Microsoft Access工作区)找到特定记录 但是,在代码中,您打开的是一个记录集,而不是使用seek或find。在这种情况下,您没有要求匹配任何内容,因此
.NoMatch
每次都将False
。逻辑与此相似
如果rs1.NoMatch,则
'此代码永远不会运行
其他的
'此代码每次都会运行
如果结束
您可以使用DCount
确定ExpAsset
是否包含给定的Asset\u ID
值
DCount(“*”,“ExpAsset”,“Asset\u ID=“&Me!Asset\u ID)”,如果Asset\u ID是数字
数据计数(“*”、“ExpAsset”、“Asset\u ID=”、&Me!Asset\u ID&“”),如果Asset\u ID为文本
一旦有了一个有效的DCount
表达式,就可以使用如下逻辑
如果DCount(“*”,“ExpAsset”,“Asset\u ID=“&Me!Asset\u ID)=0,则
'资产ID不在表中->添加它
其他的
'通知用户资产\u ID已存在于表中
如果结束
你说得对,HansUp,我的代码太傻了,我在发布后意识到没有标准可供测试。下面是正确的代码,我测试了它,它可以正常工作:)
请你编辑我的工作代码,我几乎没有VBA的知识,我在这里所拥有的只是从互联网上学习。我还搜索了两个东西,序列号和资产ID。
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