Print Page | Close Window

SQL sorting

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Database Discussion
Forum Description: Discussion and chat on database related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=4550
Printed Date: 30 March 2026 at 12:17pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: SQL sorting
Posted By: Boecky
Subject: SQL sorting
Date Posted: 27 July 2003 at 8:27am

I just want to know if this is the best way to do this. It's for sorting a list with 2 parameters (strSort and strType). Or I use both, only strSort or only strType or Nothing. Any other suggestions pls let me know.

If strSort = "" AND strType = "" Then
 rsList.Source = "SELECT * FROM tblList"
 ElseIf strSort <> "" AND strType = "" Then
 rsList.Source = "SELECT * FROM tblList ORDER BY " & strSort & " ASC"
 ElseIf strSort = "" AND strType <> "" Then
 rsList.Source = "SELECT * FROM tblList WHERE Type=" & strType2 & ""
 ElseIf strSort <> "" AND strType <> "" Then
 rsList.Source = "SELECT * FROM tblList WHERE Type=" & strType2 & " ORDER BY " & strSort & " ASC"
End If




Replies:
Posted By: Boecky
Date Posted: 29 July 2003 at 2:40am
nobody can gives a meaning?


Posted By: b_bonnett
Date Posted: 29 July 2003 at 3:27am

Personally I would do it as follows:

strSQL = "Select * From tblList "
If strType <> "" Then
  strSQL = strSQL & "Where Type=" & strType2 & " "
End If
If strSort <> "" Then
  strSQL = strSQL & "Order By " & strSort & " Asc "
End If
rsList.Source = strSQL

Hope this helps,
Blair



-------------
Webmaster, http://www.planegallery.net/ - The Plane Gallery
Greetings From Christchurch


Posted By: Boecky
Date Posted: 30 July 2003 at 9:16am
Ok tkx, but is this faster or more 'optimized' then an 'ElseIf'?


Posted By: b_bonnett
Date Posted: 30 July 2003 at 4:52pm

Don't know, they're both using If...ElseIf blocks, so they should be about the same. The main reason I do it that way is because it is easier, and makes neater code, than writing all the combinations yourself. It is not so noticeable with just two options, but if you have four options, then there are (I think) 12 different combinations.

Blair



-------------
Webmaster, http://www.planegallery.net/ - The Plane Gallery
Greetings From Christchurch



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net