Results 1 to 12 of 12

Thread: SQL statement not working

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Posts
    76

    SQL statement not working

    I am using a SQL statement to populate a MSFlexgrid, but I find that I am getting the wrong results I want to select and group and count all records by a field (clm_det_r) where one field ( chkConfirmed_r) is true. Instead, it is pulling all records where clm_det_r is not null.

    This is the code I've written:
    VB Code:
    1. CatSQL = "SELECT [page2_3].[clm_det_r], COUNT([page2_3].[clm_det_r])FROM [Page2_3]GROUP BY [page2_3].[clm_det_r] WHERE [chkconfirmed_r] = TRUE"
    What am I doing wrong?

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: SQL statement not working

    Try:
    VB Code:
    1. CatSQL = "SELECT COUNT([page2_3].[clm_det_r])FROM [Page2_3] WHERE [chkconfirmed_r] = TRUE
    2. GROUP BY [page2_3].[clm_det_r] "

  3. #3
    Hyperactive Member
    Join Date
    May 2003
    Posts
    401

    Re: Re: SQL statement not working

    That is correct..try salvelinus's reply...what u are doing wrong is when u are using group by statement in your sql, the selected fields should be group functions...not single fields...look into the following link for more info..
    http://www.w3schools.com/sql/sql_groupby.asp
    Enjoy!!!
    apps_tech

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Also, I believe that when you use a GROUP BY clause the WHERE
    should be replaced by a HAVING caluse.
    VB Code:
    1. CatSQL = "SELECT COUNT([page2_3].[clm_det_r]) "
    2. CatSQL = CatSQL & "FROM [Page2_3] "
    3. CatSQL = CatSQL & "GROUP BY [page2_3].[clm_det_r] "
    4. CatSQL = CatSQL & "HAVING [chkconfirmed_r] = TRUE"
    Hope this helps.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Originally posted by RobDog888
    Also, I believe that when you use a GROUP BY clause the WHERE
    should be replaced by a HAVING caluse.
    VB Code:
    1. CatSQL = "SELECT COUNT([page2_3].[clm_det_r]) "
    2. CatSQL = CatSQL & "FROM [Page2_3] "
    3. CatSQL = CatSQL & "GROUP BY [page2_3].[clm_det_r] "
    4. CatSQL = CatSQL & "HAVING [chkconfirmed_r] = TRUE"
    Hope this helps.
    ONLY when the filtered item is the aggregated result....IE, you want to only return items whose max value is greater than 10.
    SELECT Field1, Field2, MAX(Field3) FROM tblSample
    GROUP BY Field1, Field2
    HAVING MAX(Field3) > 10
    But it is still possible to use the WHERE clause to exclude records from the aggregation in the first place.... it just cannot go at the end ... it MUST come before the GROUP BY.
    Also, if it is used in the HAVING, it MUST be in the SELECT clause as well.

    TG
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    True, True.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Posts
    76
    It is now giving me run-time error 3122 "You tried to execute a query that does not include the specified expression '[page2_3].[chkconfirmed_r]=-1' as part of an aggregate function."

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Since the field in not part of the aggregate function you still need the where clause.
    VB Code:
    1. CatSQL = "SELECT COUNT([page2_3].[clm_det_r]) "
    2. CatSQL = CatSQL & "FROM [Page2_3] "
    3. CatSQL = CatSQL & "WHERE [chkconfirmed_r] = TRUE"
    4. CatSQL = CatSQL & "GROUP BY [page2_3].[clm_det_r] "
    This was discussed in the previous two postings.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Posts
    76
    It is now giving me run-time error 3122 "You tried to execute a query that does not include the specified expression '[page2_3].[chkconfirmed_r]=-1' as part of an aggregate function."

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Since we will be adding the field to the select list we can use the having clause.
    VB Code:
    1. CatSQL = "SELECT COUNT([page2_3].[clm_det_r]), "
    2. CatSQL = CatSQL & "[Page2_3].[chkconfirmed_r]"
    3. CatSQL = CatSQL & "FROM [Page2_3] "
    4. CatSQL = CatSQL & "GROUP BY [page2_3].[clm_det_r], "
    5. CatSQL = CatSQL & "[Page2_3].[chkconfirmed_r]"
    6. CatSQL = CatSQL & "HAVING [Page2_3].[chkconfirmed_r] = TRUE"
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Posts
    76
    Originally posted by RobDog888
    Since we will be adding the field to the select list we can use the having clause.
    VB Code:
    1. CatSQL = "SELECT COUNT([page2_3].[clm_det_r]), "
    2. CatSQL = CatSQL & "[Page2_3].[chkconfirmed_r]"
    3. CatSQL = CatSQL & "FROM [Page2_3] "
    4. CatSQL = CatSQL & "GROUP BY [page2_3].[clm_det_r], "
    5. CatSQL = CatSQL & "[Page2_3].[chkconfirmed_r]"
    6. CatSQL = CatSQL & "HAVING [Page2_3].[chkconfirmed_r] = TRUE"
    OK, I tried it this way, and it stopped giving me the 3122 error, but the results I am getting in my MSFlexgrid are wrong. It is displaying the correct count for the fields [Page2_3].[clm_det_r], but then it does not show the items grouped in [Page2_3].[clm_det_r]. Instead, it displays True

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Posts
    76
    Originally posted by FinChase
    OK, I tried it this way, and it stopped giving me the 3122 error, but the results I am getting in my MSFlexgrid are wrong. It is displaying the correct count for the fields [Page2_3].[clm_det_r], but then it does not show the items grouped in [Page2_3].[clm_det_r]. Instead, it displays True
    To clarify further, I am trying to pull a recordset that will contain two fields - a grouping of Error Categories (contained in the fields [clm_det_r] and a count of each Error Category. It's pulling the correct counts, but it's not grouping the error categories. If this helps, here's what I'm trying to do in a query built directly in Access:
    VB Code:
    1. SELECT PAGE2_3.CLM_DET_R, Count(PAGE2_3.CLM_DET_R) AS CountOfCLM_DET_R
    2. FROM PAGE2_3
    3. WHERE (((PAGE2_3.CHKCONFIRMED_r)=True))
    4. GROUP BY PAGE2_3.CLM_DET_R;

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