一对多的保存问题?多谢了
如一个客户表和地址表是一对多的关系,如录完成客户名,性别和三个地址(市,区,街,门号)后如何保存?多谢了!最好一个事务中或存储过程中
[解决办法]
如一个客户表和地址表是一对多的关系,如录完成客户名,性别和三个地址(市,区,街,门号)后如何保存?多谢了!最好一个事务中或存储过程中
On Error Goto ErrorHandler
cn.BeginTrans
cn.Execute "Insert Into 客户表(ID,客户名,性别) Values( " & txtID & ", ' " & txtName & " ', ' " & cboSex & " ' "
cn.Execute "Insert Into 地址表(ID,市,区,街,门号) Values( " & txtID & ", ' " & txtCity(0) & " ', ' " & txtDist(0) & " ', ' " & txtStrt(0) & " ', ' " & txtNum(0) & " ' "
cn.Execute "Insert Into 地址表(ID,市,区,街,门号) Values( " & txtID & ", ' " & txtCity(1) & " ', ' " & txtDist(1) & " ', ' " & txtStrt(1) & " ', ' " & txtNum(1) & " ' "
cn.Execute "Insert Into 地址表(ID,市,区,街,门号) Values( " & txtID & ", ' " & txtCity(2) & " ', ' " & txtDist(2) & " ', ' " & txtStrt(2) & " ', ' " & txtNum(2) & " ' "
cn.CommitTrans
Exit Sub
ErrorHandler:
cn.RollbackTrans
[解决办法]
A_table 是 客户表、B_table 是地址表
Dim Con As ADODB.Connection
Dim rs As ADODB.Recordset
Dim rs_db As ADODB.Recordset
'添加A_table记录
Private Sub Command1_Click()
你试试这样做,引用ado
Set Con = New ADODB.Connection
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & App.Path & "\计划管理系统.mdb;Persist Security Info=False "
Con.Open strCon
Set rs = New ADODB.Recordset
strsql= "select * from A_table "
rs.Open strsql, Con, adOpenKeyset, adLockOptimistic
rs.addnew
rs!字段1=text1.text
rs!字段2=text2.text
rs.update
text10.text=rs(A_id)
rs.close
set rs=nothing
end sub
'添加B_table记录
Private Sub Command1_Click()
Set Con = New ADODB.Connection
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & App.Path & "\计划管理系统.mdb;Persist Security Info=False "
Con.Open strCon
Set rs_db = New ADODB.Recordset
strsql_db= "select * from B_table "
rs_db.Open strsql_db, Con, adOpenKeyset, adLockOptimistic
rs_db.addnew
rs_db!字段1=text3.text
rs_db.fields(A_id)=text10.text
rs_db!字段2=text4.text
rs_db.update
rs_db.close
set rs_db=nothing
end sub