Results 1 to 10 of 10
  1. #1
    tekerin is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2013
    Posts
    7

    Access Module to report field with max value

    Hello,



    I am fairly new to programming and so far I've only been able to create a database using queries (the drag and drop a link kind, not SQL). I am trying to find the max value from a table and return the field header, and I think I need to create that via a module, which I was hoping to get some help with. For example, in the below table, I am trying to return the month where each client had the largest value. A would return Mar while B would return Feb. Is there an easy way to create a module for this? Ideally, the output of "Mar" & "Feb" can be in a newly created column in Table 1, or if I can create a 2nd table containing this information with Customer Name filled as well, that would work.

    Thanks in advance for your help. I am just clueless when it comes to programming.

    Table1:
    Customer Name Jan Feb Mar
    A 1 2 3
    B 4 5 0

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    The design isn't normalized, but you could adapt this:

    Minimum or Maximum Value Across Fields of Record
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    As Paul noted, if data was normalized, code would not be needed - a query could accomplish without a custom function.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    tekerin is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2013
    Posts
    7
    Thanks guys for your help. I know it's not normalized and it's probably not the best way of doing it, it's just that I have to work with about 7 files, each with over ~100k records, so I was hoping to not normalize the data if I can. The link to the ACC2000 worked perfect! Thanks very much. One last question, how do I actually find the month that the max value occurs in? I know have:

    Table1:
    Customer Name, Max Value (this is showing as text for some reason, not values)
    A, 3
    B, 5

    How do I actually report the month as well, like:
    Customer Name, Max Value, Month
    A, 3, Mar
    B, 5, Feb

    Thanks again for everyone's help.

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I'll stay out of June's way.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    Since you don't want to normalize, more VBA. The code Paul referenced could be modified to return the Month fieldname and Max as a single data. Or write another function to return the month fieldname of the maximum value.

    Suggest you take a crash course in VBA programming, looks like you're really going to need it.

    However, another option might be UNION query. A UNION can rearrange data to an essentially normalized structure. Then use that query as you would the equivalent table for data analysis, could not edit data through the UNION.

    SELECT [Customer Name], "Jan" As Mo, [Jan] As Data FROM tablename
    UNION SELECT [Customer Name], "Feb", [Feb] FROM tablename
    UNION SELECT [Customer Name], "Mar", [Mar] FROM tablename;

    Are there multi-year records?

    7 files, only 100,000 records each? I have worked with posters who described Access database with millions of records.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    tekerin is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2013
    Posts
    7
    Thanks for your response.

    For the VBA code, what's the syntax you would use to grab the header field? I've had some basic experiences with VBA before, but I searched online for a bit and couldn't find how to return the header once a value is matched. Is this an easy code that can be created?

    Thanks again.

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    I took another look at the suggested code and modifying it would be more difficult than I first thought, so would be another function.

    Why does it matter what field has the max? What if there is a tie?

    Have you explored the UNION approach? No VBA needed.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    tekerin is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2013
    Posts
    7
    I need to reply the header as we want to report the month that the maximum resides in for each company. There shouldn't be tie as this is really for a stress test (just used company / month to illustrate), and each stress test is unique so the final figure will be unique as well.

    I know this isn't best practice, but would it be easier if i duplicated the header into the first record always?

    Thanks again.

  10. #10
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    Sorry, don't follow the question.

    Again, have you considered the UNION query? - which would eliminate need for any VBA
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Getting Variable from Module to Report
    By dylcon in forum Modules
    Replies: 3
    Last Post: 02-14-2014, 03:43 PM
  2. Replies: 3
    Last Post: 04-11-2013, 06:53 AM
  3. class module vs regular module
    By Madmax in forum Modules
    Replies: 1
    Last Post: 05-01-2012, 03:44 PM
  4. Replies: 4
    Last Post: 05-16-2011, 04:58 PM
  5. Create module to add field
    By DavidAlan in forum Modules
    Replies: 3
    Last Post: 09-08-2010, 09:18 AM

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