I'm sure there is a really simple solution for this matter, but I have tried all I know without success.
I'm trying to modify this fully Working SQL string to yield only the Top 25 results, not all results. Spaces corrected on this post. (I have used the TOP 25, etc. with Jet/Access databases for years with no problems)
"SELECT * FROM " & "data.CSV" & " ORDER BY " & "Elapsed" & " DESC "*
For the Top 25 SQL syntax I have tested:
1."SELECT TOP 25 FROM " & "data.CSV" & " ORDER BY " & "Elapsed" & " DESC "
2."SELECT TOP 25 * FROM " & "data.CSV" & "ORDER BY " & "ELAPSED" & " DESC "
3."SELECT TOP 25" & " ORDER BY " & "ELAPSED" & " DESC " & FROM" & " data.CSV"
None of these three produce any results from the 45 line test data file. No errors indicated, all compile.
Edit: Corrected spaces of working string. Actual code in use is correct. Corrected missing double quote line 2.
Any advice appreciated.
Edit: Code added.
Private Sub Command4_Click() '*** Print direct to report ***
Dim Cn1 As ADODB.Connection
Dim rs As ADODB.Recordset
Dim fld As ADODB.Field
Dim strData, iSQLStr As String
Dim sData() As String
Dim c, i, s As Integer
On Error Resume Next
Set Cn1 = New ADODB.Connection
Cn1.ConnectionString = _
"Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
"DefaultDir=" & "C:\"
Cn1.Open
Set rs = New ADODB.Recordset
rs.Fields.Append "Room", adVarChar, 2
rs.Fields.Append "Procedure", adVarChar, 2
rs.Fields.Append "Group", adVarChar, 12
rs.Fields.Append "Label", adVarChar, 12
rs.Fields.Append "Time_In", adVarChar, 22
rs.Fields.Append "Time_Out", adVarChar, 22
rs.Fields.Append "Elapsed", adVarChar, 8
rs.Open
'iSQLStr = "Select TOP 25" & " ORDER BY " & "Elapsed" & " DESC " & "FROM" & " data.csv"
'iSQLStr = "Select TOP 25 * FROM" & " data.csv" & " ORDER BY " & "Elapsed" & " DESC "
iSQLStr = "Select * FROM " & "data.csv" & " ORDER BY " & "Elapsed" & " DESC "
Set rs = Cn1.Execute(iSQLStr)
Set DataReport1.DataSource = rs
DataReport1.Show vbModal
rs.Close
End Sub
"SELECT TOP 25 * FROM " & "data.CSV" & "ORDER BY " & "ELAPSED" & DESC "
Is the one that looks closest to working except it's missing some spaces.
What language is this in? I don't know what the ampersands and quotes are all meaning, but if you can build the string like this it should work:
SELECT TOP 25 * FROM data.CSV ORDER BY ELAPSED DESC
Following the syntax you're using I think this has the correct spacing:
"SELECT TOP 25 * FROM " & "data.CSV " & "ORDER BY " & "ELAPSED " & DESC"