John said:
The alternative would be to have some pretty snarky and inefficient
VBA code to generate a sortkey which could deal with .10. values
anywhere in the string (or even .100. if that ever comes up).
Just for fun, I decided to try and write some SQL code to do the same
(probably still 'pretty snarky and inefficient') ... but I ran into a
problem. Can anyone who speaks SQL take a look at this for me?
Sub test1()
With CurrentProject.Connection
..Execute _
"CREATE TABLE WbsNumbers (" & _
"wbs_nbr VARCHAR(22) NOT NULL);"
..Execute _
"INSERT INTO WbsNumbers VALUES (" & _
"'A.5.12.1.6.5');"
..Execute _
"INSERT INTO WbsNumbers VALUES (" & _
"'A.5.3.4.11');"
..Execute _
"INSERT INTO WbsNumbers VALUES (" & _
"'A.4.2.1');"
..Execute _
"INSERT INTO WbsNumbers VALUES (" & _
"'A.1.2');"
..Execute _
"INSERT INTO WbsNumbers VALUES (" & _
"'A.1');"
..Execute _
"CREATE TABLE Sequence (" & _
"seq INTEGER NOT NULL);"
..Execute _
"INSERT INTO Sequence VALUES (1);"
..Execute _
"INSERT INTO Sequence VALUES (2);"
..Execute _
"INSERT INTO Sequence VALUES (3);"
..Execute _
"INSERT INTO Sequence VALUES (4);"
..Execute _
"INSERT INTO Sequence VALUES (5);"
..Execute _
"INSERT INTO Sequence VALUES (6);"
..Execute _
"INSERT INTO Sequence VALUES (7);"
..Execute _
"INSERT INTO Sequence VALUES (8);"
..Execute _
"INSERT INTO Sequence VALUES (9);"
..Execute _
"INSERT INTO Sequence VALUES (10);"
..Execute _
"INSERT INTO Sequence VALUES (11);"
..Execute _
"INSERT INTO Sequence VALUES (12);"
..Execute _
"INSERT INTO Sequence VALUES (13);"
..Execute _
"INSERT INTO Sequence VALUES (14);"
..Execute _
"INSERT INTO Sequence VALUES (15);"
..Execute _
"INSERT INTO Sequence VALUES (16);"
..Execute _
"INSERT INTO Sequence VALUES (17);"
..Execute _
"INSERT INTO Sequence VALUES (18);"
..Execute _
"INSERT INTO Sequence VALUES (19);"
..Execute _
"INSERT INTO Sequence VALUES (20);"
..Execute _
"INSERT INTO Sequence VALUES (21);"
..Execute _
"INSERT INTO Sequence VALUES (22);"
..Execute _
"CREATE VIEW WbsSeq AS " & _
"SELECT WbsNumbers.wbs_nbr, Sequence.seq " & _
"FROM WbsNumbers, Sequence " & _
"WHERE MID$(WbsNumbers.wbs_nbr, Sequence.seq, 1) = '.';"
..Execute _
"CREATE VIEW WbsSortKeyPos AS " & _
"SELECT T1.wbs_nbr, T1.seq AS pos, " & _
"(SELECT COUNT(*)" & _
"FROM WbsSeq AS T2 " & _
"WHERE T2.wbs_nbr = T1.wbs_nbr " & _
"AND T2.Seq <= T1.Seq" & _
") AS sort_key " & _
"FROM WbsSeq AS T1;"
End With
End Sub
So far so good, but when I try the following SQL I get an error 'No
value given for one or more required parameters':
Sub test2()
Dim rs As Object
Set rs = CurrentProject.Connection.Execute( _
"SELECT T1.wbs_nbr, T1.sort_key, " & _
"T1.pos AS pos1, " & _
"(SELECT MIN(T2.pos) " & _
"FROM WbsSortKeyPos AS T2 " & _
"WHERE T2.wbs_nbr = T1.wbs_nbr " & _
"AND T2.pos > T1.pos) AS pos2 " & _
"FROM WbsSortKeyPos AS T1")
End Sub
The problem is the T1.pos in the subquery. Change it to, say, T2.pos >
T2.pos and it runs without error.
No doubt it is a limitation of the Jet engine because it works OK in
SQL Server 2000 (just replace MID$ with SUBSTRING) but I can't see
what limit I'm hitting.
Any ideas?
Thanks,
Jamie.
--