recordset对象的recordcount属性的返回值不准确

lyhfox 2000-06-19 11:24:00
dim mreco as recordset
dim mdatabase as database
dim i as long

set mreco=mdatabase.openrecordset("mytablename",dbopentable)
i=mreco.recordcount

通过access打开数据库,发现表mytablename里的记录总数和 i 值不等。
请问各位大虾:recordcount属性为啥不准确?而有的表记录数准确。
...全文
178 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
forest 2000-06-20
  • 打赏
  • 举报
回复
recordcount是RecordSet对象处理过的记录数。
zzj_mjz 2000-06-20
  • 打赏
  • 举报
回复
要先执行movelast,把整个recordset都读一遍,recordcount才能正确.
mafangsan 2000-06-20
  • 打赏
  • 举报
回复
dim mreco as recordset
dim mdatabase as database
dim i as long

set mreco=mdatabase.openrecordset("mytablename",dbopentable)
mreco.MoveLast
mreco.MoveFirst
i=mreco.recordcount

试一试,应该i就是准确的了
mxp 2000-06-20
  • 打赏
  • 举报
回复
用Sql语句吧!recordcount问题很多的,比如没有记录还要mreco.movelast就要出错,
就要用一个if not mreco.bof or not mreco.eof then
mreco.movelast
i=mreco.recordcount
end if
或者
if mreco.recordcount<>0 then
mreco.movelast
i=mreco.recordcount
end if
多麻烦!

一个sql语句就搞定了

set mreco=mdatabase.openrecordset("select count(*) as mrecomax from mytablename",dbopentable)
i= mreco!mrecomax


Limu 2000-06-20
  • 打赏
  • 举报
