Hoe to fix run time error 2147217900 (80040e14) syntax error in Order By clause
Hoe to fix run time error 2147217900 (80040e14) syntax error in Order By clause
(OP)
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
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
RE: Hoe to fix run time error 2147217900 (80040e14) syntax error in Order By clause
When debugging, I would print for testing the sql file in procedure:
strSQL = "SELECT TOP 50 * FROM " & dsn & " ORDER BY " & DTE & " DESC"
Debug.Print strSQL
ConnCmd.CommandText = strSQL
combo
RE: Hoe to fix run time error 2147217900 (80040e14) syntax error in Order By clause
RE: Hoe to fix run time error 2147217900 (80040e14) syntax error in Order By clause
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
RE: Hoe to fix run time error 2147217900 (80040e14) syntax error in Order By clause
RE: Hoe to fix run time error 2147217900 (80040e14) syntax error in Order By clause
strSQL = "SELECT TOP 10 * FROM " & dsn _
& " WHERE " & DTE & " BETWEEN " & S_DT & " AND" & E_DT & " ORDER BY" & DTE & " DESC"
DTE is a field name used in sql string, why it is declared as date?
After Debug.Print strSQL 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:
combo
RE: Hoe to fix run time error 2147217900 (80040e14) syntax error in Order By clause
If so, why not un-confuse everybody here (including yourself) and use:
Sub SlTable1(strTblName As String, strDateFieldName As String, datS_DT As Date, datE_DT As Date)
So your Select would look like:
strSQL = "SELECT TOP 10 * FROM " & strTblName _
& " WHERE " & strDateFieldName & " BETWEEN #" & datS_DT & "# AND #" & datE_DT & "# ORDER BY " & strDateFieldName & " DESC"
Just an example of naming variables...
---- Andy
"Hmm...they have the internet on computers now"--Homer Simpson