×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

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

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

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

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:
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

(OP)
Thanks combo for the quick return and good tips!

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

(OP)
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

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

Step 1: put a space after AND and a space after ORDER BY

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

In:
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:
Function DateToSQL(InputDate As Date) As String
    DateToSQL = "#" & Format(InputDate, "m/d/yyyy") & "#"
End Function 

combo

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

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:

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close