0
Sub Run()

  Call ConnectDB

    Dim Cmd As ADODB.Command
    Dim rcs As ADODB.Recordset
    Dim SQL As String
    Dim res() As String

    Set Cmd = New ADODB.Command
    Set Cmd.ActiveConnection = con

    SQL = "select tl.id, al.price_crossing, al.price_exchange_fees, tl.charges_execution, tl.charges_mariana, tl.charges_exchange, tl.trade_date, un.value, tl.nb_crossing  from mfb.trade_leg tl" & _
    "inner join mfb.trade t on t.id = tl.id_trade" & _
    "inner join mfb.instrument i on t.id_instrument = i.id" & _
    "inner join mfb.instrument_type it on it.id = i.id_instrument_type" & _
    "inner join mfb.options o on o.id_instrument = i.id" & _
    "inner join mfbref.mfb.underlying un on un.id = o.id_underlying" & _
    "inner join mfb.allocation_leg al on al.id_trade_leg = tl.id" & _
    "where tl.trade_date > '20160101' and t.state = 3"

    Cmd.CommandText = SQL
    Set rcs = Cmd.Execute()

Every time I execute this code an debug error appears saying incorrect syntax near 'tl'. I've tries executing the SQL in different ways but the error won't change

4
  • Perhaps you have missed a whitespace before inner join statements Commented Sep 20, 2016 at 12:57
  • 1
    you don't have a space between lines. Commented Sep 20, 2016 at 12:57
  • I'm not about the spaes and whether you need to put the AS before tl. Commented Sep 20, 2016 at 12:57
  • 1
    It didn't seem to matter in this example, but in the future make sure you include what database software you are using. They have different syntaxes that can change the answer you get. (Again, this was a pretty universal issue, but it could bite you in the future) Commented Sep 20, 2016 at 13:13

3 Answers 3

2
SQL = "select tl.id, al.price_crossing, al.price_exchange_fees, tl.charges_execution, tl.charges_mariana, tl.charges_exchange, tl.trade_date, un.value, tl.nb_crossing  from mfb.trade_leg AS tl " & _
"inner join mfb.trade t on t.id = tl.id_trade " & _
"inner join mfb.instrument i on t.id_instrument = i.id " & _
"inner join mfb.instrument_type it on it.id = i.id_instrument_type " & _
"inner join mfb.options o on o.id_instrument = i.id " & _
"inner join mfbref.mfb.underlying un on un.id = o.id_underlying " & _
"inner join mfb.allocation_leg al on al.id_trade_leg = tl.id " & _
"where tl.trade_date > '20160101' and t.state = 3 "
Sign up to request clarification or add additional context in comments.

Comments

1
SQL = "select tl.id, al.price_crossing, al.price_exchange_fees, tl.charges_execution, tl.charges_mariana, tl.charges_exchange, tl.trade_date, un.value, tl.nb_crossing  from mfb.trade_leg tl " & _
"inner join mfb.trade t on t.id = tl.id_trade " & _
"inner join mfb.instrument i on t.id_instrument = i.id " & _
"inner join mfb.instrument_type it on it.id = i.id_instrument_type " & _
"inner join mfb.options o on o.id_instrument = i.id " & _
"inner join mfbref.mfb.underlying un on un.id = o.id_underlying " & _
"inner join mfb.allocation_leg al on al.id_trade_leg = tl.id " & _
"where tl.trade_date > '20160101' and t.state = 3;"

you left out tons of spaces

3 Comments

Thank you. This helped me so much
give a guy a thumbs up! and dont forget to mark someones answer as "answered"
also its worth nothing that due to spacing being an issue with embedded sql, its good to develop a pattern. I personally tyr to leave my spaces at the end of each line. Setting up a pattern of bgehavior will help you trouble shoot in the future. @PaulKuriakides
0

You should use "as" when assigning aliases to your SQL objects.

Try changing

     ...from mfb.trade_leg tl...
     ...join mfb.trade t...
     ...join mfb.options o ...

to

     ...from mfb.trade_leg as tl...
     ...join mfb.trade as t...
     ...join mfb.options as o ...

1 Comment

1) This isn't the root cause of the syntax error. 2) It's actually a range variable. 3) Opinion based e.g. I prefer to omit the AS because it encourages the wrong thinking i.e. table as x wrongly implies that x is an alias.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.