Results 1 to 7 of 7
  1. #1
    LUTINO is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2016
    Posts
    7

    Select query with max and group by

    Dear Seniors,
    I am new to VBA and I seek your help
    My logic is to have primary key on both columns CMP_ID & CmP_ID_Char.

    Table Company
    Fields Cmp_ID Number , Cmp_ID_char Text

    DATA
    Cmp_ID Cmp_ID_Char
    1 Black
    1 Red
    2 Black
    1 White
    2 Red


    In Vba code i want to run a SQL Query which can give me max(cmp_id) with group by cmp_id_char.. so i now the max numberr of black, red, white. etc

    i run this query in acces builder and resluts with good. but same query does not fecth any records. Recordset Eof & BOF are ture..



    Dim Max_Dbs As DAO.Database
    Dim max_Rst As DAO.Recordset
    Dim Tmp_Cmp_Char As String


    Dim MySql As String




    Set Max_Dbs = OpenDatabase("d:\access qoute new try\qt.accdb")




    MySql = "select max(cmp_id) from tbl_company where cmp_id_char= ' " & Txt_Cmp_Char & " ' group by cmp_id_char"
    Set max_Rst = Max_Dbs.OpenRecordset(MySql)






    here. Max_rst bof and eof are true...
    tell me where am i wrong..
    Txt_cmp_Char is a text box to input.(i tried both cases. upper and lower)

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    If you are simply trying to eliminate duplicates, you can do something like the following. I suggest pasting the SQL into SQL View of a query object so you can test the results. From there, it should be easy to determine the max. However, Max is not Count.
    Code:
    SELECT Company.CMP_ID, Company.CmP_ID_Char
    FROM Company
    GROUP BY Company.CMP_ID, Company.CmP_ID_Char
    ORDER BY Company.CMP_ID

  3. #3
    LUTINO is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2016
    Posts
    7
    Thank you for your prompt reply.. and appology if my question was too naive or too vague.

    I have applied Primary key on both ids. (its like plate of a vehicel... TAR0001, TAR0002 TAR0003... TAR9999). AND IN ORDER TO CREATE new record i have to know the MAX digit in specific Chaaracter group ... like MAX in TAR.. then I will MAX+1 to get new record id..
    this querey works fine in SQL View of ACCESS..but When I used this behind a form. it picks empty recordset.. where. EOF AND BOF both are true

    This query picks Company id (character) from a user input text box.. (TAR.. ZMO ETC)
    THEN IT CHECK THE MAX DIGIT ID FROM THE TABLE AGAINST THAT CRITERIA...
    NO ERROR IN QUERY. BUT RECORDSET IS EMPTY


    MySql = "select max(cmp_id) from tbl_company where cmp_id_char= ' " & Txt_Cmp_Char & " ' group by cmp_id_char"
    Set max_Rst = Max_Dbs.OpenRecordset(MySql)

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    It seems like you are missing an alias for your function
    Code:
    MySql = "select max(cmp_id) AS MyMax from tbl_company where cmp_id_char= ' " & Txt_Cmp_Char & " ' group by cmp_id_char"
    I am not really seeing anything else wrong with the SQL. Is the name of your table tbl_company or is it company?

    Another option might be to use DMax and include the value of Txt_Cmp_Char as criteria.
    https://msdn.microsoft.com/en-us/lib.../ff835050.aspx

  5. #5
    LUTINO is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2016
    Posts
    7
    Quote Originally Posted by ItsMe View Post
    It seems like you are missing an alias for your function
    Code:
    MySql = "select max(cmp_id) AS MyMax from tbl_company where cmp_id_char= ' " & Txt_Cmp_Char & " ' group by cmp_id_char"
    I am not really seeing anything else wrong with the SQL. Is the name of your table tbl_company or is it company?

    Another option might be to use DMax and include the value of Txt_Cmp_Char as criteria.
    https://msdn.microsoft.com/en-us/lib.../ff835050.aspx

    I added Alias as suggested in query


    MsgBox (max_Rst![cmp_id])

    Error Msg "Item not found in this collection.." ERROR CODE 3265

    Bof & Eof are still true. (means not data fatched)

    Table name is Tbl_Company


    COMPLETE CODE FOR THIS ROUTINE
    -----------------------------------------
    Sub Max_id_Gen()
    Dim Max_Dbs As DAO.Database
    Dim max_Rst As DAO.Recordset
    Dim Tmp_Cmp_Char As String
    Dim MySql As String
    Dim Max_id As Single

    Set Max_Dbs = OpenDatabase("d:\access qoute new try\qt.accdb")

    Tmp_Cmp_Char = Txt_Cmp_Char ' COPY TEXT DATA TO TEMPORARY VARIABLE'

    MySql = "select max(cmp_id) AS ALIAS_COMP_ID from tbl_company where cmp_id_char= ' " & Tmp_Cmp_Char & " ' group by cmp_id_char, CMP_ID"


    Set max_Rst = Max_Dbs.OpenRecordset(MySql)


    If max_Rst.BOF = True Then
    MsgBox "bof"
    End If

    If max_Rst.EOF = True Then
    MsgBox "Eof"
    End If

    MsgBox (max_Rst![cmp_id])

    End Sub
    ------------------------------------------

  6. #6
    LUTINO is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2016
    Posts
    7
    when i change this query criteria to static value

    MySql = "select max(cmp_id) AS ALIAS_COMP_ID from tbl_company where cmp_id_char= 'TAR' group by cmp_id_char"

    I gets results correctly.. but when i want to access same query with criteria picked form textbox. my recordset is empty.

    MySql = "select max(cmp_id) AS ALIAS_COMP_ID from tbl_company where cmp_id_char= ' " & Tmp_Cmp_Char & " ' group by cmp_id_char, CMP_ID"

  7. #7
    LUTINO is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2016
    Posts
    7
    Dear All,

    I found the BUG.. It was space between " and ' in select Query.
    I forgot to realise that for processer ' TAR ' and 'TAR' and two different values..

    Thank you all
    Last edited by LUTINO; 09-05-2016 at 10:55 AM.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 01-27-2016, 08:38 AM
  2. Replies: 6
    Last Post: 06-09-2015, 09:51 AM
  3. Replies: 17
    Last Post: 04-24-2014, 10:58 AM
  4. GROUP by yet SELECT fields not within GROUP BY
    By johnseito in forum Access
    Replies: 25
    Last Post: 11-03-2013, 10:20 PM
  5. Replies: 5
    Last Post: 07-29-2011, 11:54 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums