Results 1 to 7 of 7
  1. #1
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839

    Display First/Last or Max/MIN


    I have a form with an 2 unbound fields:

    Me.MyChanges

    Code:
    Public Sub SelectChanges_Click()
        Dim StrWhere As String, ctl As Control, varItem As Variant, tbWhere As String, StrWhere2 As String
        
            Set ctl = Me.MyChanges
            
    If Me.MyChanges.ItemsSelected.Count = 0 Then
         MsgBox "Nothing was selected"
            Exit Sub
        End If
    
    For Each varItem In ctl.ItemsSelected
            StrWhere = StrWhere & "'" & ctl.ItemData(varItem) & "',"
            StrWhere2 = StrWhere2 & " " & ctl.ItemData(varItem) & ","
    
    
    Next varItem
       StrWhere = Left(StrWhere, Len(StrWhere) - 1)
       StrWhere2 = Left(StrWhere2, Len(StrWhere2) - 2)
       Me.tbWhere = StrWhere2
        
       DoCmd.OpenReport "rptSelectChanges", acViewReport, , "CRNumber IN(" & StrWhere & ")"
    End Sub

    How do I go about only using the smallest and largest of the selected items?

    .Subject = "Change(s) - " & First(MyChanges) & " - " & Last(MyChanges)

    I think with the Format that it changes to a string vice a number?

    Code:
    SELECT Format((([tblChangeRequest].[CRNo])+([SubNo]*0.01)),"Fixed") AS  CRNumber FROM tblChangeRequest GROUP BY  Format((([tblChangeRequest].[CRNo])+([SubNo]*0.01)),"Fixed"),  tblChangeRequest.CRID HAVING (((tblChangeRequest.CRID)>25)) ORDER BY  tblChangeRequest.CRID;
    Thanks
    T

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Aggregate functions must be used in an aggregate (GROUP BY) query or in the ControlSource of a textbox. On a form the textbox would be in form header/footer section. On report could be in group/page/report header/footer section.

    Domain aggregate functions (DLookup, DSum, DCount, etc.) can be used in query, ControlSource, or VBA.

    Yes, Format function returns a string. Do formatting in textbox.
    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.

  3. #3
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839
    I figured how to get the first:
    Sml = ctl.ItemData(varItem)

    How would I get the last item from ctl?

  4. #4
    NTC is offline VIP
    Windows 10 Access 2013
    Join Date
    Nov 2009
    Posts
    2,392
    I don't follow exactly what you seek. However my sense is that this should not be a code-centric approach. As J7 points out there are aggregate functions. The most straight forward way to implement these are with queries. Look for the sigma symbol (like a big E) - and there are instructions online as to how to work them for max, min, avg, sum, etc.

    Make a query that results in the record correctly - and save it with a name.

    Then in terms of your form - consider it only as a point of display (not a point of calculation). In your unbound textbox you bind it to a control source: =DLookUp("field name", "query name")

  5. #5
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839

    Post

    I am looking for the first and last item of ctl

    so Me.MyChanges.ItemSelected Count = 1 should be the first item.

    I am selecting them from Form_frmSelectChanges from an unbound field on frmSelectChanges.MyChanges which gets its information from:

    Code:
    SELECT Format((([tblChangeRequest].[CRNo])+([SubNo]*0.01)),"Fixed") AS CRNumber FROM tblChangeRequest GROUP BY Format((([tblChangeRequest].[CRNo])+([SubNo]*0.01)),"Fixed"), tblChangeRequest.CRID HAVING (((tblChangeRequest.CRID)>25)) ORDER BY tblChangeRequest.CRID;

    What you are saying is that I should put a DMAX and DMIN in the above query?

    I would like to do the coding route also:

    If I have example data in CRNumber where CRID greater then 26:
    1.00
    1.01
    1.02
    1.03
    2.00
    2.01
    3.00
    3.01
    3.02
    3.03
    In the form I select (Clicked on and highlighted) 1.00, 1.01, 1.02 and

    If Me.MyChanges.ItemSelected Count = 1, then
    ctl.ItemData(varItem) = 1.00
    StrWhere2 = StrWhere2 & " " & ctl.ItemData(varItem) & "," would be 1.00

    If Me.MyChanges.ItemSelected Count = 2, then
    ctl.ItemData(varItem) = 1.01
    StrWhere2 = StrWhere2 & " " & ctl.ItemData(varItem) & "," would be 1.00, 1.01

    If Me.MyChanges.ItemSelected Count = 3, then
    ctl.ItemData(varItem) = 1.02
    StrWhere2 = StrWhere2 & " " & ctl.ItemData(varItem) & "," would be 1.00, 1.01, 1.02

    Me.MyChanges.ItemSelected Count = 3 is the last/final ctl.ItemData(varItem) of this series.

    All I want is the first VarItem and the last VarItem, so that I can put it in my .Subject line and part of my file naming convention with attachments.

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    What is ctl? A multi-select extended listbox?

    You want values from the first and last rows of extended listbox selection? Consider:

    Code:
        Dim varItem As Variant
        Dim strFirst As String, strLast As String, strWhere As String
        Dim iCount As Integer
        iCount = 0
        With Me.myChanges
            If .ItemsSelected.Count <> 0 Then
                For Each varItem In .ItemsSelected
                    strWhere = strWhere & "'" & ctl.ItemData(varItem) & "',"
                    If iCount = 0 Then strFirst = .ItemData(varItem)
                    If iCount = .ItemsSelected.Count - 1 Then strLast = .ItemData(varItem)
                    iCount = iCount + 1
                Next
                Me.tbWhere = Replace(strWhere, "'", "")
                DoCmd.OpenReport "rptSelectChanges", acViewReport, , "CRNumber IN(" & strWhere & ")"
            Else
                MsgBox "Nothing was selected from the list", vbInformation
            End If
        End With
    
    ...
    
    .Subject = "Change(s) - " & strFirst & " - " & strLast
    Or instead of the IN operator, use BETWEEN AND in the criteria. Eliminates the strWhere concatenation.

    Me.tbWhere = strFirst & " - " & strLast
    DoCmd.OpenReport "rptSelectChanges", acViewReport, , "CRNumber BETWEEN '" strFirst & "' AND '" & strLast & "'"

    Review https://support.microsoft.com/en-us/...on-list-box-as
    Last edited by June7; 03-09-2018 at 06:42 AM.
    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
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839
    Thanks June7, what I was looking for. I did some tweaking and it does what I intended.

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

Similar Threads

  1. Replies: 3
    Last Post: 10-28-2015, 12:38 PM
  2. Replies: 2
    Last Post: 06-24-2015, 02:38 PM
  3. Does not display
    By Thompyt in forum Forms
    Replies: 12
    Last Post: 03-02-2015, 12:49 PM
  4. Getting a map to display
    By cggriggs in forum Forms
    Replies: 1
    Last Post: 07-13-2011, 06:43 PM
  5. Report control of a field display/no display
    By systems013 in forum Reports
    Replies: 5
    Last Post: 02-01-2010, 09:44 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