Solved

Connecting to an Sql Server Database using VB6 (DAO)

Posted on 2003-12-04
3
2,729 Views
Last Modified: 2009-10-09
I've converted from using an access database to sql Server.  I have always used DAO.  I am aware that DAO has it's contraints, but for now will continue with it.  I plan to move to ado gradually.  

I am unable to connect to my database - Kept getting "ODBC call failed".  Here is code I used:

[

Global wsPayroll As Workspace

Global dbPayroll As Connection
Global dbUser As Connection
Global dbs As Database
Global MyPath As String                
Global MyDbPath As String              
Global MyDbName As String              
Global MyAppPath As String            

Global s As String

Open App.Path & "\Payroll.txt" For Input As #1

Input #1, s
MyPath = Right(s, Len(s) - 3)
If Right(s, 1) <> "\" Then
    MyPath = MyPath & "\"
End If
   
Input #1, s
MyAppPath = Right(s, Len(s) - 4)
If Right(s, 1) <> "\" Then
    MyAppPath = MyAppPath & "\"
End If

Close #1


MyDbName = "Paysql_data"
   
MyDbPath = MyPath & MyDbName & ".mdf"


Set wsPayroll = CreateWorkspace("NewODBCWorkspace", "admin", "", dbUseODBC)


Set dbPayroll = wsPayroll.OpenConnection("ConPayroll", _
       dbDriverNoPrompt, , _
       "ODBC;DATABASE=Paysql_data.mdf;UID=Administrator;PWD=;FILEDSN=Paysql")
]

I get the error when executing the last line of code above.

Any help would be good.

Thnaks
0
Comment
Question by:33216CLC
  • 2
3 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 9880599
dim oConn as adodb.connection
set oConn as new adodb.connection
oConn.Open "Provider=sqloledb;" & _
           "Data Source=myServerName;" & _
           "Initial Catalog=myDatabaseName;" & _
           "User Id=myUsername;" & _
           "Password=myPassword

See http://www.able-consulting.com/MDAC/ADO/Connection/OLEDB_Providers.htm#OLEDBProviderForSQLServer for more information about connectionstrings
0
 

Author Comment

by:33216CLC
ID: 9915951
Dhaest,

Thank you for the response.  The code you gave works for ADO, but I'm using DAO.  Do you have the code for making the connection with DAO?  The one I use presently only works with An Access Database, but not sql Server.  I'm using SQL Server 7.0.
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 250 total points
ID: 9918523
Dim ws As DAO.Workspace
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim cn As DAO.Connection

Set ws = CreateWorkspace("", "admin", "", dbUseODBC)
Set cn = ws.OpenConnection _("", , , "ODBC;DATABASE=MyDB;DSN=MYDSN")
Set db = cn.Database
Set rs = db.OpenRecordset("Table1", dbOpenDynamic)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now