Results 1 to 9 of 9

Thread: Problem with query, AGAIN!!!!!

  1. #1

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606
    What could i do to get something like:
    Code:
    memberName        count
    -------------------------
    Ace               12
    Normal            34
    Member            02
    Those are my table(they more field, but for the purpose we wont need them)
    Code:
    Table:
    -tblMemberType
       -memberTypeId
       -memberName
    
    -tblPlayer
       -playerMemberTypeId
    I want to get the memberName and how many player have this memberName.

    I can't get the Count function to work.

    It's always the same error :....as part of an aggregate function.


  2. #2
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008
    Sebs,

    I've just picked this up. I'll answer in the morning (Tuesday) when I've got a bit more time.

    P.
    Not nearly so tired now...

    Haven't been around much so be gentle...

  3. #3
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Thumbs up Using DAO Object

    this may not the shortest code but yet it does what you need...

    Code:
    Option Explicit
    Private db As DAO.Database
    Private rs As DAO.Recordset
    Private rs2 As DAO.Recordset
    Private sql As String
    
    Private Sub Form_Load()
    Dim result As String
    Set db = DAO.OpenDatabase("C:\player.mdb", False, False)
    
    sql = "SELECT DISTINCT memberName, memberTypeId " & _
            "FROM tblMemberType " & _
            "ORDER BY memberName ASC;"
    Set rs = db.OpenRecordset(sql, dbOpenSnapshot)
    With rs
        If .RecordCount <> 0 Then
            Debug.Print "memberName" & vbTab & "Count"
            Debug.Print "-----------------"
            While Not .EOF
                sql = "SELECT Count(playerMemberTypeId) as nMember " & _
                        "FROM tblPlayer " & _
                        "WHERE playerMemberTypeId = '" & rs.Fields("memberTypeId") & "';"
                        
                Set rs2 = db.OpenRecordset(sql, dbOpenSnapshot)
                If rs2.RecordCount <> 0 Then
                    result = String(128, Chr(32))
                    Mid(result, 1) = rs.Fields("memberName")
                    Mid(result, 13) = Format(rs2.Fields("nMember"), "00")
                    Debug.Print result
                End If
                rs2.Close
                Set rs2 = Nothing
                DoEvents
                .MoveNext
            Wend
        End If
    End With
    
    rs.Close
    Set rs = Nothing
    db.Close
    Set db = Nothing
    End Sub

  4. #4
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Thumbs up ADO Object

    This another sample with ADO object

    Code:
    Option Explicit
    Private conn As ADODB.Connection
    Private rs As ADODB.Recordset
    Private rs2 As ADODB.Recordset
    Private sql As String
    
    Private Sub Form_Load()
    Dim result As String
    Set conn = New ADODB.Connection
    conn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\player.mdb"
    
    sql = "SELECT DISTINCT memberName, memberTypeId " & _
            "FROM tblMemberType " & _
            "ORDER BY memberName ASC;"
    Set rs = New ADODB.Recordset
    With rs
        .Open sql, conn, adOpenKeyset, adLockPessimistic
        If .RecordCount <> 0 Then
            Debug.Print "memberName" & vbTab & "Count"
            Debug.Print "-----------------"
            While Not .EOF
                sql = "SELECT Count(playerMemberTypeId) as nMember " & _
                        "FROM tblPlayer " & _
                        "WHERE playerMemberTypeId = '" & rs.Fields("memberTypeId") & "';"
                        
                Set rs2 = New ADODB.Recordset
                rs2.Open sql, conn, adOpenKeyset, adLockPessimistic
                If rs2.RecordCount <> 0 Then
                    result = String(128, Chr(32))
                    Mid(result, 1) = rs.Fields("memberName")
                    Mid(result, 13) = Format(rs2.Fields("nMember"), "00")
                    Debug.Print result
                End If
                rs2.Close
                Set rs2 = Nothing
                DoEvents
                .MoveNext
            Wend
        End If
    End With
    
    rs.Close
    Set rs = Nothing
    conn.Close
    Set conn = Nothing
    End Sub

  5. #5
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Unhappy Single SQL statement

    Mean while I still can not archieve this with a single SQL statement. If anyone got the single SQL statement solution do post it & I would like to know how can it be done.

  6. #6
    Member
    Join Date
    Oct 2000
    Location
    Netherlands
    Posts
    54

    Cool Single SQL

    I'm using the SQL below to determine howmany times someone has looged onto an application. It's counts the succesfull logons and also give the last used logon date, all in one SQL.

    strSQL = "SELECT tblLog.Username, Last(tblLog.LogDate) AS LastOfLogDate, Count(tblLog.Succes) AS CountOfSucces FROM tblLog GROUP BY tblLog.Username ORDER BY tblLog.Username, Last(tblLog.LogDate)"

    I have no time to convert this to your requirements, but maybe it gives you an idea. I just used a Group By in a query and used Count on the Succes field.
    A mind is like a parachute, it has to open to let it work
    www.2beesoft.com for Icon Manager with over 20.000 free icons
    VB6 Ent. SP4, ASP, W2000/W98

  7. #7
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Hi! Berthil, I think you case is a bit different because all your data seem to be from the same source table.

  8. #8
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008
    This can be accomplished using:

    Code:
    SELECT tblMemberType.memberName, Count(tblMemberType.memberName) AS memberCount
    FROM tblPlayer INNER JOIN tblMemberType ON tblPlayer.playerMemberTypeId = tblMemberType.memberTypeId
    GROUP BY tblMemberType.memberName;
    Should work.

    Cheers,

    P.
    Not nearly so tired now...

    Haven't been around much so be gentle...

  9. #9
    Member
    Join Date
    Oct 2000
    Location
    Netherlands
    Posts
    54

    Thumbs up Good Job

    Paul has done a good job. I suspected yall had some knowledge of SQL to put that together.

    You can think of MSACCESS what you want to think but I use it to build complex queries and copy the SQL to a string in VB. Works fine every time.
    A mind is like a parachute, it has to open to let it work
    www.2beesoft.com for Icon Manager with over 20.000 free icons
    VB6 Ent. SP4, ASP, W2000/W98

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