Results 1 to 5 of 5

Problem with VBA/ADO Select Into

Threaded View

JohnM3 Problem with VBA/ADO Select... 06-16-2011, 11:14 AM
snb Re: Problem with VBA/ADO... 06-16-2011, 11:28 AM
JohnM3 Re: Problem with VBA/ADO... 06-16-2011, 11:59 AM
romperstomper Re: Problem with VBA/ADO... 06-16-2011, 11:58 AM
JohnM3 Re: Problem with VBA/ADO... 06-16-2011, 12:06 PM
  1. #1
    Forum Contributor
    Join Date
    12-26-2010
    Location
    Kansas City, Kansas
    MS-Off Ver
    Excel 2007
    Posts
    147

    Problem with VBA/ADO Select Into

    When I run this routine - I get
    Unexpected error from external database driver (1309)

    Not certain if (a) I can do Select into (b) my sql syntax is wrong or (c) I have
    left something out ....

    Here is code ....

    
    Public Sub Main_AppendNewWS()
        
        '-------------------
        '   Goal
        '-------------------
        'Attempt to do a SQL  SELECT INTO .....
        'One WS to another WS in same WB
        
        '-------------------
        '   Error I am getting
        '-------------------
        'Error Message
        '   Unexpected error from external database driver (1309)
        'Error occurs on oCMD.execute (BELOW)
        'SQL used is in SQL property SQL2 (BELOW)
        
        
        On Error GoTo EH_Main_AppendNewWS
    
        '-------------------
        '   WS
        '-------------------
        Const CON_WS_CONTROL As String = "CONTROL"
        Const CON_WS_ANSWER As String = "ANSWER"
        Const CON_WS_WRT As String = "WRT"
    
        '-------------------
        '   Variables
        '-------------------
        Dim UseCMD As Boolean
        Dim ws_Answer As String
        Dim ws_WRT As String
    
        '-------------------
        '   ADO Variables
        '-------------------
        Dim oCN As ADODB.Connection
        Dim oCmd As ADODB.Command
        Dim oRS As ADODB.Recordset
    
        '-------------------
        '   Init
        '-------------------
        ws_Answer = CON_WS_ANSWER
        ws_WRT = CON_WS_WRT
        UseCMD = True
        
        '-------------------
        '   Init Connection - Provider/Data Source/Extended Properties
        '   Details Shown AFTER If
        '-------------------
        If 1 = 2 Then
            Init_Connection
        End If
        
        '-------------------
        '   Init Properties - Provider
        '-------------------
        PS = "Microsoft.ACE.OLEDB.12.0;"
        
        '-------------------
        '   Init Properties - Data Source
        '-------------------
        DS = vbNullString
        DS = "Data Source="
        DS = DS & ThisWorkbook.Path & "\"
        DS = DS & ThisWorkbook.Name
        DS = DS & ";"
        
        '-------------------
        '   Init Properties - Extended Properties
        '-------------------
        EP = "Extended Properties="
        EP = EP & """Excel 12.0 Xml;HDR=YES;"""
            
        '-------------------
        '   Connection String
        '-------------------
        CS = DS & EP
    
        '-------------------
        '   Connect - Opens OK
        '-------------------
        Set oCN = Nothing
        Set oCN = New ADODB.Connection
        With oCN
            .Provider = PS
            .ConnectionString = CS
            .Open
        End With
    
        '-------------------
        '   SQL1 -
        '   Not Used - Works OK when used
        '-------------------
        If Not UseCMD Then
            SQL1 = vbNullString
            SQL1 = SQL1 & "Select "
            SQL1 = SQL1 & "GIZID, "
            SQL1 = SQL1 & "ABBR "
            SQL1 = SQL1 & "From "
            SQL1 = SQL1 & "["
            SQL1 = SQL1 & ws_Answer
            SQL1 = SQL1 & "$] "
            SQL_Active = SQL1
        End If
        
        '-------------------
        '   SQL2 -
        '   SQL being Used which results in Error
        '-------------------
        If UseCMD Then
            SQL2 = vbNullString
            SQL2 = SQL2 & " Select "
            SQL2 = SQL2 & " GIZID, "
            SQL2 = SQL2 & " ABBR "
            SQL2 = SQL2 & " INTO "
            SQL2 = SQL2 & "[" & ws_WRT & "]"
            SQL2 = SQL2 & " From "
            SQL2 = SQL2 & "["
            SQL2 = SQL2 & ws_Answer
            SQL2 = SQL2 & "$] "
            SQL_Active = SQL2
        End If
    
        '-------------------
        '   Command - FAILS - Error Occurs on EXECUTE
        '-------------------
        If UseCMD Then
            Set oCmd = Nothing
            Set oCmd = New ADODB.Command
            With oCmd
                .ActiveConnection = oCN
                .CommandText = SQL_Active
                .CommandType = adCmdText
                .Execute
            End With
        End If
    
        '-------------------
        '   RS
        '-------------------
        If Not UseCMD Then
            Set oRS = Nothing
            Set oRS = New ADODB.Recordset
            With oRS
                .ActiveConnection = oCN
                .CursorLocation = adUseClient
                .CursorType = adOpenDynamic
                .LockType = adLockOptimistic
                .Source = SQL_Active
                .Open
            End With
        End If
    
        '-------------------
        '   Close RS
        '-------------------
        If Not oRS Is Nothing Then
            If oRS.State = 1 Then
                oRS.Close
                Set oRS = Nothing
            End If
        End If
    
        '-------------------
        '   Close CMD
        '-------------------
        If Not oCmd Is Nothing Then
            If oCmd.State = 1 Then
                Set oCmd = Nothing
            End If
        End If
    
        '-------------------
        '   Close Connection
        '-------------------
        If Not oCN Is Nothing Then
            If oCN.State = 1 Then
                oCN.Close
                Set oCN = Nothing
            End If
        End If
    
        Exit Sub
    
    EH_Main_AppendNewWS:
    
        MsgBox Err.Number & " " & Err.Description, vbCritical, "Public Sub Main_AppendNewWS()"
    
        '-------------------
        '   Close RS
        '-------------------
        If Not oRS Is Nothing Then
            If oRS.State = 1 Then
                oRS.Close
                Set oRS = Nothing
            End If
        End If
    
        '-------------------
        '   Close RS
        '-------------------
        If Not oCmd Is Nothing Then
            If oCmd.State = 1 Then
                Set oCmd = Nothing
            End If
        End If
    
        '-------------------
        '   Close Connection
        '-------------------
        If Not oCN Is Nothing Then
            If oCN.State = 1 Then
                oCN.Close
                Set oCN = Nothing
            End If
        End If
    
        Exit Sub
    
    End Sub
    Thanks for any help with this.

    regards
    JohnM3
    Last edited by JohnM3; 06-16-2011 at 12:13 PM.

Thread Information

Users Browsing this Thread

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

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