编辑 Access 2013 中的特定行 table
Edit specific row in an Access 2013 table
我有 Table1
,其中 CustomerID
和 Category
作为主键,还有我用来编辑此 table 的表单。表格中有几个 FieldComboBoxes
包含其他信息,而 CategoryComboBox
在我 select 一个选项后更新 table 。
我想做的是查找 table 中是否存在主键,如果存在,则查找并编辑该行。我在 CategoryComboBox 的 On Change 事件中使用 VBA:
Dim db As DAO.Database
Dim rec As DAO.Recordset
Set db = CurrentDb
Set rec = db.OpenRecordset("Table1")
If Not IsNull(DLookup("CustomerID", "Table1", "CustomerID = " & [CustomerID] AND _
"Category = '" & CategoryComboBox.Value & "'")) Then
rec.Edit
rec!CustomerID = CustomerID
rec!Category = CategoryComboBox.Value
rec!Field1 = Field1ComboBox.Value
rec!Field2 = Field2ComboBox.Value
...
rec.Update
End If
如果 table 中存在与表单中当前记录具有相同主键的行,我希望它更新该行,但实际情况是它更新了 table 不管 PrimaryKey。我也尝试在 AddNew 命令之前添加 If Category = CategoryComboBox.Value
但它没有效果。
简而言之,我如何才能访问 select 并编辑特定行 - 在我的例子中,是具有正确主键的行?
这个有用吗?我很困惑它如何知道根据 customerID 更新哪个记录,因为我认为可能有多个,但我认为这会让你更接近。
Dim db As DAO.Database
Dim rec As DAO.Recordset
Dim sql As String
Set db = CurrentDb
Set rec = db.OpenRecordset("Table1")
If IsNull(DLookup("CustomerID", "Table1", "CustomerID = " & Me![CustomerID] AND _
"Category = '" & Me!CategoryComboBox.Value & "'")) Then
rec.AddNew
rec!CustomerID = CustomerID
rec!Category = CategoryComboBox.Value
rec.Update
ELSE:
sql = "UPDATE Table1 SET Category = " & Me!CategoryComboBox.Value & " WHERE CustomerID = " & Me![CustomerID]
DoCmd.RunSql sql
End If
Set rec = Nothing
Set db = Nothing
Exit Sub
End Sub
您的代码说 "if a record exists in Table1 for this customer and category, then add a new record to table one for this customer and category" 如果如您所描述的那样,您正在编辑现有行而不是添加新行,那么您的编辑将是空操作,因为您正在设置 Customer 和 Category 以匹配您的标准是什么——所以我假设您的示例中缺少某些内容,如下所示 [other_field] 和 'new value'.
首先,对于像您描述的这样的操作,直接使用 SQL 比创建记录集、对象化记录并通过 VBA 更新要快得多。例如:
currentdb.execute "UPDATE Table1 set [other_field] = 'new value' where CustemerID = " & CustemerID & " AND Category = '" CategoryComboBox.Value & "';"
但是如果你确实有一些复杂的东西并且需要一个 VBA 挂钩到记录那么首选是限制记录集:
Set rec = db.OpenRecordSet "select * from Table1 where customerID = " & CustomerID & " AND CategoryComboBox.Value = '" & CategoryComboBox.Value & "';"
if rec.RecordCount > 0 then
rec.movefirst
rec.edit
rec.[other field] = 'new value'
rec.update
rec.close
end if
同样,让sql尽可能多地完成。
如果您出于其他原因确实需要记录集中的所有内容,那么
Set rec = db.OpenRecordSet "Table1"
if rec.RecordCount > 0 then
rec.findfirst("customerID = " & CustomerID & " AND CategoryComboBox.Value = '" & CategoryComboBox.Value & "'")
if not rec.nomatch then
rec.edit
rec.[other field] = 'new value'
rec.update
rec.close
end if
end if
我有 Table1
,其中 CustomerID
和 Category
作为主键,还有我用来编辑此 table 的表单。表格中有几个 FieldComboBoxes
包含其他信息,而 CategoryComboBox
在我 select 一个选项后更新 table 。
我想做的是查找 table 中是否存在主键,如果存在,则查找并编辑该行。我在 CategoryComboBox 的 On Change 事件中使用 VBA:
Dim db As DAO.Database
Dim rec As DAO.Recordset
Set db = CurrentDb
Set rec = db.OpenRecordset("Table1")
If Not IsNull(DLookup("CustomerID", "Table1", "CustomerID = " & [CustomerID] AND _
"Category = '" & CategoryComboBox.Value & "'")) Then
rec.Edit
rec!CustomerID = CustomerID
rec!Category = CategoryComboBox.Value
rec!Field1 = Field1ComboBox.Value
rec!Field2 = Field2ComboBox.Value
...
rec.Update
End If
如果 table 中存在与表单中当前记录具有相同主键的行,我希望它更新该行,但实际情况是它更新了 table 不管 PrimaryKey。我也尝试在 AddNew 命令之前添加 If Category = CategoryComboBox.Value
但它没有效果。
简而言之,我如何才能访问 select 并编辑特定行 - 在我的例子中,是具有正确主键的行?
这个有用吗?我很困惑它如何知道根据 customerID 更新哪个记录,因为我认为可能有多个,但我认为这会让你更接近。
Dim db As DAO.Database
Dim rec As DAO.Recordset
Dim sql As String
Set db = CurrentDb
Set rec = db.OpenRecordset("Table1")
If IsNull(DLookup("CustomerID", "Table1", "CustomerID = " & Me![CustomerID] AND _
"Category = '" & Me!CategoryComboBox.Value & "'")) Then
rec.AddNew
rec!CustomerID = CustomerID
rec!Category = CategoryComboBox.Value
rec.Update
ELSE:
sql = "UPDATE Table1 SET Category = " & Me!CategoryComboBox.Value & " WHERE CustomerID = " & Me![CustomerID]
DoCmd.RunSql sql
End If
Set rec = Nothing
Set db = Nothing
Exit Sub
End Sub
您的代码说 "if a record exists in Table1 for this customer and category, then add a new record to table one for this customer and category" 如果如您所描述的那样,您正在编辑现有行而不是添加新行,那么您的编辑将是空操作,因为您正在设置 Customer 和 Category 以匹配您的标准是什么——所以我假设您的示例中缺少某些内容,如下所示 [other_field] 和 'new value'.
首先,对于像您描述的这样的操作,直接使用 SQL 比创建记录集、对象化记录并通过 VBA 更新要快得多。例如:
currentdb.execute "UPDATE Table1 set [other_field] = 'new value' where CustemerID = " & CustemerID & " AND Category = '" CategoryComboBox.Value & "';"
但是如果你确实有一些复杂的东西并且需要一个 VBA 挂钩到记录那么首选是限制记录集:
Set rec = db.OpenRecordSet "select * from Table1 where customerID = " & CustomerID & " AND CategoryComboBox.Value = '" & CategoryComboBox.Value & "';"
if rec.RecordCount > 0 then
rec.movefirst
rec.edit
rec.[other field] = 'new value'
rec.update
rec.close
end if
同样,让sql尽可能多地完成。
如果您出于其他原因确实需要记录集中的所有内容,那么
Set rec = db.OpenRecordSet "Table1"
if rec.RecordCount > 0 then
rec.findfirst("customerID = " & CustomerID & " AND CategoryComboBox.Value = '" & CategoryComboBox.Value & "'")
if not rec.nomatch then
rec.edit
rec.[other field] = 'new value'
rec.update
rec.close
end if
end if