473,387 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Custom SQL Order By.

I'm trying to create a custom order by that builds a statement based on the order of the array. I'm starting to think that SQL Case statement is not supported by MS Access. Or possibly there is a syntax error, on my part.
Here is the String building loop..
Expand|Select|Wrap|Line Numbers
  1.     strSQL = "Case [" & FieldName & "] "
  2.  
  3.     For z = intFirst To intLast
  4.  
  5.         strSQL = strSQL & _
  6.             "WHEN " & SortOrder(z) & _
  7.             " THEN " & forCount & " "
  8.  
  9.         forCount = forCount + 1
  10.  
  11.     Next z
  12.  
  13.     strSQL = strSQL & "END;"
  14.  
  15.  
Then i set the query.SQL property to this string. And it gives error 3075 Syntax error (missing operator).

Here is the main sql statement.

Expand|Select|Wrap|Line Numbers
  1.     sqlSort = "SELECT * FROM [table1] ORDER BY (" & _
  2.         CustomSort(Sorts(), FieldName) & ")"
  3.  
CustomSort is a function, returns string.

If this method is not supported, is there a way to do this in access, without adding a separate identifying column?
I just really want a generic and quick way to do this.
Oct 28 '10 #1
1 1817
I couldn't find any other way, so for conclusion I am using this function... the 'DoesFieldExist' function is another function. GetFirstPos and GetLastPos is so that I can throw any array at it. I just have an order by sql clause that sorts by the _sort column.


Expand|Select|Wrap|Line Numbers
  1. Function CustomSortField(ByRef SortOrder() As String, FieldName As String, TableName As String) As String
  2.  
  3.     Dim intFirst As Integer, intLast As Integer
  4.     Dim strSQL As String, z As Integer, forCount As Integer
  5.     Dim Tbl As TableDef, fld As Field, Rst As Recordset, dbs As DAO.Database
  6.  
  7.     '===
  8.     'Get First Position
  9.     'Generalization
  10.     '===
  11.     intFirst = GetFirstPos(SortOrder())
  12.  
  13.     '===
  14.     'Get Last Position
  15.     'Generalization
  16.     '===
  17.     intLast = GetLastPos(SortOrder())
  18.     '===============================
  19.     Set dbs = CurrentDb
  20.  
  21.     DoCmd.Close acTable, TableName, acSaveYes
  22.     '===
  23.     'Add Column to end of table.
  24.     '===
  25.     If DoesFieldExist(TableName, FieldName & "_Sort") = True Then _
  26.         DoCmd.RunSQL "ALTER TABLE [" & TableName & "] DROP COLUMN [" & FieldName & "_Sort" & "]"
  27.  
  28.         Set Tbl = dbs.TableDefs(TableName)
  29.  
  30.         'Create New column
  31.         Set fld = Tbl.CreateField(FieldName & "_Sort", dbLong)
  32.         'fld.Properties("ColumnHidden").Value = True
  33.         Tbl.Fields.Append fld
  34.  
  35.         Set fld = dbs.TableDefs(TableName).Fields(FieldName)
  36.  
  37.         Set fld = Nothing
  38.         Set Tbl = Nothing
  39.  
  40.  
  41.     forCount = 0
  42.  
  43.     Set Rst = dbs.OpenRecordset(TableName)
  44.  
  45.     For z = intFirst To intLast
  46.  
  47.         With Rst
  48.         If Rst.RecordCount = 0 Then GoTo ZeroRecord
  49.             .MoveFirst
  50.  
  51.             Do
  52.  
  53.                 If Left(.Fields(FieldName).Value, Len(SortOrder(z))) = SortOrder(z) Then
  54.  
  55.                     .Edit
  56.                     .Fields(FieldName & "_Sort").Value = forCount
  57.                     .Update
  58.  
  59.                 End If
  60.  
  61.                 .MoveNext
  62.  
  63.             Loop Until .EOF
  64.         End With
  65.  
  66.         forCount = forCount + 1
  67.  
  68.     Next z
  69.  
  70.     CustomSortField = FieldName & "_Sort"
  71.  
  72.     Exit Function
  73. ZeroRecord:
  74.     CustomSortField = ""
  75.  
  76. End Function
  77.  
  78. Private Function GetFirstPos(ByRef acArray() As String)
  79.  
  80.     intFirst = -1
  81.  
  82.     Do
  83.  
  84.         intFirst = intFirst + 1
  85.  
  86.     Loop Until acArray(intFirst) <> ""
  87.  
  88.     GetFirstPos = intFirst
  89.  
  90.  
  91. End Function
  92.  
  93. Private Function GetLastPos(ByRef acArray() As String) As Integer
  94.     Dim intLast As Integer
  95.  
  96.     On Error GoTo OverFlowArray
  97.     Do
  98.  
  99.         Debug.Print acArray(intLast)
  100.         intLast = intLast + 1
  101.  
  102.     Loop
  103.  
  104. FindLastPos:
  105.     intLast = intLast - 1
  106.     On Error GoTo 0
  107.  
  108.     Do
  109.  
  110.         intLast = intLast - 1
  111.  
  112.     Loop Until acArray(intLast) <> ""
  113.  
  114.     GetLastPos = intLast
  115.  
  116. ExitHere:
  117.     Exit Function
  118. OverFlowArray:
  119.     Err.Clear
  120.     Resume FindLastPos
  121. End Function
  122.  
Nov 11 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Craig | last post by:
Does anyone know whether you can take a dataset and serialise into a custom collection? Over the past couple of months I've been retreiving my datasets and manually iterating them to create my...
1
by: Tom L | last post by:
I'm pretty new to .net stuff, but not new to programming (lots of vb, sql, cold fusion, and some asp).. I have a "middle layer" object model I created, that has all my business rules and database...
1
by: Jeff S | last post by:
I'm storing a list of widgets in a database. The list changes infrequently (twice per week at most), and is relatively short (200 items at most, with very little detail per item). A small subset of...
21
by: One Handed Man \( OHM - Terry Burns \) | last post by:
When using a custom control. In order to check and see if values have changed one has to implement the IPostBackDataCollection interface. The values returned for the control seem to be simply a...
0
by: Vi | last post by:
Hi, I have a form that lists orders from a database. All orders have the same format. Each order has an order header that consists of: - a few labels with orders numbers and some general details...
3
by: Chris Newby | last post by:
I have a very simple custom control that derives from WebControls.Panel and implements INamingContainer. It appear that controls created as children of my custom control are having ViewState...
7
by: Girish | last post by:
OK.. phew. Playing with data grids for the past few days has been fun and a huge learning experience.. My problem. I have a requirement to display a gird with a gird. Within the embedded grid,...
2
by: Suzanne | last post by:
Hi all, I'm reposting this message as I'm experiencing this problem more and more frequently : I really hope someone out there can help me as I've been tearing my hair out on this one for a...
0
by: jacobmarble | last post by:
Hey NG- I'm trying to order a column such that it's in order by pinyin, hence an ORDER BY clause would change this: ma mà má ma ma
2
by: Dennis Jones | last post by:
Hello, I have a class that will eventually look something like this: class TTableHolder { private: boost::scoped_ptr<TSessionFSession; boost::shared_ptr<TTableFTable;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.