下面是我的一段程序,不知道是什么原因rst.Recordcount=-1.
'保存ID号和时间
 '1.连接数据库
   Dim cnn As ADODB.Connection
   Dim rst As ADODB.Recordset
   Dim strcnn, SQL As String
   Dim i As Integer
   Dim CurrentDBTime As Date
   TempTime = 10
   MousePointer = vbHourglass
   strcnn = "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=Sa;Password=;Initial Catalog=rfid;Data Source=front"
   Set cnn = New ADODB.Connection
   Set rst = New ADODB.Recordset
   cnn.Open strcnn
   MousePointer = vbDefault
   SQL = "Select * From memberinfo "
   cnn.Execute (SQL)
   rst.Open SQL, cnn, adOpenDynamic, adLockBatchOptimistic
 '2访问数据库
   If rst.EOF = False Then
      For i = 0 To rst.RecordCount
         CurrentID = rst.Fields("bh").Value
         CurrentDBTime = rst.Fields("qdsj").Value
      If Communication.GetCurrentTagid(nCounter) = CurrentID And DateDiff("s", Trim(CurrentDBTime), CurrentSysTime) >= TempTime Then
         rst.AddNew
         rst.Fields("bh") = Communication.GetCurrentTagid(nCounter)
         rst.Fields("qdsj") = CurrentSysTime
         rst.Update
         rst.MoveFirst
      End If
      Next i
   End If

解决方案 »

  1.   

    因为你没有设置游标的类型:
    rst.CursorLocation = adUseClient
    rst.open ..............................
    msgbox rst.RecordCount
      

  2.   

    把游标设置为客户端游标类型:.....
    Set rst = New ADODB.Recordset
    cnn.CursorLocation = adUseClient
    cnn.Open strcnn
    ......
      

  3.   

    比如在数据库中的table中有一个记录的一个"bh"字段是0001,现在同一个记录即字段"bh"也是0001的记录要保存在table中.怎么办?
      

  4.   

    打开参数修改成:rst.Open SQL, cnn,adOpenKeyset, adLockBatchOptimistic
    或者作循环的时候:
    do while not Rst.eof
    rst.movenext
    loop
      

  5.   

    你也可以这样设置
    把 rst.Open SQL, cnn, adOpenDynamic, adLockBatchOptimistic
    的adOpenDynamic改成OpenKeyset就可以了