Results 1 to 25 of 25

Thread: Need changes in Statement to get the correct balance amount ...

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    287

    Need changes in Statement to get the correct balance amount ...

    Hi,

    The following code works accurate when I don't care about the Date Order.

    VB Code:
    1. rs.Open "SELECT CustomerName, Option, FileNumber, City, Telephone, Fax, DDate, RP, InvoiceAmount As Credit, AmountReceived As Debit,(SELECT SUM(InvoiceAmount-AmountReceived) FROM SalesData SD2 WHERE SD2.ID <= SD1.ID AND SD2.CustomerName= SD1.CustomerName) As Balance FROM SalesData SD1 Where CustomerName like '%" & txtSearchAnyWord & "%'ORDER BY CustomerName, ID", db

    But I need all the dates in a particular order and the problem is that sometimes I need to enter data with old dates as well. The following code doesn't work properly showing wrong values in "Balance" Column. How to solve the problem? Please help me out.

    VB Code:
    1. rs.Open "SELECT CustomerName, Option, FileNumber, City, Telephone, Fax, DDate, RP, InvoiceAmount As Credit, AmountReceived As Debit,(SELECT SUM(InvoiceAmount-AmountReceived) FROM SalesData SD2 WHERE SD2.DDate <= SD1.DDate AND SD2.CustomerName= SD1.CustomerName) As Balance FROM SalesData SD1 Where CustomerName like '%" & txtSearchAnyWord & "%'ORDER BY CustomerName, DDate", db

    The attached file may help to understand my problem.

    Regards,

    Margaret
    Attached Images Attached Images  

  2. #2
    Member
    Join Date
    Jan 2007
    Posts
    36

    Re: Need changes in Statement to get the correct balance amount ...

    Hi...
    If u r using oracle database then use rowid in place of date... that'll solve your problem.

    But if u r using MS Access database then I think u have to modify your table because in Access there is no such rowid field. U have to add another column in the table and set data type of that field as 'AutoNumber'. Now if u use this field in place of the date field your problem will be solved.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    287

    Re: Need changes in Statement to get the correct balance amount ...

    Quote Originally Posted by tomy_3003
    Hi...
    If u r using oracle database then use rowid in place of date... that'll solve your problem.

    But if u r using MS Access database then I think u have to modify your table because in Access there is no such rowid field. U have to add another column in the table and set data type of that field as 'AutoNumber'. Now if u use this field in place of the date field your problem will be solved.
    Hi,

    I am using the MS Access database. There is a field called "ID" with autonumber. How to solve the problem now?

    Regards,

    Margaret
    Attached Images Attached Images  

  4. #4
    Member
    Join Date
    Jan 2007
    Posts
    36

    Re: Need changes in Statement to get the correct balance amount ...

    VB Code:
    1. rs.Open "SELECT CustomerName, Option, FileNumber, City, Telephone, Fax, DDate, RP, InvoiceAmount As Credit, AmountReceived As Debit,(SELECT SUM(InvoiceAmount-AmountReceived) FROM SalesData SD2 WHERE SD2.ID <= SD1.ID AND SD2.CustomerName= SD1.CustomerName) As Balance FROM SalesData SD1 Where CustomerName like '%" & txtSearchAnyWord & "%'ORDER BY CustomerName, DDate", db

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    287

    Re: Need changes in Statement to get the correct balance amount ...

    Quote Originally Posted by tomy_3003
    VB Code:
    1. rs.Open "SELECT CustomerName, Option, FileNumber, City, Telephone, Fax, DDate, RP, InvoiceAmount As Credit, AmountReceived As Debit,(SELECT SUM(InvoiceAmount-AmountReceived) FROM SalesData SD2 WHERE SD2.ID <= SD1.ID AND SD2.CustomerName= SD1.CustomerName) As Balance FROM SalesData SD1 Where CustomerName like '%" & txtSearchAnyWord & "%'ORDER BY CustomerName, DDate", db
    Hi,

    I have tried the above code but it shows wrong BALANCE amount.

    Regards,

    Margaret
    Attached Images Attached Images  

  6. #6
    Member
    Join Date
    Jan 2007
    Posts
    36

    Re: Need changes in Statement to get the correct balance amount ...

    Hi..

    Actually the output is not wrong. What is happening here is that the output is coming in reverse order because u have sorted the output as reverse order. I think in your table the date and id field are sorted opposite to each other.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    287

    Re: Need changes in Statement to get the correct balance amount ...

    Quote Originally Posted by tomy_3003
    Hi..

    Actually the output is not wrong. What is happening here is that the output is coming in reverse order because u have sorted the output as reverse order. I think in your table the date and id field are sorted opposite to each other.
    Hi, I have not sorted anything in the Database table. Just using the querry only. How to solve the problem now?

    Regards,

    Margaret

  8. #8
    Member
    Join Date
    Jan 2007
    Posts
    36

    Re: Need changes in Statement to get the correct balance amount ...

    VB Code:
    1. rs.Open "SELECT CustomerName, Option, FileNumber, City, Telephone, Fax, DDate, RP, InvoiceAmount As Credit, AmountReceived As Debit,(SELECT SUM(InvoiceAmount-AmountReceived) FROM SalesData SD2 WHERE (SD2.DDate < SD1.DDate OR (SD2.DDate = SD1.DDate AND SD2.ID <= SD1.ID)) AND SD2.CustomerName= SD1.CustomerName) As Balance FROM SalesData SD1 Where CustomerName like '%" & txtSearchAnyWord & "%'ORDER BY CustomerName, DDate", db

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    287

    Re: Need changes in Statement to get the correct balance amount ...

    Quote Originally Posted by tomy_3003
    VB Code:
    1. rs.Open "SELECT CustomerName, Option, FileNumber, City, Telephone, Fax, DDate, RP, InvoiceAmount As Credit, AmountReceived As Debit,(SELECT SUM(InvoiceAmount-AmountReceived) FROM SalesData SD2 WHERE (SD2.DDate < SD1.DDate OR (SD2.DDate = SD1.DDate AND SD2.ID <= SD1.ID)) AND SD2.CustomerName= SD1.CustomerName) As Balance FROM SalesData SD1 Where CustomerName like '%" & txtSearchAnyWord & "%'ORDER BY CustomerName, DDate", db
    Now, I get the following output which is also incorrect.

    Margaret
    Attached Images Attached Images  

  10. #10
    Member
    Join Date
    Jan 2007
    Posts
    36

    Re: Need changes in Statement to get the correct balance amount ...

    Can u please send me the data from your table for this particular customer....

    Copy the data from access table and paste in in an excel file and then attach that excel file. Without seeing the table i can't find the problem... I created a dummy table and this code is working fine in my table.

  11. #11
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Need changes in Statement to get the correct balance amount ...

    Because you used LIKE to specify the customer ... so what happens is debits and credits from customers with similar names are also included in the running balance.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    287

    Re: Need changes in Statement to get the correct balance amount ...

    Quote Originally Posted by leinad31
    Because you used LIKE to specify the customer ... so what happens is debits and credits from customers with similar names are also included in the running balance.
    Hi, It shows the exact CODE (Customer Code) as it is in the last post which determines that it retrieved the data for the same customer not other customers whose name is similar.

    Regards,

    Margaret

  13. #13
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Need changes in Statement to get the correct balance amount ...

    Check that there are no nulls in the table for debits/credits... and set the relevant fields to default to 0 rather than null to ensure that subtraction can be performed on all rows (can't subtract null).

  14. #14
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Need changes in Statement to get the correct balance amount ...

    When you post your SQL string would you mind, please, putting a couple of CARRIAGE RETURNS in the string so that it's not 3 feet wide!

    It's impossible to look at and understand when you cannot see the whole string in one screen view!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    287

    Re: Need changes in Statement to get the correct balance amount ...

    Quote Originally Posted by leinad31
    Check that there are no nulls in the table for debits/credits... and set the relevant fields to default to 0 rather than null to ensure that subtraction can be performed on all rows (can't subtract null).
    I am using the following code (to set the format in MSHFlexGrid to show):

    VB Code:
    1. Dim Y As Long
    2.     With MSHFlexGrid1
    3.     For Y = .FixedRows To .Rows - 1
    4.         .TextMatrix(Y, 8) = Format(.TextMatrix(Y, 8), ("#,##.00;;\- "))
    5.         Next Y
    6.         End With

    Regards,

    Margaret

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    287

    Re: Need changes in Statement to get the correct balance amount ...

    Quote Originally Posted by szlamany
    When you post your SQL string would you mind, please, putting a couple of CARRIAGE RETURNS in the string so that it's not 3 feet wide!

    It's impossible to look at and understand when you cannot see the whole string in one screen view!
    Yes, you are correct. But I don't know how to cut it into small pieces/lines.

    Margaret

  17. #17
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Need changes in Statement to get the correct balance amount ...

    First of all - you are giving us the SQL statement inside a VB string - we really just need to discuss the SQL statement...

    But at any rate...

    VB Code:
    1. rs.Open "SELECT CustomerName, Option, FileNumber, City, Telephone" _
    2.       & ", Fax, DDate, RP, InvoiceAmount As Credit, AmountReceived As Debit" _
    3.       & ",(SELECT SUM(InvoiceAmount-AmountReceived) FROM SalesData SD2" _
    4.       & " WHERE SD2.DDate <= SD1.DDate AND SD2.CustomerName=" _
    5.       & " SD1.CustomerName) As Balance FROM SalesData SD1" _
    6.       & " Where CustomerName like '%" & txtSearchAnyWord _
    7.       & "%'ORDER BY CustomerName, DDate", db
    but even better like this...

    VB Code:
    1. SELECT CustomerName, Option, FileNumber, City, Telephone
    2.           , Fax, DDate, RP, InvoiceAmount As Credit, AmountReceived As Debit
    3.           ,(SELECT SUM(InvoiceAmount-AmountReceived) FROM SalesData SD2
    4.                  WHERE SD2.DDate <= SD1.DDate AND SD2.CustomerName=
    5.                               SD1.CustomerName) As Balance
    6.             FROM SalesData SD1
    7.                   Where CustomerName like '%SMITH%'
    8.                        ORDER BY CustomerName

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    287

    Re: Need changes in Statement to get the correct balance amount ...

    Quote Originally Posted by szlamany
    Where CustomerName like '%SMITH%'
    Hi, there are more than 150 customers in the database and I need to select any customer from the database to get the statement. So, I put one textbox with name txtSearchAnyWord.

    Margaret

  19. #19
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Need changes in Statement to get the correct balance amount ...

    That was just an example - we don't need to battle the "build a SQL string in VB issues" when trying to figure out why a query does not work.

    What is wrong with this query:

    Code:
    SELECT CustomerName, Option, FileNumber, City, Telephone
              , Fax, DDate, RP, InvoiceAmount As Credit, AmountReceived As Debit
              ,(SELECT SUM(InvoiceAmount-AmountReceived) FROM SalesData SD2
                     WHERE SD2.DDate <= SD1.DDate AND SD2.CustomerName=
                                  SD1.CustomerName) As Balance
                FROM SalesData SD1
                      Where CustomerName like '%SMITH%'
                           ORDER BY CustomerName
    Obviously change the NAME to one that is in your database.

    Does it not return a BALANCE that you expect?

    As indicated by LEINAD31 if either the InvoiceAmount or the AmountReceived is null in any record for a given customer then that SELECT SUM()... will be wrong.

    Did you see if any of these amounts are null?

    Answer these two questions and post back the query you used most recently that did not work.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  20. #20
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Need changes in Statement to get the correct balance amount ...

    Its not advisable to use date for determining the running balance, rows on the same end day will be included in the calculation when they should be excluded cause they follow the row whose running balance is to be determined. That's why we used autonumber ID as the basis for the running balance, since its unique and indicates the order of the addition of the records... which raises another point, your view should follow the same ascending order as the ID.

    Based on post number 9, your ordering by description then by date, the IDs are in jumbled order... so the running balance based on ID will give the wrong result.

  21. #21

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    287

    Re: Need changes in Statement to get the correct balance amount ...

    Quote Originally Posted by leinad31
    Its not advisable to use date for determining the running balance, rows on the same end day will be included in the calculation when they should be excluded cause they follow the row whose running balance is to be determined. That's why we used autonumber ID as the basis for the running balance, since its unique and indicates the order of the addition of the records... which raises another point, your view should follow the same ascending order as the ID.

    Based on post number 9, your ordering by description then by date, the IDs are in jumbled order... so the running balance based on ID will give the wrong result.
    Hi,

    Thanks a lot for your advice. Finally, what should be the right code that I have to write now (Cause I have modified it so many times).

    Regards,

    Margaret

  22. #22
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Need changes in Statement to get the correct balance amount ...

    Quickest fix would be to display the records ordered on ID... if you will follow description, code, etc sort then you will have to implement the running balance progmatically ...which also means you will have to get the starting balance based on the lowest ID, or total balance for IDs before lowest ID (note: not first ID in returned recordset sorted by description, etc), in the returned recordset, then iterate through the grid/listview control and do arithmetic on the debits/credits to get next balance.

  23. #23

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    287

    Re: Need changes in Statement to get the correct balance amount ...

    Quote Originally Posted by leinad31
    Quickest fix would be to display the records ordered on ID... if you will follow description, code, etc sort then you will have to implement the running balance progmatically ...which also means you will have to get the starting balance based on the lowest ID, or total balance for IDs before lowest ID (note: not first ID in returned recordset sorted by description, etc), in the returned recordset, then iterate through the grid/listview control and do arithmetic on the debits/credits to get next balance.
    Thanks a lot for your usual cooperation. Since I am a learner, still have confusion which code to follow.

    Once again thanks for the help.

    Regards,

    Margaret

  24. #24
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Need changes in Statement to get the correct balance amount ...

    Note that the subquery for subtotal works on the assumption that the records it returns are the records before and up to the parent query's current record. The subquery and its parent query will have to use the same "yardstick" for the running balance to work. If the subquery is based on order of ID's while its parent query orders by description, code then you have different yardsticks or it doesn't work regardless of the condition in your WHERE clause.

    Imagine your earning 200 a day from sunday to saturday, getting running balance based on ID is similar to running balance based on weekday (or better day in year); 400 on day 2, 600 on day 3 etc. If the parent query sorted the days alphabetically it will start on Friday day 6. Instead of running balance 200 for friday as first day, the subquery based on yearday will return 1200.

    It gets more complicated as you go father away from the order based on unique, ordinal (I HAVE to stress that so you won't accidentally base running balance thru query on non-unique, non-ordinal fields), autonumbered ID.

    So either display records based on common yardstick ID and not by description, code, etc fields. Or calculate the running balance using VB code at run-time (not thru query) after loading the debits/credits in the grid-like control, you will also need the starting balance based on earliest ID in returned recordset.

  25. #25

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    287

    Re: Need changes in Statement to get the correct balance amount ...

    Quote Originally Posted by leinad31
    Note that the subquery for subtotal works on the assumption that the records it returns are the records before and up to the parent query's current record. The subquery and its parent query will have to use the same "yardstick" for the running balance to work. If the subquery is based on order of ID's while its parent query orders by description, code then you have different yardsticks or it doesn't work regardless of the condition in your WHERE clause.

    Imagine your earning 200 a day from sunday to saturday, getting running balance based on ID is similar to running balance based on weekday (or better day in year); 400 on day 2, 600 on day 3 etc. If the parent query sorted the days alphabetically it will start on Friday day 6. Instead of running balance 200 for friday as first day, the subquery based on yearday will return 1200.

    It gets more complicated as you go father away from the order based on unique, ordinal (I HAVE to stress that so you won't accidentally base running balance thru query on non-unique, non-ordinal fields), autonumbered ID.

    So either display records based on common yardstick ID and not by description, code, etc fields. Or calculate the running balance using VB code at run-time (not thru query) after loading the debits/credits in the grid-like control, you will also need the starting balance based on earliest ID in returned recordset.
    Hi,

    Thanks a lot for your detailed description.

    With best regards,

    Margaret

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