?
Solved

Connecting to an Sql Server Database using VB6 (DAO)

Posted on 2003-12-04
3
Medium Priority
?
3,106 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 1000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…
Suggested Courses

864 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