Consulting

Results 1 to 2 of 2

Thread: Field Name unknown in SQL with VBA excel

  1. #1

    Field Name unknown in SQL with VBA excel

    Hi,
    Please check code SQL query .
    If use in query : SELECT *
    FROM Table1.Table2
    WHERE Table1.sid=Table2.sid ,have Resulted.

    If use this code : SELECT Table1.sid, Table1.name,Table2.company
    FROM Table1.Table2
    WHERE Table1.sid=Table2.sid ,have no Reuslted.If this code run ,show message box ( SQL syntax error '1004').


    Why unknown (Field Name) in VBA excel with ODBC?

    Please Help me!

    [vba]Sub query1()
    Sheets.Add
    ActiveSheet.Name = n & "-SQL1"
    sSQL = "SELECT Shop.idshop,Shop.sname,Laptop.lname,Laptop.ltype,Laptop.price" _
    & "FROM Shop,Laptop " _
    & "WHERE Shop.idshop=Laptop.idshop" _
    & "ORDER BY Laptop.price" _

    ActiveCell.Range("C1") = sSQL
    With ActiveSheet.QueryTables.Add(Connection:=Array(Array( _
    "ODBC;DSN=Test;DBQ=H:\myfolder\VBA\Test"), _
    Array("\Test.mdb;DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;")), _
    Destination:=Range("A4"))
    .CommandText = Array(sSQL)
    .Name = "Query From Test"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = True
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
    .Refresh BackgroundQuery:=False
    End With
    End Sub

    [/vba]

  2. #2
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    Your join is incorrect - I would suggest that you either build queries in Access and copy the SQL string, or do some reading on the syntax of SQL.

    [VBA]
    sSQL = "SELECT Shop.idshop,Shop.sname,Laptop.lname,Laptop.ltype,Laptop.price " & _
    "FROM Shop INNER JOIN Laptop ON Shop.idshop = Laptop.idshop " & _
    "ORDER BY Laptop.price"

    [/VBA]

Posting Permissions

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