Solved

How to connect VB to ODBC ( SQL Server ) ?

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

773 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