Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Incorrect syntax near the keyword ''IN'' error Expand / Collapse
Author
Message
Posted Sunday, January 30, 2005 3:10 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, October 19, 2005 1:59 AM
Points: 3, Visits: 1

Hi,

i'm trying to insert data queried from SQL server into an Excel sheet using the following query.

"INSERT INTO [sheet1$] IN '" & App.Path & strWBName & ".xls' 'Excel 8.0;' Select * from TEST.dbo.Testing"

 Later i execute the query using the SQL server connection.

I'm getting following error:

Run-Time error '-214721900 (80040e14)':

[Microsoft][ODBC SQL Server Driver][SQL Server] Incorrect syntax near the keyword 'IN'".

Can anyone help me out of the situation?

Thanks in advance

Regards

Naresh

 

Post #158409
Posted Monday, January 31, 2005 2:29 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, May 23, 2006 6:15 AM
Points: 45, Visits: 1
Have you thought about using a querytable in excel. It might be easier?


Post #158464
Posted Monday, January 31, 2005 7:26 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 10:00 AM
Points: 6,917, Visits: 6,991

This is the format you want

"INSERT INTO OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database='" & App.Path & strWBName + "', [sheet1$]) (col1,col2,col3) SELECT col1,col2,col3 FROM TEST.dbo.Testing'"

Note that the spreadsheet must exist and have columnheaders in row 1 (e.g. col1,col2,col3 above) it will not work otherwise

Otherwise use DTS

 




Far away is close at hand in the images of elsewhere.

Anon.

Post #158515
Posted Monday, January 31, 2005 7:27 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, May 23, 2006 6:15 AM
Points: 45, Visits: 1
Much easier to use a query table unless you have a specific reason not too!


Post #158516
Posted Monday, January 31, 2005 8:27 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, July 17, 2014 4:30 PM
Points: 281, Visits: 233

I looked up 'Query Table' in Excel to see what you were talking about, couldn't find it. Are you talking about importing the data into the Excel sheet, from Excel, by using a database query? In other words, importing the data fro within, rather than injecting it from without?

Just curious, thought I might learn something if I ask...

 




Post #158539
Posted Monday, January 31, 2005 8:31 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, May 23, 2006 6:15 AM
Points: 45, Visits: 1

Yes. In Excel goto Data/Get External Data.New Database query. Follow the steps. It's fairly straightforward and is a good quick way to display your database data in a spreadsheet. Hope it's helpful. Sounds like you might be interested in pivot tables as well (same sort of thing, but you can split the data up and get counts, averages etc).

cheers

Andy




Post #158543
Posted Monday, January 31, 2005 8:44 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 10:00 AM
Points: 6,917, Visits: 6,991

I create hidden spreadsheet in a workbook with a macro that automatically starts when the workbook is opened and creates a new workbook, populates it with the data and closes the original workbork leaving the data behind. The user can then choose what they do with the results.

The code is standard apart from the connection string and the stored procedure used. It creates the headings from the sql column names and the data type to determine data format.




Far away is close at hand in the images of elsewhere.

Anon.

Post #158547
Posted Tuesday, February 1, 2005 4:13 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, April 9, 2014 7:34 AM
Points: 53, Visits: 28
Can you please paste the macro code in here...I guess it would be great to learn its functionality.

Naveen
Post #158728
Posted Tuesday, February 1, 2005 4:22 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 10:00 AM
Points: 6,917, Visits: 6,991

Here you go

Sub Auto_Open()
    Dim SQL
    Dim r As Integer
    Dim c As Integer
    Dim i As Integer
    Dim OldBook As Workbook
    Dim NewBook As Workbook
    Dim Conn As ADODB.Connection
    Dim RS As ADODB.Recordset
    Set OldBook = ThisWorkbook
    Set Conn = New ADODB.Connection
    connstr = "connection string here"
    SQL = "sql here"
    Conn.Open connstr
    Set RS = Conn.Execute(SQL)
    Workbooks.Add
    r = 1
    If RS.EOF = False Then
        i = RS.Fields.Count - 1
        For c = 0 To i
            ActiveSheet.Cells(r, c + 1).Value = RS.Fields(c).Name
        Next c
    End If
    With Range(ActiveSheet.Cells(r, 1), ActiveSheet.Cells(r, i + 1)).Interior
        .ColorIndex = 15
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
    End With
    With Range(ActiveSheet.Cells(r, 1), ActiveSheet.Cells(r, i + 1)).Borders
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    Do Until RS.EOF
        r = r + 1
        For c = 0 To i
            Select Case RS.Fields(c).Type
                Case 3    'int
                    ActiveSheet.Cells(r, c + 1).NumberFormat = "0"
                    ActiveSheet.Cells(r, c + 1).Value = RS(c)
                Case 131  'Numeric
                    ActiveSheet.Cells(r, c + 1).NumberFormat = "#,#0.00"
                    ActiveSheet.Cells(r, c + 1).Value = Val(RS(c))
                Case 135  'Date
                    ActiveSheet.Cells(r, c + 1).NumberFormat = "dd/mm/yyyy"
                    ActiveSheet.Cells(r, c + 1).HorizontalAlignment = xlLeft
                    ActiveSheet.Cells(r, c + 1).Value = RS(c) & ""
                Case 200  'varchar
                    ActiveSheet.Cells(r, c + 1).NumberFormat = "@"
                    ActiveSheet.Cells(r, c + 1).Value = RS(c) & ""
                Case Else
                    ActiveSheet.Cells(r, c + 1).Value = RS(c) & ""
            End Select
        Next c
        RS.MoveNext
    Loop
    ActiveSheet.Columns.AutoFit
    ActiveSheet.Range("A1").Select
    RS.Close
    Conn.Close
    OldBook.Close
End Sub




Far away is close at hand in the images of elsewhere.

Anon.

Post #158730
Posted Tuesday, February 1, 2005 4:48 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, May 23, 2006 6:15 AM
Points: 45, Visits: 1

That's not a query table though... It can be cumbersome going through a recordset record by record. This is the VBA to add a query table. You need to put your DSN name in and obviously change the command text to the sql you want to use.

Public Sub CreateQueryTable()
    With ActiveWorkbook.ActiveSheet.QueryTables.Add(Connection:= _
        "ODBC;DSN=YOURDSNNAME" _
        , Destination:=Range("A1"))
        .CommandText = Array("SELECT * FROM TESTTABLE")
        .Name = "WHATEVERQUERYNAMEYOULIKE"
        .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


 

 




Post #158733
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse