Results 1 to 13 of 13

Thread: Count

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2004
    Location
    Tampa, Fl
    Posts
    39

    Count

    Hello,
    I am makeing a program for the Lotto.
    I have a Access 2003 database with fields 0-9, would like to count the number of entries in each field and display in label caption.
    Form has Labels 0-9.
    cmdCount.
    VB Code:
    1. rs.Open "Select COUNT (*) FROM Cash3", cn, adOpenKeyset, adLockPessimistic, adCmdText
    2.         With rs
    3.             lblZero.Caption = rs.Fields("Zero").Value
    4.            
    5.    
    6.         End With
    7.     rs.Close
    Can anyone help with this?

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Count

    I usually do:
    VB Code:
    1. Dim count as Long
    2. rs.Open "Select COUNT (*) FROM Cash3", cn, adOpenKeyset, adLockPessimistic, adCmdText
    3. count = rs.GetString
    4. rs.Close
    5. MsgBox count
    Like Archer? Check out some Sterling Archer quotes.

  3. #3
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Smile Re: Count

    VB Code:
    1. Rs.open "select count(scholarNo) as S from Student_Info",Con,2,3
    2. Msgbox Rs!S

  4. #4
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Count

    Clarification please... do you need the number of records in that table? If so then solutions were given.

    Or do you want to count number of records relative to a given field where the field is not null or zero. You have to clarify rule/s for not counting a given record.field.value in the count; such as null and zero.

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2004
    Location
    Tampa, Fl
    Posts
    39

    Re: Count

    I need to count the number of records relative to a given field where the field is not null or zero.
    Example- how many times the number 1 has been entered into field One.

  6. #6
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Count

    This should work:
    VB Code:
    1. Rs.Open "SELECT COUNT(*) FROM tablename WHERE One=1", cn
    Then, you can either use rs.GetString or set the output "as X" and call it like that.. using the code I posted previously, you just need to add:
    VB Code:
    1. count = Rs.GetString
    2. Rs.Close
    Like Archer? Check out some Sterling Archer quotes.

  7. #7
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Count

    Try

    Select Count(Zero) As RecCount FROM Cash3 WHERE Zero > 0
    UNION
    Select Count(One) FROM Cash3 WHERE One > 0
    UNION
    Select Count(Two) FROM Cash3 WHERE Two > 0
    etc etc insert other queries for 3-8
    UNION
    Select Count(Nine) FROM Cash3 WHERE Nine > 0

    So first record (value of rs!RecCount at first record) is the count for field zero, second record for field one and so on and so forth. Let us know if it throws an error msg.

  8. #8

    Thread Starter
    Member
    Join Date
    Jan 2004
    Location
    Tampa, Fl
    Posts
    39

    Re: Count

    Error
    Data type mismatch in criteria exepression.

  9. #9
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Count

    Then we need info regarding your table structure; table name, field names, data type of each field

    Also post the erroneous query you used

  10. #10

    Thread Starter
    Member
    Join Date
    Jan 2004
    Location
    Tampa, Fl
    Posts
    39

    Re: Count

    Data Base = Lotto
    Table = Cash3
    Field =
    Date - Date
    zero - Number
    One - Number
    Two - Number
    Three - Number
    Four - Number
    Five - Number
    Six - Number
    Seven - Number
    Eight - Number
    Nine - Number
    Doubles - Number
    Triples - Number
    Order -
    VB Code:
    1. Option Explicit
    2. Private cn As ADODB.Connection
    3. Private rs As Recordset
    4. Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, _
    5.   ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
    6. Private Const HWND_TOPMOST = -1
    7. Private Const HWND_NOTOPMOST = -2
    8. Private Const SWP_NOMOVE = &H2
    9. Private Const SWP_NOSIZE = &H1
    10. Private Sub cmdCount_Click()
    11.         'just to see if it worked
    12.        rs.Open "Select COUNT (Zero) As RecCount FROM Cash3 WHERE Zero > 0", cn, adOpenKeyset, adLockPessimistic, adCmdText
    13.        With rs
    14.             lblZero.Caption = rs.Fields("Zero")
    15.            
    16.    
    17.         End With
    18.     rs.Close
    19. End Sub
    20.  
    21. Private Sub Form_Load()
    22.     Call SetWindowPos(Me.hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
    23.     frmCash3.Left = (Screen.Width - frmCash3.Width) / 2
    24.     frmCash3.Top = (Screen.Height - frmCash3.Height) / 2
    25.     Me.MousePointer = 11
    26.     Set cn = New ADODB.Connection
    27.     cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    28.      "Data Source= C:\Mikes\Loto\Lotto.mdb"
    29.     cn.Open
    30.     Set rs = New ADODB.Recordset
    31.     rs.Open "Cash3", cn, adOpenKeyset, adLockPessimistic, adCmdTable
    32.     rs.MoveFirst
    33.     Do Until rs.EOF = True
    34.     rs.MoveNext
    35.     Loop
    36.     rs.MoveFirst
    37.     Me.MousePointer = 0
    38.     rs.Close
    39. End Sub
    40. Private Sub mnuClear_Click()
    41.     txtDate.Text = ""
    42.     txt0.Text = ""
    43.     txt1.Text = ""
    44.     txt2.Text = ""
    45.     txt3.Text = ""
    46.     txt4.Text = ""
    47.     txt5.Text = ""
    48.     txt6.Text = ""
    49.     txt7.Text = ""
    50.     txt8.Text = ""
    51.     txt9.Text = ""
    52.     txtDoubles.Text = ""
    53.     txtTriples.Text = ""
    54.     txtRandom.Text = ""
    55.     txtOrder = ""
    56.     lblYes.Caption = ""
    57.     txtDate.SetFocus
    58. End Sub
    59.  
    60. Private Sub mnuCount_Click()
    61.    
    62.     rs.Open "Select COUNT (*) FROM Cash3 Where Zero ", cn, adOpenKeyset, adLockPessimistic, adCmdText
    63.        With rs
    64.             lblZero.Caption = rs.Fields("Zero").Value
    65.    
    66.         End With
    67.     rs.Close
    68. End Sub
    69.  
    70. Private Sub mnuEnter_Click()
    71.     rs.Open "Cash3", cn, adOpenKeyset, adLockPessimistic, adCmdTable
    72.         With rs
    73.             .AddNew
    74.             .Fields("Date") = txtDate.Text
    75.             .Fields("Zero") = txt0.Text
    76.             .Fields("One") = txt1.Text
    77.             .Fields("Two") = txt2.Text
    78.             .Fields("Three") = txt3.Text
    79.             .Fields("Four") = txt4.Text
    80.             .Fields("Five") = txt5.Text
    81.             .Fields("Six") = txt6.Text
    82.             .Fields("Seven") = txt7.Text
    83.             .Fields("Eight") = txt8.Text
    84.             .Fields("Nine") = txt9.Text
    85.             .Fields("Doubles") = txtDoubles.Text
    86.             .Fields("Triples") = txtTriples.Text
    87.             .Fields("Order") = txtOrder.Text
    88.             .Update
    89.     rs.Close
    90.     End With
    91.     lblYes.Caption = "YES"
    92. End Sub
    93.  
    94. Private Sub mnuExit_Click()
    95.     End
    96. End Sub
    Number

  11. #11
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Count

    There's nothing wrong with the query, your not using the alias reccount.

    VB Code:
    1. Dim sSQL As String
    2.  
    3. sSQL = "Select COUNT (Zero) As RecCount FROM Cash3 WHERE Zero > 0"
    4. sSQL = sSQL & "UNION Select Count(One) FROM Cash3 WHERE One > 0"
    5.  
    6. 'just to see if it worked
    7. rs.Open sSQL, cn, adOpenKeyset, adLockPessimistic, adCmdText
    8.        With rs
    9.             [b]'lblZero.Caption = .Fields("Zero")   'this is your error[/b]
    10.             lblZero.Caption = .fields("RecCount").Value
    11.             .MoveNext
    12.             lblOne.Caption = .Fields("RecCount").Value
    13.         End With

    fill in the rest... update sSQL to include union of other fields' count aggregate... update othe labels to display result... don't forget to use alias RecCount and to iterate (.MoveNext) through recordset.

    BTW I don't think this will work due to different data types
    VB Code:
    1. .Fields("Date") = txtDate.Text
    2.             .Fields("Zero") = txt0.Text
    3.             .Fields("One") = txt1.Text
    4.             .Fields("Two") = txt2.Text
    5.             .Fields("Three") = txt3.Text
    6.             .Fields("Four") = txt4.Text
    7.             .Fields("Five") = txt5.Text
    8.             .Fields("Six") = txt6.Text
    9.             .Fields("Seven") = txt7.Text
    10.             .Fields("Eight") = txt8.Text
    11.             .Fields("Nine") = txt9.Text
    12.             .Fields("Doubles") = txtDoubles.Text
    13.             .Fields("Triples") = txtTriples.Text
    14.             .Fields("Order") = txtOrder.Text

    Convert string/text to relevant data type when assigning to a field. sample
    .Fields("Zero") = Val(txt0.Text)

  12. #12

    Thread Starter
    Member
    Join Date
    Jan 2004
    Location
    Tampa, Fl
    Posts
    39

    Re: Count

    I get
    Syntax error (missing operator) in query expression 'Zero >UNION
    Select Count (One) FROM Cash3 WHERE One > 0'.

    There is a simple way to do this ( I lost it when my computer cashed ).
    I do not remember using sSQL = sSQL as I am using Access.
    Oh well I'll find it, just have to keep looking.

  13. #13
    Lively Member
    Join Date
    Sep 2005
    Location
    Tijuana,Mexico
    Posts
    109

    Re: Count

    The error you get is caused because you are missing a space:

    VB Code:
    1. sSQL = "Select COUNT (Zero) As RecCount FROM Cash3 WHERE Zero > 0"
    2. sSQL = sSQL & "UNION Select Count(One) FROM Cash3 WHERE One > 0"
    You would get:

    VB Code:
    1. sSQL = "Select COUNT (Zero) As RecCount FROM Cash3 WHERE Zero > [B]0UNION[/B] Select Count(One) FROM Cash3 WHERE One > 0"

    So just make a simple adjustement...

    VB Code:
    1. sSQL = "Select COUNT (Zero) As RecCount FROM Cash3 WHERE Zero > 0"
    2. sSQL = sSQL &[B] " UNION [/B] Select Count(One) FROM Cash3 WHERE One > 0"
    and that's all...

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