Solved

Connecting to SQL Server from VB6

Posted on 2007-11-23
12
541 Views
Last Modified: 2012-05-05
Hello,

I'm trying to connect to a SQL Server (2005) database from Visual Basic 6.0.  The attached code produces no errors, yet never fills the dataset.

Thanks in advance for your help.


Dim thisWorkspace As DAO.Workspace
    Set thisWorkspace = CreateWorkspace("", "admin", "", dbUseODBC)
    
    Dim thisConnection As DAO.Connection
    Set thisConnection = thisWorkspace.OpenConnection("", , , "ODBC;DATABASE=KnowledgeTracker;DSN=KTracker")
    
    Set gdbKnowledgeTracker = thisConnection.Database
    
    Dim rs As DAO.Recordset
    
    Dim sSQL As String
    sSQL = "SELECT * FROM Problems"
    Set rs = gdbKnowledgeTracker.OpenRecordset(sSQL)

Open in new window

0
Comment
Question by:NigelRocks
  • 5
  • 4
  • 3
12 Comments
 
LVL 10

Assisted Solution

by:3_S
3_S earned 300 total points
ID: 20340368
Hello NigelRocks,

this works from VB6 to sql server 2000. I do not have MS sql 2005
Change SQLSERVER with your servername and databasename with the databasename you want to use

set thisconnection = new adodb.connection
set rs = new adodb.recordset
thisconnecton.open = "Provider=sqloledb;Network Library=DNETLIB;datasource=SQLSERVER,1433;Initial catalog=KnowledgeTracker;Trusted Connection=Yes;Encrypt=YES"
rs.activeconnection = thisconnection
rs.source = "SELECT * FROM Problems"
rs.open , , adOpenForwardOnly, adLockReadOnly
'Use your recordset
rs.close
thisConnection.close
set rs = nothing
set thisConnection = nothing
0
 
LVL 1

Author Comment

by:NigelRocks
ID: 20340526
Are you sure that what you posted is VB6 code?  My compiler choked on this:

    Dim thisConnection As Connection
    Set thisConnection = New Adodb.Connection

0
 
LVL 10

Assisted Solution

by:3_S
3_S earned 300 total points
ID: 20344384
yes,

but you have to add the following reference in VB6
Microsoft ActiveX Data Objects 2.8 Library
2.8 can vary depending the version of MDAC you have installed. (2.8 is default win xp)

Control the following line in you code
Dim thisConnection as ADODB.Connection

wiht databasename I mean the initial catalog, I gues in your case this is KnowledgeTracker
good luck
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 65

Expert Comment

by:Jim Horn
ID: 20345605
>My compiler choked on this: ...  Set thisConnection = New Adodb.Connection

That's because DAO does not have a Connection object, only a Database object.  ADO (ADODB) has a connection object.

The code 3_S posted for you using ADO only should work for you.
0
 
LVL 1

Author Comment

by:NigelRocks
ID: 20346224
I added the Microsoft ActiveX Data Objects 2.8 Library reference.

I got a "variable not defined" error on this line;
thisconnecton.Open = "Provider=sqloledb;Network Library=DNETLIB;datasource=SQLSERVER,1433;Initial catalog=KnowledgeTracker;Trusted Connection=Yes;Encrypt=YES"


These two lines above it define the object:
Dim thisConnection As ADODB.Connection
 Set thisConnection = New ADODB.Connection



If this code has worked for you, there must be something VERY different about our IDEs.

0
 
LVL 10

Expert Comment

by:3_S
ID: 20352472
Did you copy paste the line with the 'variable not defined' error
thisconnecton is not the variable you defined:  thisConnection is  (letter i is missing in you posted code)

You can check this by typing thisconnection and then the point.  If no propositions are made then you typed your variable wrong.
I would also recommend that you always add option explicit to every form/module/class you add in a project.  With this option you must declare every variable else you get compiler error to warn you.

I use VB6 with sp5
0
 
LVL 10

Expert Comment

by:3_S
ID: 20352481
Sorry. The i was a typo of mine in thisConnection
0
 
LVL 1

Author Comment

by:NigelRocks
ID: 20352572
Yes, it was a typo.

I also got an error on:

thisConnection.Open = "  "

It has to be

thisConnection.Open(" ")


I'll get back to you about what happens.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 20352591
{very wild guess}

Lose the = sign

thisconnecton.open "Provider=sqloledb;Network Library=DNETLIB;datasource=SQLSERVER,1433;Initial catalog=KnowledgeTracker;Trusted Connection=Yes;Encrypt=YES"
0
 
LVL 1

Author Comment

by:NigelRocks
ID: 20352609
It doesn't like this line:


thisConnection.Open ("Provider=sqloledb;Network Library=DNETLIB;datasource=SQLSERVER,1433;Initial catalog=KnowledgeTracker;Trusted Connection=Yes;Encrypt=YES")


Where in the above string would I tell it what server to use?

0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 200 total points
ID: 20352635
http://www.connectionstrings.com is an excellent resource for peicing together connection strings.

{another wild guess, I'm on a roll....}

Lose the parentheses as well, as they imply that you are trying to return a value/reference.

thisConnection.Open "Provider=sqloledb;Network Library=DNETLIB;datasource=SQLSERVER,1433;Initial catalog=KnowledgeTracker;Trusted Connection=Yes;Encrypt=YES"
0
 
LVL 1

Author Comment

by:NigelRocks
ID: 20353011
This is the string that did it:


thisConnection.Open "Driver={SQL Server};Server=COPS14\COPSDEV;Database=KnowledgeTracker;Trusted_Connection=Yes;"
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Modifying Conditional Format from VBA code 3 60
INSERT DATE FROM STRING COLUMN 18 59
Unable to save view in SSMS 21 73
SQL query with cast 38 55
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question