Consulting

Results 1 to 9 of 9

Thread: sql excel vba connection problem

  1. #1

    sql excel vba connection problem

    hi all,
    when i run this code, i would get "run time error -2147467259
    automation error unspecified error"
    i used debug, and the error accord in oCon.Open
    please help me.

    [VBA]Sub Connect2SQLXpress()
    Dim oCon As ADODB.Connection
    Dim oRS As ADODB.Recordset
    Set oCon = New ADODB.Connection
    oCon.ConnectionString = "Driver={SQL Native Client};Server=.\SQLEXPRESS;Database=DB1; Trusted_Connection=yes;" oCon.Open
    Set oRS = New ADODB.Recordset
    oRS.ActiveConnection = oCon
    oRS.Source = "Select * From Table1"
    oRS.Open Range("A1").CopyFromRecordset oRS
    oRS.Close
    oCon.Close
    If Not oRS Is Nothing Then Set oRS = Nothing
    If Not oCon Is Nothing Then Set oCon = Nothing
    End Sub[/VBA]
    Last edited by Bob Phillips; 02-07-2012 at 02:37 AM. Reason: Added VBA tags

  2. #2
    VBAX Tutor mohanvijay's Avatar
    Joined
    Aug 2010
    Location
    MADURAI
    Posts
    268
    Location
    Try different connection strings like below (Which works fine for me)

     
    "Provider=SQLNCLI;Server=YourServerName;Database=YourDataBaseName;Uid=UserName;Pwd=Password;"
    for more connection strings see http://www.connectionstrings.com/

  3. #3
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Are you sure the server name is right?

    Does it definitely hav ./ at the start?

  4. #4
    Quote Originally Posted by mohanvijay
    Try different connection strings like below (Which works fine for me)

     
    "Provider=SQLNCLI;Server=YourServerName;Database=YourDataBaseName;Uid=UserName;Pwd=Password;"
    hi,
    first, thanks for you replay
    second, i had tried u suggestion and a new error accord at the same line as before.
    RUN-time error 3706 "application defined or odject defined error"
    i have ado 2.8 library attached in my available ref.
    also i want to mention that i tried excel feature of exporting external data form sql server and it worked fine.

    i want to apologise for my late response, i had a hard week.
    thx again.

    here is my code:

    Sub Connect2SQLXpress2()

    Dim oCon As ADODB.Connection
    Dim oRS As ADODB.Recordset

    Set oCon = New ADODB.Connection
    oCon.ConnectionString = "Provider=SQLNCLI;Server=n13088c;Database=ALEX;"
    oCon.Open

    Set oRS = New ADODB.Recordset
    oRS.ActiveConnection = oCon
    oRS.Source = "Select * From dbo.emp"
    oRS.Open

    Range("A1").CopyFromRecordset oRS

    oRS.Close

    oCon.Close

    If Not oRS Is Nothing Then Set oRS = Nothing
    If Not oCon Is Nothing Then Set oCon = Nothing

    End Sub

  5. #5
    Quote Originally Posted by Norie
    Are you sure the server name is right?

    Does it definitely hav ./ at the start?
    hi,
    first, thanks for you replay
    i want to apologise for my late response, i had a hard week.
    i posted my update code

  6. #6
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Where do you get the latest error?

  7. #7
    hi
    oCon.Open - plz see the update code above,

  8. #8
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    I can't see anything wrong with the code and I've never had an error like that when working with ADO.

    I actually thought at first it might be an Excel VBA error, but if it's happening on oRS I can't see how that would be possible.

    Which ADO library did you add?

    There are 2 with the version number 2.8, one of them is for labelled Mult-Dimensional and that's the one you don't want.

  9. #9

    finally i found something that works

    hi,
    i found this code on web, and it's worked like charm!

    i'm guessing it because of the provider SQLOLEDB.1
    thnks for the help .

    good day

    Sub Add_Results_Of_ADO_Recordset()
    'This was set up using Microsoft ActiveX Data Components version 2.8

    Dim cnt As ADODB.Connection
    Dim rst As ADODB.Recordset
    Dim stSQL As String
    Dim wbBook As Workbook
    Dim wsSheet As Worksheet
    Dim rnStart As Range


    Const stADO As String = "Provider=SQLOLEDB.1;Integrated Security=SSPI;" & _
    "Persist Security Info=False;" & _
    "Initial Catalog=ALEX;" & _
    "Data Source=N13088C"


    Set wbBook = ActiveWorkbook
    Set wsSheet = wbBook.Worksheets(1)

    With wsSheet
    Set rnStart = .Range("A1")
    End With


    stSQL = "INSERT INTO dbo.emp (id, name, age) " & _
    " VALUES (" & _
    "'" & Sheet1.Cells(1, 1) & "', " & _
    "'" & Sheet1.Cells(1, 2) & "', " & _
    "'" & Sheet1.Cells(1, 3) & "')"

    Set cnt = New ADODB.Connection

    cnt.Open stADO
    cnt.Execute(stSQL)

    cnt.Close
    Set rst = Nothing
    Set cnt = Nothing

    End Sub

Posting Permissions

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