How can i count the no of records fetched in datagrid from admission table and how to show these no of records in a label i.e

Total No of records = 20

Please help

Recommended Answers

All 11 Replies

Dim i As Integer

i = 0
Label1.Caption = i

For i = 0 To rsRec.RecordCount
Label1.Caption = Label1.Caption + 1
rsRec.MoveNext
Next i

I have made a function Loaddata() and write the code provided by you and call the function on form load but it gives me following error
"Either BOF or EOF is true or the current record has been deleted requested operation requires a current record" So please help me to resolve this problem.

Public Sub LoadData()
Set con = New adodb.Connection
Set rs = New adodb.Recordset
con.Open "Provider =Microsoft.Jet.OLEDB.4.0;Data Source=D:\EA\DB\EA.mdb;"
rs.CursorLocation = adUseClient
rs.Open "select StudentID,StudentName,Dob,Class from Admission", con, adOpenDynamic, adOpenStatic
'While rs.EOF = True Or rs.BOF = True
Dim i As Integer
i = 0
lblids.Caption = i
For i = 0 To rs.RecordCount
lblids.Caption = lblids.Caption + 1
rs.MoveNext
Next i
'Wend
Set DataGrid1.DataSource = rs.DataSource
DataGrid1.DataMember = rs.DataMember
End Sub

Try this...

lblids.Caption = lblids.Caption & rs.RecordCount

That id because there is no records to load.

rs.Open "select StudentID,StudentName,Dob,Class from Admission", con, adOpenDynamic, adOpenStatic
If rs.EOF = True Or rs.BOF = True Then
msgbox "No Records"
lblIds.Caption = "0"
Exit Sub
Else
Dim i As Integer
i = 0
lblids.Caption = i
For i = 0 To rs.RecordCount
lblids.Caption = lblids.Caption + 1
rs.MoveNext
Next i
End If

use this->

set rs=new adodb.recordset
rs.cusorlocation=aduseclient
rs.Open "select StudentID,StudentName,Dob,Class from Admission", con, adOpenDynamic, adOpenStatic

if rs.recordcount >0 then
label1.caption="Total Records Found: " & rs.recordcount
else
label1.caption="Total Records Found: 0"
end if

rs.close
set rs=nothing

Now it looks like this it gives no error but now nothing is displayed in datagrid and in lblids.

Public Sub LoadData()
Set con = New adodb.Connection
Set rs = New adodb.Recordset
con.Open "Provider =Microsoft.Jet.OLEDB.4.0;Data Source=D:\EA\DB\EA.mdb;"
rs.CursorLocation = adUseClient
      rs.Open "select StudentID,StudentName,Dob,Class from Admission", con, adOpenDynamic, adOpenStatic
      If rs.EOF = True Or rs.BOF = True Then
      lblids.Caption = "0"
      Exit Sub
      Dim i As Integer
      i = 0
      lblids.Caption = i
      For i = 0 To rs.RecordCount
      lblids.Caption = lblids.Caption + 1
      rs.MoveNext
      Next i
      Set DataGrid1.DataSource = rs.DataSource
      DataGrid1.DataMember = rs.DataMember
      End If
End Sub
If rs.EOF = True Or rs.BOF = True Then
      lblids.Caption = "0"
      Exit Sub
ELSE 'ERROR HERE, add else ..............
      Dim i As Integer
      i = 0
      lblids.Caption = i
      For i = 0 To rs.RecordCount
      lblids.Caption = lblids.Caption + 1
      rs.MoveNext
      Next i
      Set DataGrid1.DataSource = rs.DataSource
      DataGrid1.DataMember = rs.DataMember
      End If

Its working fine but i had only one problem now it is counting the records but it shows one extra records i.e

if there is three records in admission table it shows me 4 please help

Sorry, my bad totally. Change the following -

For i = 0 To rs.RecordCount - 1

Thanks it really helped me.

It's a pleasure.:) Happy coding....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.