Solved

How to connect VB to ODBC ( SQL Server ) ?

Posted on 1998-05-01
2
224 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
[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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
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…

724 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