Solved

How to connect VB to ODBC ( SQL Server ) ?

Posted on 1998-05-01
2
217 Views
Last Modified: 2012-05-04
How can I write a VB 5 application that can connect to a SQL Server ?
( I already know the steps for adding SQL Server to ODBC )
Can I just directly use a data control to connect the SQL Server ?

Thanks a lot
0
Comment
Question by:stanleylcm
2 Comments
 
LVL 1

Accepted Solution

by:
jcastr earned 50 total points
ID: 1436632
Better connect directly to ODBC  SQL Server DataSource.
To do this in VB5 just follow this

1. In References check DAO 2.5/3.5
2. Create in a Module

Public DB as Database
Public Rs as Recordset


Function OpenDB () as boolean
On error goto ErrHandler

    OpenDB = false
     Set DB = OpenDatabase("MSSQL", false,false,"ODBC;DSN=MSSQL;DATABASE=MyDB;UID=sa;PWD=;")

    OpenDB = True

Out:
     exit sub

ErrHandler:
      Msgbox Error,vbInformation
      Resume Out

end function


now you can execute directly SQL Commands if your OpenDB was successful, try with

if you're querying data

Set Rs = DB.OpenRecordset (SQL, dbOpenTable,dbSQLPAssthrough)

if you are using SQL DDL/DML

DB.Execute SQL, dbSQLPAssthrough
0
 

Expert Comment

by:BogdanGrama
ID: 2802333
You can put this in a module

Public Function fConnectSQL(ByVal sqldsn As String, ByVal sqluser As String, ByVal sqlpass As String) As Boolean
        fConnectSQL = True
        Err = 0
        'La connessione con la SQL Server
        Set connserv = New ADODB.Connection
        connserv.ConnectionString = "DSN=" & sqldsn & ";UID=" & sqluser & ";PWD=" & sqlpass
        connserv.Open
        If Err <> 0 Then
            fConnectSQL = False
            Set connserv = Nothing
        End If
End Function

Send a note if it works
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…

706 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

19 Experts available now in Live!

Get 1:1 Help Now