Solved

Connecting to an Sql Server Database using VB6 (DAO)

Posted on 2003-12-04
3
2,877 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

705 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