Question about sorting in VB + SQL query

Asked By 410 points N/A Posted on -

I want to retrieve already saved records from the database of SQL. But the issue is that whenever I want to retrieve multiple records with sorting on some fields selected in table/query, it shows syntax error. Help me out what can be issue and how can I make the query in the given situation?????

Answered By 0 points N/A #90560

Question about sorting in VB + SQL query



First of all you don’t have mentioned query in detail or just idea of query. I think the scenario you are discussing is about the making of query when you want to sort out the record in term of ascending or descending order. 

sql = " SELECT     VDetail. VoucherID, VDetail. VoucherDate, VDetail .VoucherNo, "

sql = sql & " VDetail. AccountNo, Accounts. AccountTitle, Accounts. AccountType,

sql = sql & " FROM         VDetail LEFT OUTER JOIN"

sql = sql & " Accounts ON VDetail. AccountNo = Accounts. AccountNo "

sql = sql & " ORDER BY Accounts. AccountType DESC, VDetail. AccountNo"

In the given query you can see that I have selected Voucher ID, Voucher Date, Voucher No, and Account No from the VDetail table and Account Title, Account Type from the table of Accounts. Then I use LEFT OUTER JOIN between both tables as a connection. After that I make condition on both tables that the query will select the records where Account No of VDetail is equal to the Account No of Accounts. And in last I use ORDER BY condition to sort the records. Here I want to show the records in descending order in terms of Account Type that is why I use DESC after mentioning Account type field. Keep in mind two things:

1: all records are by default in ascending order when you use ORDER BY condition. For descending order you need to mention DESC after the required field.

2: the filed you will mention before DESC will only be sort out in descending order. Other fields will not be affected.


Login/Register to Answer

Related Questions