回复
不是所有的Recordset都能用MoveLast后讀出Recordcount,有些查詢語句﹐比如多表聯合查詢將得不到RecordCount(永遠是-1)
很不错的哦,很好的Option Explicit Public gUserName As String Public gUserKind As String Public gLoginSucceeded As Boolean ' ****************************************************************************** '过程名:Main '说 明:系统启动函数 '参 数:无 '返回值:无 ' ****************************************************************************** Sub Main() '启动登陆窗体 Dim fLogin As New frmLogin fLogin.Show vbModal If Not gLoginSucceeded Then MsgBox "系统启动失败,请重试!", vbOKOnly + vbExclamation, "警告" End If Unload fLogin End Sub ' ****************************************************************************** '函数名:ConnectString '说 明:设置数据库连接字符串,连接数据库前要首先通过ODBC建立文件DSN:house.dsn '参 数:无 '返回值:数据库连接字符串 ' ****************************************************************************** Public Function ConnectString() As String ConnectString = "Provider=SQLOLEDB.1;Password=sa;User ID=sa;Initial Catalog=DBHouse;Data Source=1D5C3B643D354AB;" End Function ' ****************************************************************************** '函数名:ExecuteSQL '说 明:执行SQL语句 '参 数:SQL As String, rst As ADODB.Recordset, Optional enableWrite As Boolean '返回值:SQL语句执行成功——true,失败——false ' ****************************************************************************** Public Function ExecuteSQL(ByVal SQL As String, rst As ADODB.Recordset, _ Optional enableWrite As Boolean = True) As Boolean Dim con As ADODB.Connection Dim sTokens() As String On Error GoTo Execute_Error sTokens = Split(SQL) Set con = New ADODB.Connection con.Open ConnectString Set rst = New ADODB.Recordset If enableWrite Then rst.Open Trim$(SQL), con, adOpenStatic, adLockOptimistic Else rst.Open Trim$(SQL), con, adOpenStatic, adLockReadOnly End If ExecuteSQL = True Exit Function Execute_Error: ExecuteSQL = False Exit Function End Function ' ****************************************************************************** '函数名:DBExist '说 明:判断数据库中是否存在记录 '参 数:SQL As String '返回值:存在则返回记录数,不存在返回0 ' ****************************************************************************** Public Function DBExist(ByVal SQL As String) As Integer Dim con As ADODB.Connection Dim sTokens() As String Dim flag As String Dim rst As ADODB.Recordset sTokens = Split(SQL) Set con = New ADODB.Connection con.Open ConnectString flag = ExecuteSQL(SQL, rst, False) '判断该记录是否存在 If rst.RecordCount 0 Then DBExist = rst.RecordCount Else DBExist = 0 End If con.Close End Function ' ****************************************************************************** '函数名:TxtIsNull '说 明:判断输入内容是否为空 '参 数:text As TextBox '返回值:存在——true,不存在——false ' ****************************************************************************** Public Function TxtIsNull(txt As TextBox) As Boolean If Trim(txt.Text) = "" Then TxtIsNull = True txt.SetFocus txt.BackColor = &HFF0000 Else TxtIsNull = False End If End Function ' ****************************************************************************** '函数名:IsOverStringLen '说 明:判断输入内容是否超过允许最大值lenthText '参 数:str As String, lenthText As Integer '返回值:不超过——true,超过——false ' ****************************************************************************** Public Function IsOverStringLen(ByVal str As String, lenthText As Integer) As Boolean If Len(Trim(str)) > lenthText Then IsOverStringLen = True Else IsOverStringLen = False End If End Function ' ****************************************************************************** '过程名:viewData '说 明:将数据在datagrid中显示 '参 数:txtSql as String,dataGridAll As DataGrid '返回值:存在则返回记录数,不存在返回0 ' ****************************************************************************** Public Function viewData(ByVal txtSql As String, dataGridAll As DataGrid) As Integer Dim rstData As ADODB.Recordset Dim result As String '检索需要的信息 result = ExecuteSQL(txtSql, rstData, False) '设置datagrid的数据源 If rstData.RecordCount 0 Then Set dataGridAll.DataSource = rstData viewData = rstData.RecordCount Else MsgBox "还没有数据!", vbOKOnly + vbExclamation, "警告" viewData = 0 End If End Function ' ****************************************************************************** '过程名:ISEquelLen '说 明:判断文本框中内容是否等于给定的长度 '参 数:txt As TextBox, intlen As Integer '返回值:超过返回值为True,否则为false ' ****************************************************************************** Public Function ISEquelLen(ByVal txt As TextBox, intlen As Integer) As Boolean If Len(txt.Text) intlen Then txt.SetFocus txt.BackColor = &HFF0000 ISEquelLen = False Else ISEquelLen = True End If End Function ' ****************************************************************************** '过程名:ComboData '说 明:为Combo赋值 '参 数:txt as String,cmb as ComboBox '返回值:存在记录值返回true,否则返回false ' ******************************************************************************v Public Function ComboData(ByVal txt As String, cbo As ComboBox) As Boolean Dim res As String Dim rstcbo As ADODB.Recordset Dim i As Integer res = ExecuteSQL(txt, rstcbo, False) If rstcbo.RecordCount 0 Then For i = 0 To rstcbo.RecordCount - 1 cbo.AddItem (rstcbo.Fields(1)) cbo.ItemData(cbo.NewIndex) = rstcbo.Fields(0) rstcbo.MoveNext Next ComboData = True Else ComboData = False End If End Function ' ****************************************************************************** '过程名:ComboYear '说 明:为年份列表赋值 '参 数:Combo as Combobox '返回值:无 ' ******************************************************************************v Public Sub ComboYear(Combo As ComboBox) Dim i As Integer For i = 1990 To 2050 Combo.AddItem (CStr(i)) Next End Sub ' ****************************************************************************** '过程名:ComboMonth '说 明:为月份列表赋值 '参 数:Combo as Combobox '返回值:无 ' ******************************************************************************v Public Sub ComboMonth(Combo As ComboBox) Dim i As Integer For i = 1 To 12 If Len(CStr(i)) 2 Then Combo.AddItem ("0" + CStr(i)) Else Combo.AddItem (CStr(i)) End If Next End Sub ' ****************************************************************************** '过程名:ComboDate '说 明:为日期列表赋值 '参 数:Combo as Combobox '返回值:无 ' ******************************************************************************v Public Sub ComboDate(Combo As ComboBox) Dim i As Integer For i = 1 To 31 If Len(CStr(i)) 2 Then Combo.AddItem ("0" + CStr(i)) Else Combo.AddItem (CStr(i)) End If Next End Sub ' ****************************************************************************** '过程名:ComboHour '说 明:为小时列表赋值 '参 数:Combo as Combobox '返回值:无 ' ******************************************************************************v Public Sub ComboHour(Combo As ComboBox) Dim i As Integer For i = 0 To 24 If Len(CStr(i)) 2 Then Combo.AddItem ("0" + CStr(i)) Else Combo.AddItem (CStr(i)) End If Next End Sub ' ****************************************************************************** '过程名:ComboMin '说 明:为分钟列表赋值 '参 数:Combo as Combobox '返回值:无 ' ******************************************************************************v Public Sub ComboMin(Combo As ComboBox) Dim i As Integer For i = 0 To 60 If Len(CStr(i)) 2 Then Combo.AddItem ("0" + CStr(i)) Else Combo.AddItem (CStr(i)) End If Next End Sub ' ****************************************************************************** '过程名:setCboDataReg() '说 明:为cboReg赋值 '参 数:无 '返回值:无 ' ******************************************************************************v Public Sub setCboDataReg(Combo As ComboBox) Dim txtReg As String Dim resReg As String txtReg = "select RegId,RegName from tbRegion" resReg = ComboData(txtReg, Combo) End Sub ' ****************************************************************************** '过程名:setCboDataItem '说 明:为cboItem赋值 '参 数:无 '返回值:无 ' ******************************************************************************v Public Sub setCboDataItem(Combo As ComboBox) Dim txtItem As String Dim resItem As String txtItem = "select ItemId,ItemName from tbItem" resItem = ComboData(txtItem, Combo) End Sub ' ****************************************************************************** '过程名:setCboDataStru() '说 明:为cboStru赋值 '参 数:无 '返回值:无 ' ******************************************************************************v Public Sub setCboDataStru(Combo As ComboBox) Dim txtStru As String Dim resStru As String txtStru = "select StruId,StruName from tbStru" resStru = ComboData(txtStru, Combo) End Sub ' ****************************************************************************** '过程名:getCboRegId() '说 明:获取显示检索部分区域名称的ComboBox所对应的Id值 '参 数:无 '返回值:无 ' ****************************************************************************** Public Sub getCboId(Combo As ComboBox, str As String) If Combo.Text = "" Then str = "" Else str = CStr(Combo.ItemData(Combo.ListIndex)) If Len(str) 5 Then Select Case Len(str) Case 4 str = "0" + str Case 3 str = "00" + str Case 2 str = "000" + str Case 1 str = "0000" + str End Select End If End If End Sub ' ****************************************************************************** '过程名:setCboIdData '说 明:为Combo赋值 '参 数:txt as String,cmb as ComboBox '返回值:存在记录值返回true,否则返回false ' ******************************************************************************v Public Sub setCboIdData(cbo As ComboBox) Dim res As String Dim rstcbo As ADODB.Recordset Dim i As Integer Dim strSQL As String strSQL = "select HouseId from tbHouse" res = ExecuteSQL(strSQL, rstcbo, False) If rstcbo.RecordCount 0 Then For i = 0 To rstcbo.RecordCount - 1 cbo.AddItem (rstcbo.Fields(0)) rstcbo.MoveNext Next End If End Sub ' ****************************************************************************** '过程名:setLabelData '说 明:为Combo赋值 '参 数:txt as String,cmb as ComboBox '返回值:存在记录值返回true,否则返回false ' ******************************************************************************v Public Sub setLabelData(cbo As ComboBox, lbl As Label) Dim res As String Dim rstcbo As ADODB.Recordset Dim strSQL As String strSQL = "select HouseName from tbHouse where HouseId='" + Trim(cbo.Text) + "'" res = ExecuteSQL(strSQL, rstcbo, False) If rstcbo.RecordCount 0 Then lbl.Caption = rstcbo.Fields(0) End If End Sub

7,762

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