尼莫和可爱多
2017-12-29 03:04
采纳率: 100%
浏览 1.1k
已采纳

VB记录集问题,大神进。急急急!

Private Sub Opt1_Click()
Dim cn As New ADODB.Connection '定义数据库的连接
Dim rs As New ADODB.Recordset
Dim sql1 As String
Dim sql2 As String
cn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=MZJJSFXT;Data Source=."
cn.Open
sql1 = "select 操作员,工号,窗口号,count(收据号)as 收据数,sum(总金额)as 金额 from 收费单据信息表 where 日期 between '" + Str(DTPDate1.Value) + "'And '" + Str(DTPDate2.Value) + "'group by 操作员,工号,窗口号"
rs.Open sql1, cn, adOpenKeyset, adLockOptimistic
With rs
If rs.RecordCount > 0 Then
MSHGrid1.Rows = MSHGrid1.Rows + rs.RecordCount
Do While rs.EOF = False
r = rs.AbsolutePosition + 1
MSHGrid1.TextMatrix(r, 0) = rs.Fields(0)
MSHGrid1.TextMatrix(r, 1) = rs.Fields(1)
MSHGrid1.TextMatrix(r, 2) = rs.Fields(2)
For c = 3 To 10 Step 2
MSHGrid1.TextMatrix(r, c) = "0"
Debug.Print MSHGrid1.TextMatrix(r, c + 1)
MSHGrid1.TextMatrix(r, c + 1) = "0.00"
Next c
rs.MoveNext
Loop
End If
End With

rs.Close '用后关闭记录集
Set rs = New ADODB.Recordset

 For i = 0 To 2
    s1 = Array("现金", "支票", "计账")

    sql2 = "select 操作员,工号,窗口号,count(收据号)as 收据数,sum(总金额)as 金额 from 收费单据信息表 where 结算方式='" + s1(i) + "' and 日期 between '" + Str(DTPDate1.Value) + "'And '" + Str(DTPDate2.Value) + "'group by 操作员,工号,窗口号"

rs.Open sql2, cn, adOpenKeyset, adLockOptimistic
With rs
If rs.RecordCount > 0 Then
For r = 2 To MSHGrid1.Rows - 1
rs.MoveFirst
Do While rs.EOF = False
If rs.Fields(0) = MSHGrid1.TextMatrix(r, 0) Then
MSHGrid1.TextMatrix(r, 2 * i + 3) = rs.Fields(3)
MSHGrid1.TextMatrix(r, 2 * i + 4) = Format(rs.Fields(4), "0.00")
Exit Do
End If
rs.MoveNext
Loop
Next r
End If
End With
Next i
For i = 2 To MSHGrid1.Rows - 1
MSHGrid1.TextMatrix(i, 9) = Val(MSHGrid1.TextMatrix(i, 3)) + Val(MSHGrid1.TextMatrix(i, 5)) + Val(MSHGrid1.TextMatrix(i, 7))
MSHGrid1.TextMatrix(i, 10) = Format(Val(MSHGrid1.TextMatrix(i, 4)) + Val(MSHGrid1.TextMatrix(i, 6)) + Val(MSHGrid1.TextMatrix(i, 8)), "0.00")
Next i

rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing

MSHGrid1.Visible = True
MSHGrid2.Visible = False
tj1 = True
End Sub
一直显示对象已打开无法进行操作这是怎么回事,我明明上一个已经关了。求大神解答

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • 已采纳

    rs.Open sql1, cn, adOpenKeyset, adLockOptimistic 和 rs.Open sql1, cn, adOpenKeyset, adLockOptimistic前面加上
    if rs.state=adstateopen then rs.close
    rs.CursorLocation=adUseClient

    点赞 打赏 评论

相关推荐