运行时错误3704

时间:2011-05-30 06:03:31

标签: vb6 adodb

在我的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

4 个答案:

答案 0 :(得分:0)

更新查询不会返回记录集,因此您的rs2未打开。

答案 1 :(得分:0)

您在错误的记录集上执行循环:我在代码中用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

答案 2 :(得分:0)

您不需要创建记录集来在数据库上执行更新,插入或删除。只需使用语句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")

答案 3 :(得分: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