问题描述
|
在我的vb6中,关闭对象时,我收到错误3704的操作不允许。
我在搜索stackoverflow中遇到了类似的问题,但是我想缺少一些东西。我需要根据记录集rs1更新vfp中的每一行,这是我的代码:
Option Explicit
Dim cn As New ADODB.Connection
Dim cn1 As New ADODB.Connection
Private Sub trns_Click()
Set cn = New ADODB.Connection
Set cn1 = New ADODB.Connection
cn.ConnectionString = MDI1.txtcn.Text
cn.Open
cn1.ConnectionString = \"Provider=VFPOLEDB;Data Source=\\\\host1\\software\\MIL\\company0\"
cn1.Open
rs1.Open \"Select * from trans\",cn,adOpenStatic,adLockPessimistic
Do While Not rs2.EOF
rs2.Open \"update transac set no_ot_1_5 = \" & rs1.Fields(\"ovt1\") & \",no_ot_2_0 = \" & rs1.Fields(\"ovt2\") & \",no_ot_3_0\" _
& \"= \" & rs1.Fields(\"ovt3\") & \",Meal_allw = \" & rs1.Fields(\"meal_allow\") & \",on_duty = \" & rs1.Fields(\"cnt\") & \",no_d_local = \" & rs1.Fields(\"local\") & \",no_d_sick\" _
& \"= \" & rs1.Fields(\"sick\") & \",no_d_abs = \" & rs1.Fields(\"absence\") & \",no_d_spc = \" & rs1.Fields(\"special\") & \",Revenue02\" _
& \"= \" & rs1.Fields(\"refund\") & \",Revenue05 = \" & rs1.Fields(\"prepay\") & \",Deduct05 = \" & rs1.Fields(\"prepay\") & \",Revenue01 = \" & rs1.Fields(\"comm\") & \"where code = \'\" & rs1.Fields(\"emp_code\") & \"\' and transac.date = CTOD(\'\" & trans.txtend2 & \"\')\",cn1,adOpenDynamic,adLockPessimistic
If Not rs2.EOF Then
rs2.MoveNext
End If
Loop
rs2.close
解决方法
更新查询不返回记录集,因此未打开您的rs2。
, 您在错误的recordeset上执行了循环:我在您的代码中用rs1替换了一些rs2。
Do While Not rs1.EOF
rs2.Open \"update transac set no_ot_1_5 = \" & rs1.Fields(\"ovt1\") & \",no_ot_2_0 = \" & rs1.Fields(\"ovt2\") & \",no_ot_3_0\" _
& \"= \" & rs1.Fields(\"ovt3\") & \",Meal_allw = \" & rs1.Fields(\"meal_allow\") & \",on_duty = \" & rs1.Fields(\"cnt\") & \",no_d_local = \" & rs1.Fields(\"local\") & \",no_d_sick\" _
& \"= \" & rs1.Fields(\"sick\") & \",no_d_abs = \" & rs1.Fields(\"absence\") & \",no_d_spc = \" & rs1.Fields(\"special\") & \",Revenue02\" _
& \"= \" & rs1.Fields(\"refund\") & \",Revenue05 = \" & rs1.Fields(\"prepay\") & \",Deduct05 = \" & rs1.Fields(\"prepay\") & \",Revenue01 = \" & rs1.Fields(\"comm\") & \"where code = \'\" & rs1.Fields(\"emp_code\") & \"\' and transac.date = CTOD(\'\" & trans.txtend2 & \"\')\",cn1,adOpenDynamic,adLockPessimistic
If Not rs1.EOF Then
rs1.MoveNext
End If
Loop
rs1.close
, 您无需创建记录集即可对数据库执行更新,插入或删除操作。只需使用语句cn1.Execute YourSqlStatement,其中YourSqlStatement是您在rs2.Open指令上传递的字符串。连接上的Execute方法可以选择接受byRef变量,您可以在其中获取受影响的记录数。
例:
Dim nRecords As Integer
cn1.Execute \"Update Table Set Field = Value Where AnotherField = SomeValue \",nRecords
MsgBox \"Total Updated Records: \" & Format(nRecords,\"0\")
, 尝试在do while语句中使用if之前先打开rs2,或者这样做
rs2.open \" blah blah blah \"
Do Until rs2.eof
For Each fld In rs2.field
value_holder = fld.value
Next
rs2.movenext
Loop