Solved

Connecting to an Sql Server Database using VB6 (DAO)

Posted on 2003-12-04
3
2,710 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

760 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

18 Experts available now in Live!

Get 1:1 Help Now