Results 1 to 15 of 15

Thread: [RESOLVED] Display Record Count in a label

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    Resolved [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

  2. #2
    Addicted Member Cimperiali's Avatar
    Join Date
    Oct 2002
    Location
    Milan, Italy, Europe
    Posts
    188

    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....

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    Re: Display Record Count in a label

    i use oledb this is my connention

    code Code:
    1. Module modConnection
    2.  
    3.     Public conn As New System.Data.OleDb.OleDbConnection
    4.     Public strConnString As String
    5.  
    6.     Public Sub connectDatabase()
    7.         strConnString = "PROVIDER=microsoft.Jet.OleDb.4.0;Data Source=data.mdb"
    8.         conn.ConnectionString = strConnString
    9.         conn.Open()
    10.     End Sub
    11.     Public Sub DisconnectDatabase()
    12.         If conn.State = ConnectionState.Open Then
    13.             conn.Close()
    14.         Else
    15.             'continue
    16.         End If
    17.     End Sub
    18.  
    19.     Public Function getDate() As String
    20.         Return Format(Date.Today, "Short Date")
    21.     End Function
    22.  
    23.     Public Function getTime() As String
    24.         Return Format(Date.Now, "Long Time")
    25.     End Function
    26. End Module
    come back and mark your original post as resoved if your problem is fixed

    Jamie Garland

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    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?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    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

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    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

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    Re: Display Record Count in a label

    Can anyone help?.
    come back and mark your original post as resoved if your problem is fixed

    Jamie Garland

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: Display Record Count in a label

    Quote Originally Posted by Jamie_Garland View Post
    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.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: Display Record Count in a label

    Quote Originally Posted by Jamie_Garland View Post
    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?

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    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

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    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

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    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.

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    Re: Display Record Count in a label

    Is it parameters one?.
    come back and mark your original post as resoved if your problem is fixed

    Jamie Garland

  14. #14

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    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:
    1. Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    2.  
    3.         Dim strCnn As String = "PROVIDER=microsoft.Jet.OleDb.4.0;Data Source=Data\data.mdb"
    4.         Dim strSql As String = "SELECT * FROM tblCustomers"
    5.         Dim cnn As New OleDbConnection(strCnn)
    6.         Dim cmd As New OleDbCommand(strCnn, cnn)
    7.         Dim dr As OleDbDataReader
    8.         cnn.Open()
    9.         dr = cmd.ExecuteReader()
    10.         While (dr.Read())
    11.             Label1.Text = (dr(0).ToString())
    12.         End While
    13.     End Sub
    come back and mark your original post as resoved if your problem is fixed

    Jamie Garland

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    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
  •  



Click Here to Expand Forum to Full Width