-
Jan 29th, 2012, 05:32 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] Display Record Count in a label
Hello,
How to i display how many records that i have in a label on my main form for example if i had 3 table called Cusotmers, Orders, Products
How do i get it to show the total amount of each table on label 1 label 2 label 3?
come back and mark your original post as resoved if your problem is fixed
Jamie Garland
-
Jan 29th, 2012, 06:39 PM
#2
Addicted Member
Re: Display Record Count in a label
first, search in your books or in google how to perform a query like a
"select count(*) from Cusotmers" via Ado.Net (I do believe you do not want to getinvolved with Entity Framework or the like right now, do you?)
Once you're able to peform that query and get back a result, assigning it to the text property of a label should not be an issue for you anymore.
Special thanks to some wonderful people,
such as Lothar the Great Haensler, Aaron Young,
dr_Michael, Chris Eastwood, TheOnlyOne ClearCode....
-
Jan 29th, 2012, 07:59 PM
#3
Thread Starter
Frenzied Member
Re: Display Record Count in a label
i use oledb this is my connention
code Code:
Module modConnection
Public conn As New System.Data.OleDb.OleDbConnection
Public strConnString As String
Public Sub connectDatabase()
strConnString = "PROVIDER=microsoft.Jet.OleDb.4.0;Data Source=data.mdb"
conn.ConnectionString = strConnString
conn.Open()
End Sub
Public Sub DisconnectDatabase()
If conn.State = ConnectionState.Open Then
conn.Close()
Else
'continue
End If
End Sub
Public Function getDate() As String
Return Format(Date.Today, "Short Date")
End Function
Public Function getTime() As String
Return Format(Date.Now, "Long Time")
End Function
End Module
come back and mark your original post as resoved if your problem is fixed
Jamie Garland
-
Jan 29th, 2012, 09:40 PM
#4
Re: Display Record Count in a label
Do you want the actual data as well or just the count? If it's the former, do you want it to update when the user adds or deletes rows?
-
Jan 29th, 2012, 09:45 PM
#5
Re: Display Record Count in a label
By the way, don;t do this:
Code:
Public Function getDate() As String
Return Format(Date.Today, "Short Date")
End Function
Public Function getTime() As String
Return Format(Date.Now, "Long Time")
End Function
For a start, you are using inconsistent naming conventions. You have some methods using camel casing and some using Pascal casing. Method names should always use Pascal casing.
Secondly, Date.Today actually uses Date.Now internally any way so there's no point using Date.Today when formatting a date string.
Finally, don't use the Format function in VB.NET. If you want a specific format then call ToString on the Date itself and pass the format string, otherwise use one of the specific methods dedicated to the four standard formats.
Code:
Public Function GetDate() As String
Return Date.Now.ToShortDateString()
End Function
Public Function GetTime() As String
Return Date.Now.ToLongTimeString()
End Function
-
Jan 30th, 2012, 05:20 AM
#6
Thread Starter
Frenzied Member
Re: Display Record Count in a label
I only want to show the ID total for how many records and when people update the database it changes from 5 to 6 and when deletes a record from 6 - 5?.
come back and mark your original post as resoved if your problem is fixed
Jamie Garland
-
Jan 30th, 2012, 06:48 PM
#7
Thread Starter
Frenzied Member
Re: Display Record Count in a label
come back and mark your original post as resoved if your problem is fixed
Jamie Garland
-
Jan 30th, 2012, 07:31 PM
#8
Re: Display Record Count in a label
Originally Posted by Jamie_Garland
Can anyone help?.
Please don't make useless posts like this. We're all around the world in different time zones. We'll get to your question if and when we can.
-
Jan 30th, 2012, 07:33 PM
#9
Re: Display Record Count in a label
Originally Posted by Jamie_Garland
I only want to show the ID total for how many records and when people update the database it changes from 5 to 6 and when deletes a record from 6 - 5?.
This begs another question. Let's say that you and I both have your application on different machines. If I make a change to the database, are you saying that you want your instance of the app to reflect that, or are you saying atht each instance of the app only reflects changes that it makes itself?
-
Jan 30th, 2012, 07:34 PM
#10
Thread Starter
Frenzied Member
Re: Display Record Count in a label
Oh i'm sorry didn't mean you bump my post.
come back and mark your original post as resoved if your problem is fixed
Jamie Garland
-
Jan 30th, 2012, 07:37 PM
#11
Thread Starter
Frenzied Member
Re: Display Record Count in a label
What i want it to do is when i add a new customer to my database once he has been added successfully and i close the form. I want the count to go up from 1 to 2 on frmMain like refresh the page once the data has been saved?.
So it displays something like this,
You currently have 4 customers and once record is added it then changes to you currently have 5 customers and so on?.
I'm using a Msaccess database for this project I only want it to show on my instance but if i send you the updated database it then shows on your instance also as the database has been updated?.
Last edited by Jamie_Garland; Jan 30th, 2012 at 07:40 PM.
come back and mark your original post as resoved if your problem is fixed
Jamie Garland
-
Jan 30th, 2012, 08:28 PM
#12
Re: Display Record Count in a label
Follow the CodeBank link in my signature and check out my thread on Retrieving & Saving Data. One of the examples is for retrieving a single value. It uses the SUM function but you can use COUNT. Just execute such a query each time you want to update the Label, e.g. when the main form loads and whenever any adding or deleting is or may be done.
-
Jan 30th, 2012, 08:31 PM
#13
Thread Starter
Frenzied Member
Re: Display Record Count in a label
come back and mark your original post as resoved if your problem is fixed
Jamie Garland
-
Jan 31st, 2012, 05:16 PM
#14
Thread Starter
Frenzied Member
Re: Display Record Count in a label
Hello,
I tried this code to display the count of all the records but i keep getting an error like this.
ERROR:
Invalid SQL statement; expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'.
it also highlights this
dr = cmd.ExecuteReader()
code Code:
Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strCnn As String = "PROVIDER=microsoft.Jet.OleDb.4.0;Data Source=Data\data.mdb"
Dim strSql As String = "SELECT * FROM tblCustomers"
Dim cnn As New OleDbConnection(strCnn)
Dim cmd As New OleDbCommand(strCnn, cnn)
Dim dr As OleDbDataReader
cnn.Open()
dr = cmd.ExecuteReader()
While (dr.Read())
Label1.Text = (dr(0).ToString())
End While
End Sub
come back and mark your original post as resoved if your problem is fixed
Jamie Garland
-
Jan 31st, 2012, 05:27 PM
#15
Re: Display Record Count in a label
Jamie, you're not the only one unfortunately but, seriously, did you even read what I posted? I said SPECIFICALLY to check out the example for retrieving a single value. You have not done that. If you can't read what's posted and follow simple instructions then how can you expect the correct result? That's not actually the cause of that error but you might want to start with code that at least attempts to do the right thing, which that does not. Read post #12 and do what it says.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|