Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hoe to fix run time error 2147217900 (80040e14) syntax error in Order By clause 1

Status
Not open for further replies.

feipezi

IS-IT--Management
Aug 10, 2006
316
US
Hello,

I cannot figure out what's wrong with the syntax on the SQL statements. It's not a complicated process, is it?
All I want is to check out if the input dataset has been updated. So I can run my process if it has.
The query that I ran was in Excel ADO. But the dataset resides in MS Access. The variable TRANDATE is defined in Access as 'Number', not 'date'. I tried 'Dim TRNADATE as Integer', 'as Long', 'as date'. They all gave me error like 'ByRef ... Mismatch'. So I picked 'Variant'. But it gave me a diff error msg as shown in the subject. Maybe 'Variant' has nothing to do with the errors.

Let me know if you want the whole program.

Please advise.
Thanks.

Sub ExportAccessDBtoExcel(dsn As String, DTE As Long)
...
ConnCmd.CommandText = "SELECT TOP 50 * FROM " & dsn _
& "ORDER BY " & DTE & "DESC"
...
Sub tnt()
ExportAccessDBtoExcel "CO_PNC_CO_RPT_NBEXTRACT", TRANDATE
End Sub
 
You may need two additional spaces, before ORDER and DESC (... & " ORDER BY " & DTE & " DESC").

When debugging, I would print for testing the sql file in procedure:
[tt]strSQL = "SELECT TOP 50 * FROM " & dsn & " ORDER BY " & DTE & " DESC"
Debug.Print strSQL
ConnCmd.CommandText = strSQL[/tt]

combo
 
Thanks combo for the quick return and good tips!
 
Hello combo,

I need more help from you. You can see the code below.
I have 10 data tables to be checked out making sure those tabs are updated, i.e., having the latest data. But each tab has different date to check on. The example below shows TERM_START_DT; others may have RUN_DATE or something like that. That's why I need to set up a date variable, apart from 'dsn as string'. But I tried a few options, like DTE as Long, DTE as date and so on but none of them working.

Please advise.
Thanks again combo


Sub SlTable1(dsn As String, DTE As Date, S_DT As String, E_DT As String)

Dim ConnObj As ADODB.Connection
Dim RecSet As ADODB.Recordset
Dim ConnCmd As ADODB.Command
Dim ColNames As ADODB.Fields
Dim DataSource As String
Dim intlp As Integer

Cells.Clear

DataSource = "S:\shared\BIS_Reports\Reserving and Reinsurance Database\DataExtractDB.accdb"

Set ConnObj = New ADODB.Connection
Set ConnCmd = New ADODB.Command

With ConnObj
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = DataSource
.Open
End With

ConnCmd.ActiveConnection = ConnObj
strSQL = "SELECT TOP 10 * FROM " & dsn _
& " WHERE " & DTE & " BETWEEN " & S_DT & " AND" & E_DT & " ORDER BY" & DTE & " DESC"
Debug.Print strSQL
ConnCmd.CommandText = strSQL

ConnCmd.CommandType = adCmdText

Set RecSet = ConnCmd.Execute
Set ColNames = RecSet.Fields

For intlp = 0 To ColNames.Count - 1
Cells(1, intlp + 1).Value = ColNames.Item(intlp).Name
Next

Range("A2").CopyFromRecordset Data:=RecSet
ActiveSheet.Name = dsn

ConnObj.Close
End Sub

Sub tnt()
SlTable1 "CO_PNC_CO_POLICY_TERM", TERM_START_DT, #4/15/2023#, #4/30/2023#
End Sub
 
In:
[tt]strSQL = "SELECT TOP 10 * FROM " & dsn _
& " WHERE " & DTE & " BETWEEN " & S_DT & " AND" & E_DT & " ORDER BY" & DTE & " DESC"[/tt]
DTE is a field name used in sql string, why it is declared as date?

After [tt]Debug.Print strSQL[/tt] in the immediate window you should see the generated sql string, it is the first place to check it.

If #date# works in query and the input is somewhere in the workbook as date, I would create a simple function to convert date to valid string:
[pre]Function DateToSQL(InputDate As Date) As String
DateToSQL = "#" & Format(InputDate, "m/d/yyyy") & "#"
End Function[/pre]

combo
 
Looks to me in your Sub SlTable1 you use dsn as table name, DTE as field's name of the date field, S_DT as starting date, and E_DT as end date.

If so, why not un-confuse everybody here (including yourself) and use:
[tt]
Sub SlTable1(strTblName As String, strDateFieldName As String, datS_DT As Date, datE_DT As Date)[/tt]

So your Select would look like:
[tt]strSQL = "SELECT TOP 10 * FROM " & strTblName _
& " WHERE " & strDateFieldName & " BETWEEN #" & datS_DT & "# AND #" & datE_DT & "# ORDER BY " & strDateFieldName & " DESC"[/tt]

Just an example of naming variables...

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top