Results 1 to 7 of 7

Thread: [RESOLVED] how can i know which customer is most profit in my business

  1. #1

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Resolved [RESOLVED] how can i know which customer is most profit in my business

    hey
    i have a table called Customers
    there are 2 fields
    FullName
    TotalPAyments
    how can i know which customer is must profit to my business
    and display it in a listview or msgbox
    e.x
    salsa 32000
    mambo 89520
    david 12300

    regards
    salsa31

  2. #2
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    868

    Re: how can i know which customer is most profit in my business

    Salsa

    Maybe something like this

    Code:
        '
        Dim maxProfit As Long
        Dim maxName As String
        '
        maxProfit = 0
        rsCustomers.MoveFirst
        Do
            If rsCustomers.EOF() Then
                Exit Do
            Else
                vprof = rsCustomers("Fullname")
                vname = rsCustomers("TotalPayments")
                If vprof > maxProfit Then
                    maxProfit = vprof
                    maxName = vname
                End If
            End If
            rsCustomers.MoveNext
        Loop
        '
        MsgBox maxName & " " & Trim(maxProfit)
        '
    Note: I have not tested this.
    HTH

    Spoo
    Last edited by Spooman; Oct 22nd, 2017 at 11:54 AM.

  3. #3

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: how can i know which customer is most profit in my business

    Maybe something like this
    hey spoo
    tried to play with your code
    still cant figure it out :/

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: how can i know which customer is most profit in my business

    If you have a table with those fields you can simply sort the data as part of your query

    If you want them all from highest to lowest then
    Code:
    Select FullName, TotalPAyments  from Customers order by TotalPAyments DESC
    If you only want the one then
    Code:
    Select Top 1 FullName, TotalPAyments  from Customers order by TotalPAyments DESC

  5. #5

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: how can i know which customer is most profit in my business

    so simple
    tnk you sir

  6. #6
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    868

    Re: [RESOLVED] how can i know which customer is most profit in my business

    Salsa

    Glad DM's approach worked for you .. so simple, indeed.

    FWIW, my approach was a brute force one (SELECT didn't occur to me).

    1. rsCustomers represented your table
    2. the concept was to loop through all records to find the record with the maximum profit (ie, largest TotalPayments)
    3. MoveFirst sets the pointer to the first record
    4. maxProfit is set to 0 to be the lowest value
    5. then the loop is entered
      • if the current record's profit (vprofit) is larger than the max so for
        • maxProfit is updated
        • maxName is also updated .. ie, the Customer associated with that profit
      • MoveNext sets pointer to the next record
      • when the last record is encountered (EOF), the loop is exited.
    6. the results are then posted in the MsgBox


    My terminology may have been a bit confusing ...
    • maxProfit vs TotalPaymemts,
    • maxName vs Customer

    ... but I hope that explains the basic concept.

    Nonetheless, DM's approach is superior.

    Spoo
    Last edited by Spooman; Oct 23rd, 2017 at 04:45 AM.

  7. #7
    Frenzied Member
    Join Date
    Jun 2014
    Posts
    1,084

    Re: [RESOLVED] how can i know which customer is most profit in my business

    Q) What is profit ?
    A) a financial gain, especially the difference between the amount earned and the amount spent in buying, operating, or producing something.
    do not put off till tomorrow what you can put off forever

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