Does this code count how many records..

bfsog

Well-known member
Joined
Apr 21, 2005
Messages
50
Location
UK
Programming Experience
5-10
My table can only have a set number of records (in this case 25), so I am trying to count how many rows there are in a table.

Does this code do that?

VB.NET:
 Me.con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Application.StartupPath & "\users.mdb"
 dta = New OleDbDataAdapter("Select * from Users", con)
 dst = New DataSet
 dta.Fill(dst, "Users")
 con.Close()
 If Me.dst.Tables("Users").Rows.Count >= 25 Then
	  MessageBox.Show("No room to add")
Else
	  MessageBox.Show("room to add")
End If

Thanks for help
 
The code looks sound. If you only want the number of rows and not the data itself, you can use this:
VB.NET:
Dim countCommand As New OleDbCommand("SELECT COUNT(*) FROM Users", con)

Try
	con.Open()

	If countCommand.ExecuteScalar() >= 25 Then
		MessageBox.Show("No room")
	Else
		MessageBox.Show("Room")
	End If
Catch
	'...
Finally
	con.Close()
End Try
 
Btw, you can still use ds to find the records number:


VB.NET:
strSQL = "SELECT * FROM Users"
 
Dim cmd As New OleDbCommand(strSQL, oledbcon)
 
da = New OleDbDataAdapter(cmd)
 
ds = New DataSet
 
da.Fill(ds, "Users")
 
If BindingContext(ds, "Users").Position >= 25 Then
 
MessageBox.Show("No room to add")
Exit Sub
 
Else
	 MessageBox.Show("room to add")
End If


Cheers :)
 
Back
Top