Có phải Recordcount có vấn đề?

  • Thread starter Thread starter vbavn
  • Ngày gửi Ngày gửi
Liên hệ QC

vbavn

Thành viên chính thức
Tham gia
10/1/08
Bài viết
70
Được thích
145
Tôi có dùng thủ tục tương tự như dưới đây nhằm tính số Record, nhưng kết quả không đúng:

Sub RecCounter()
Dim Cust As New ADODB.Recordset
Cust.Open _
"SELECT * FROM Customers", _
"Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data source=C:\ADO\QuanLy.mdb;"
Debug.Print Cust.RecordCount
End Sub
Xin các bạn trợ giúp

Vbavn
 
Chỉnh sửa lần cuối bởi điều hành viên:
vbavn đã viết:
Tôi có dùng thủ tục tương tự như dưới đây nhằm tính số Record, nhưng kết quả không đúng:
PHP:
Sub RecCounter()
Dim Cust As New ADODB.Recordset
Cust.Open _
"SELECT * FROM Customers", _
"Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data source=C:\ADO\QuanLy.mdb;"
Debug.Print Cust.RecordCount
End Sub
Xin các bạn trợ giúp

Vbavn
Anh sửa lại thành:

PHP:
Sub RecCounter()
Dim Cust As New ADODB.Recordset
Cust.Open "SELECT * FROM Customers", "Provider=Microsoft.Jet.OLEDB.4.0; " & "Data source=C:\ADO\QuanLy.mdb;",1,1
Debug.Print Cust.RecordCount
End Sub

Thêm:
ADO RecordCount Property

  • The RecordCount property returns a long value that indicates the number of records in a Recordset object.
  • If the Recordset object supports AbsolutePosition and AbsolutePage properties or bookmarks (if Supports(adApproxPosition) or Supports(adBookmark) returns true), this property will return the exact number of records in the Recordset.
  • Note: This property will return -1 for a forward-only cursor; the actual count for a static or keyset cursor; and -1 or the actual count for a dynamic cursor.
  • Note: The Recordset object must be open when calling this property. If this property is not supported it will return -1.
ADO CursorType Property

The CursorType property sets or returns the cursor type to use when opening a Recordset object. This property can take a CursorTypeEnum value. Default is adOpenForwardOnly.
Note: If the CursorLocation property is set to adUseClient, the only valid setting for the CursorType property is adOpenStatic.
Note: No error will occur if an unsupported value is set, the provider will just change to a supported CursorType instead.

CursorTypeEnum Values
  • adOpenUnspecified (-1): Does not specify the type of cursor.
  • adOpenForwardOnly (0 - Default): Uses a forward-only cursor. Identical to a static cursor, except that you can only scroll forward through records. This improves performance when you need to make only one pass through a Recordset.
  • adOpenKeyset (1) : Uses a keyset cursor. Like a dynamic cursor, except that you can't see records that other users add, although records that other users delete are inaccessible from your Recordset. Data changes by other users are still visible.
  • adOpenDynamic (2 ): Uses a dynamic cursor. Additions, changes, and deletions by other users are visible, and all types of movement through the Recordset are allowed, except for bookmarks, if the provider doesn't support them.
  • adOpenStatic (3): Uses a static cursor. A static copy of a set of records that you can use to find data or generate reports. Additions, changes, or deletions by other users are not visible.

Thanh Phong
 
Lần chỉnh sửa cuối:
Thực sự ra tôi muốn đưa ra 2 vấn đề ở đây mà người dùng thường gặp phải khi dùng Recordset:
_ Cusor Type.
_ Tốc độ.
Mã:
1. Cust.Open "Customers", Cnt,,,adCmdTable
2. Cust.Open "Customers", Cnt, Options:=adCmdTable
3. Cust.Open "SELECT * FROM Customers",Cnt
4. Cust.Open "Customers", Cnt
Trong 4 ví dụ trên sẽ cho kết quả là như nhau.
Các bạn chú ý cho trường hợp 1 và 2
Vấn đề Cursor Type, ttphong2007 đã giải thích.
Chính vì vậy ttphong2007 đã đưa ra
Mã:
[COLOR=#000000][FONT=Arial][COLOR=Black][COLOR=#0000BB]Cust[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]Open [/COLOR][COLOR=#DD0000]"SELECT * FROM Customers"[/COLOR][COLOR=#007700], [/COLOR][COLOR=#DD0000]"Provider=Microsoft.Jet.OLEDB.4.0; " [/COLOR][COLOR=#007700]& [/COLOR][COLOR=#DD0000]"Data source=C:\ADO\QuanLy.mdb;"[/COLOR][COLOR=#007700],[/COLOR][COLOR=#0000BB]1[/COLOR][COLOR=#007700],[/COLOR][/COLOR][/FONT][COLOR=#0000BB][FONT=Arial][COLOR=Black]1

Vấn đề tốc độ: thì CursorType: adOpenForwardOnlyadOpenDynamic sẽ nhanh hơn.
Chính vì vậy tùy theo nhu cầu mà chúng ta sẽ sử dụng CursorType nào cho hợp lý.

Vbavn

[/COLOR][/FONT]
[/COLOR][/COLOR]
 
Web KT

Bài viết mới nhất

Back
Top Bottom