内联接的查询方式,记录集不能马上更新
单个表不存在这个问题,如果在绑定Datagrid的SQL语句中使用了联接查询,在添加和更新操作之后,外键联接的表就不能实时更新了,只能更新子表的记录集。主要代码:
Private Rs As New ADODB.Recordset
Private Sub Command3_Click()
Rs("Brand") = Brand.Text
Rs("Model") = Model.Text
Rs("Factory") = Factory.Text
Rs("OwnerID") = OwnerID.BoundText
Rs.Update
MsgBox Rs("ownername")
AutoList.Refresh
MsgBox "编辑完成", vbOKOnly + vbExclamation, "完成"
End Sub
Private Sub Form_Load()
OpenConn
Command2.Enabled = False
Command3.Enabled = False
Dim Rs_auto As New ADODB.Recordset
Dim Sql As String
'绑定下拉
Sql = "SELECT * FROM auto_Owner ORDER BY OwnerID"
Rs_auto.Open Sql, Conn, 1, 1
Set OwnerID.RowSource = Rs_auto
OwnerID.ListField = "OwnerName"
OwnerID.BoundColumn = "OwnerID"
'绑定DataGrid
Sql = "SELECT auto_Auto.AutoID, auto_Owner.OwnerName, auto_Auto.Brand, auto_Auto.Factory, auto_Auto.Model, auto_Auto.OwnerID FROM auto_Auto INNER JOIN auto_Owner ON auto_Auto.OwnerID = auto_Owner.OwnerID"
Rs.Open Sql, Conn, 1, 3
Set AutoList.DataSource = Rs
End Sub
其中OwnerID是一个Datacombo,关联的另一个表,就是这个关联值,每次更改之后不能马上反映到记录集的更新上。 --------------------编程问答-------------------- 多表查询的可更新记录集:
Private Sub Form_Load()
objRecordset.ActiveConnection = objConnection
'ADO connection 对象
objRecordset.CursorLocation = adUseClient
' 必须使用 client-side 游标
objRecordset.CursorType = adOpenStatic
' client-sided server 游标必须使用此类型
objRecordset.LockType = adLockBatchOptimistic
'Northwind 数据库
objRecordset.Open "SELECT * FROM Customers JOIN Orders ON " & _
"Customers.CustomerID = Orders.CustomerID WHERE city = " & _
"'London' ORDER BY CustomerID"
objRecordset.Properties("Unique Table").Value = "Orders"
objRecordset.Properties("Resync Command").Value = "SELECT * FROM " & _
"(SELECT * FROM Customers JOIN Orders ON Customers.CustomerID = " & _
"Orders.CustomerID WHERE city = 'London' ORDER BY CustomerID) " & _
"WHERE Orders.OrderID = ?"
objRecordset.ActiveConnection = Nothing
'disconnected ADO recordset
Set grdTest.Datasource = objRecordset
End Sub
Private Sub Save()
objRecordset.ActiveConnection = objConnection
' 仅为保存目的重新连接
objRecordset.UpdateBatch
' 不要忘记检查 ADO 错误集合
objRecordset.ActiveConnection = Nothing
' 再断开
End Sub
补充:VB , 基础类