Wrox Programmer Forums
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 8th, 2006, 03:23 PM
Registered User
 
Join Date: Jun 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default vba ado error

i am trying to connect to a database and to populate my spreadsheet but i am getting an error. i evoke the database connection with a click command on a worksheet.

here is my connection:

Sub pubDerConn()
Dim objConn As ADODB.connection
Dim objRS As ADODB.Recordset
Dim stSQL As String
Dim sql$, i&

Set objConn = CreateObject("ADODB.Connection")
Set objRS = CreateObject("ADODB.Recordset")
Set ws = ThisWorkbook.Worksheets("intro")
objConn.Open "Provider=SQLOLEDB;data source=SQLSERVER;initial catalog=Data;UserId=PublicUser;Password=pass"
sql = "SELECT field FROM database"
rec.Open sql, objConn
While Not objRS.EOF
i = i + 1
ws.[a1].Cells(i) = rec!field
rec.MoveNext
Wend
rec.Close: conn.Close

End Sub

i get this error: compile error: user-defined type not defined;
and this line is highlighted: Dim objConn As ADODB.connection

thanks
 
Old June 9th, 2006, 03:11 AM
Authorized User
 
Join Date: Mar 2006
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You are mixing early binding with late binding..
Either reference the Microsoft ActiveX Data Objects 2.x library
in which case you dont need to CreateObject.

Or for late binding

Code:
Dim objConn As Object
Dim objRS As Object
 
Old June 13th, 2006, 09:57 PM
Registered User
 
Join Date: Jun 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

maybe like this:

Sub pubDerConn()
    Dim sql$, i&
    Set objConn = CreateObject("ADODB.Connection")
    Set objRS = CreateObject("ADODB.Recordset")
    Set ws = ThisWorkbook.Worksheets("intro")
    objConn.Open "Provider=sqloledb;Server=SQLSERVER;Database=Data; UserId=PublicUser;Password=pass"
    sql = "SELECT field FROM database1"
    objRS.Open sql, objConn
    While Not objRS.EOF
        i = i + 1
        ws.[a1].Cells(i) = objRS!field
        objRS.MoveNext
    Wend
    objRS.Close: objConn.Close
End Sub






Similar Threads
Thread Thread Starter Forum Replies Last Post
ADO.NET error gb012993 BOOK: Beginning C# 2005 Databases 2 May 9th, 2007 03:10 AM
Copyfromrecordset error using ADO maaron Excel VBA 0 August 11th, 2006 08:52 AM
Ado.Net Error digby_dog VB.NET 2002/2003 Basics 2 June 7th, 2005 04:29 AM
ADO Error -2147217913 viper7 VB Databases Basics 1 March 2nd, 2005 12:31 PM
Oracle9i and ADO error?? philVT2000 Pro VB Databases 1 March 1st, 2005 11:54 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.