Home Forums SQL Server 7,2000 T-SQL Incorrect syntax near the keyword ''IN'' error RE: Incorrect syntax near the keyword ''''IN'''' error

  • 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.