Results 1 to 8 of 8

Thread: count rows in DB

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2003
    Posts
    5

    Lightbulb count rows in DB

    heyya i'm pretty new to VB. Doing a small project and would like to know if anyone could help. Is there a way that i can limit the entry into a table (row) ? Say, i only want 10 rows to be occupied, after that a message will prompt user that the allocation is full.

    and how do i do a row count in a table and display the total in the front end?

    thanks!!

  2. #2
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724
    SELECT COUNT(*) FROM TableName

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2003
    Posts
    5

    how to put in textbox?

    i know the count(*) code.. but how do i put the count result into a textbox?

  4. #4
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724
    VB Code:
    1. Public Function getRecordCount(ByVal strTable As String) As Long
    2.   Dim tConn As ADODB.Connection
    3.   Dim rsaGetCount As ADODB.Recordset
    4.   Dim strSQL As String
    5.  
    6.   ' Default the return value.
    7.   getRecordCount = 0
    8.  
    9.   Set tConn = New ADODB.Connection
    10.   tConn.Open [Connection string to your database]
    11.  
    12.   strSQL = "SELECT COUNT(*) AS Cnt FROM " & strTable
    13.   Set rsaGetCount = New ADODB.Recordset
    14.   rsaGetCount.Open strSQL, tConn
    15.  
    16.   getRecordCount = rsaGetCount("Cnt")
    17.  
    18.   ' Clean up.
    19.   On Error Resume Next
    20.   rsaGetCount.Close
    21.   Set rsaGetCount = Nothing
    22.  
    23.   tConn.Close
    24.   Set tConn = Nothing
    25. End Function

    And to get the value into a textbox:
    VB Code:
    1. txtRecords.Text = getRecordCount("YourTableName")

  5. #5
    Fanatic Member khalik_ash's Avatar
    Join Date
    Aug 2002
    Location
    Singapore
    Posts
    724
    Originally posted by axion_sa
    VB Code:
    1. Public Function getRecordCount(ByVal strTable As String) As Long
    2.   Dim tConn As ADODB.Connection
    3.   Dim rsaGetCount As ADODB.Recordset
    4.   Dim strSQL As String
    5.  
    6.   ' Default the return value.
    7.   getRecordCount = 0
    8.  
    9.   Set tConn = New ADODB.Connection
    10.   tConn.Open [Connection string to your database]
    11.  
    12.   strSQL = "SELECT COUNT(*) AS Cnt FROM " & strTable
    13.   Set rsaGetCount = New ADODB.Recordset
    14.   rsaGetCount.Open strSQL, tConn
    15.  
    16.   getRecordCount = rsaGetCount("Cnt")
    17.  
    18.   ' Clean up.
    19.   On Error Resume Next
    20.   rsaGetCount.Close
    21.   Set rsaGetCount = Nothing
    22.  
    23.   tConn.Close
    24.   Set tConn = Nothing
    25. End Function

    And to get the value into a textbox:
    VB Code:
    1. txtRecords.Text = getRecordCount("YourTableName")

    u have to specify the lock type....


    Code:
    rsaGetCount.Open strSQL, tConn,adOpenKeyset, adLockOptimistic

  6. #6
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724
    No, you don't - SQL is being used to get record count.

  7. #7

    Thread Starter
    New Member
    Join Date
    Jan 2003
    Posts
    5

    like this?

    i did it like this. seems to work so far. do i have any errors??
    (why do i need to put the getRecordCount = 0 ) at the beginning?)

    Dim objConn As New Connection
    Dim objRS As Recordset
    Dim strSQL As String

    getRecordCount = 0

    objConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=Camp.mdb"

    Set objRS = New ADODB.Recordset
    objRS.CursorType = adOpenKeyset
    objRS.LockType = adLockOptimistic

    strSQL = "SELECT COUNT(*) AS Cnt FROM Member" & strTable
    objRS.Open strSQL, objConn

    getRecordCount = objRS("Cnt")

    objConn.Close

    calculate.Text = getRecordCount

  8. #8
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724

    Re: like this?

    Originally posted by Swimfish
    i did it like this. seems to work so far. do i have any errors??
    (why do i need to put the getRecordCount = 0 ) at the beginning?)
    You don't have to put the getRecordCount = 0, just a habit of mine. You also don't have to set the CursorType or LockType, the defaults will be fine, doesn't matter either way.
    If you've got a global connection to the database somewhere, pass that to the function as well - it's more efficient.

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