Results 1 to 5 of 5

SQL join syntax error

Threaded View

  1. #1
    Registered User
    Join Date
    04-07-2013
    Location
    belfast
    MS-Off Ver
    Excel 2010
    Posts
    26

    SQL join syntax error

    Hi



    I am getting a syntax error in FROM clause when I attempt to run some VBA with sql Join.

    I get no errors when attempting simple select queries on either table.



    The tables are as below.



    NameL

    ID NameV Age
    1 Mark 30
    2 George 22
    3 Andrew 20


    NameR

    ID NameW Salary
    1 mark 100000
    2 George 500000
    3 Arnold 50000




    The code is as follows:
    Sub ConnectAccessJoin()
     Dim MyConnection As ADODB.Connection
     Dim MyRecordset As ADODB.Recordset
     Dim MyQuery As String
     Set MyConnection = New ADODB.Connection
     Set MyRecordset = New ADODB.Recordset
     MyConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=C:\Users\mark0\OneDrive\Documents\ASampleDatabase.accdb"
     MyQuery = "Select * FROM NameR Join NameL ON (NameV=NameW);" 'backtick required if table name is an SQL reserved word or table name has spaces in it. ie `Names` is a reserved word and `Asset List` has gaps
    ' MyQuery = "Select ID, NameW FROM NameR where NameW = 'Mark';" 'backtick required if table name is an SQL reserved word or table name has spaces in it. ie `Names` is a reserved word and `Asset List` has gaps
    ' MyQuery = "Select * FROM NameR;" 'backtick required if table name is an SQL reserved word or table name has spaces in it. ie `Names` is a reserved word and `Asset List` has gaps
    'Open the connection
    MyConnection.Open MyConnectionString
    'Execute the query
     Set MyRecordset = MyConnection.Execute(MyQuery)
     'Copy the output of query in sheet1
     
     
    ' The following code extracts the field names from the table
    ' With Sheets("Sheet5") 'With can be used or not. If it is used then put a full stop in front of Cells(2,i).Value....
     For i = 1 To MyRecordset.Fields.Count
     Cells(2, i).Value = MyRecordset.Fields(i - 1).Name 'fields is a 0 based collection
     Next i
    ' .Range("A3").CopyFromRecordset MyRecordset
    'End With
     Range("A3").CopyFromRecordset MyRecordset
    ' Worksheets("Sheet5").Range("A3").CopyFromRecordset MyRecordset 'You can choose to reference the worksheet or not depends if you are going to another sheet
     'Closing the connection and setting the values of declared variables as Nothing or simply to NULL.
     MyRecordset.Close
     Set MyRecordset = Nothing
     MyConnection.Close
     Set MyConnection = Nothing
     End Sub
    Any ideas as to what I am doing incorrect?



    Both database and excel file with code are attached



    thanks
    Attached Files Attached Files
    Last edited by redmarko; 11-09-2020 at 04:37 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Runtime Error '3075': Syntax error in the string in the query expression [SQL]
    By leegrj in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-28-2020, 04:11 PM
  2. Replies: 5
    Last Post: 11-28-2019, 04:55 AM
  3. Why the syntax error with the Join function?
    By Mark43 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-14-2019, 03:09 PM
  4. [SOLVED] Syntax on line continuations around LEFT JOIN
    By ejoneslor in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-09-2016, 03:31 AM
  5. Excel macro (compile error. syntax error.) error
    By salar_younis in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-06-2014, 06:11 AM
  6. getting an error- a script out of range - Join function
    By AB33 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-16-2012, 01:29 PM
  7. Syntax Error and Compile Error: Expected Line Number or Label...
    By AnthonyWB in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-31-2011, 09:59 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1