qq_34007813
qq_34007813
采纳率66.7%
2018-06-27 11:21

求大佬解答,vb + access数据库读写不正常,在线等,急急急

5
已采纳

如题,本人最近在做一个课设,用vb和access设计一个无纸化考试系统,我事先定义了几个数组用于存储从数据库里读出的数据,用rs.move方法循环按行读取。读取第一次时,数据正常读出,读取第二次时,程序崩溃,不知为何,程序逻辑是正确的,求教,急急急。数据库用的Access2016,贴入问题代码。

Private Sub Cache_all_item()
Dim i As Byte
Dim CurPos As Long, NextPos As Long
Dim strCon As String
Dim sql As String
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Set con = New ADODB.Connection
Set rs = New ADODB.Recordset
strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & App.Path & "\Database1.accdb;Persist Security Info=False"
sql = "select * from sheet1"
con.ConnectionString = strCon
con.Open
rs.Open sql, con, adOpenKeyset, adLockReadOnly
rs.MoveFirst

For i = 0 To 9 Step 1
CurPos = rs.AbsolutePosition
NextPos = randarr(i)
If rs.EOF = False Or rs.BOF = False Then
If NextPos > CurPos Then
rs.Move (NextPos - CurPos), CurPos
Else
rs.Move (NextPos - CurPos), CurPos
End If

Cache_rubric(i) = rs(1)
Cache_A(i) = rs(2)
Cache_B(i) = rs(3)
Cache_C(i) = rs(4)
Cache_D(i) = rs(5)
End If
Next i

con.Close
rs.Close
Set con = Nothing
Set rs = Nothing
End Sub


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • caozhy 从今以后生命中的每一秒都属于我爱的人 3年前

    randarr在哪里定义的,有没有下标越界,
    rs.Move (NextPos - CurPos), CurPos
    这里Move过去的游标是有效的么,调试下。

    点赞 1 评论 复制链接分享
  • qzzxh qzzxh 3年前

    do while not rs.eof

    Cache_rubric(i) = rs(1)
    Cache_A(i) = rs(2)
    Cache_B(i) = rs(3)
    Cache_C(i) = rs(4)
    Cache_D(i) = rs(5)
    rs.movenext
    loop

    试下这个

    点赞 1 评论 复制链接分享

相关推荐