Solved

ODBC 3.0 and VB

Posted on 1997-06-01
2
291 Views
Last Modified: 2006-11-17
Trying to use ODBC 3.0 with VB4, unfortunately all examples in ODBC 3.0 SDK are in C language, any idea
from where I can get VB examples on a Internet, can you
write at least SQL AllocHandle part for me in VB.
Thank a million
Matthew
0
Comment
Question by:mativare
2 Comments
 

Accepted Solution

by:
markbob061097 earned 50 total points
ID: 1426685
The following is what I use to connect to an ODBC database.  You must supply the values for the gsDataSource (ODBC Datasource), gsUserID (Database UserID), and gsPassword (Database password) variables.  If the fODBCConnect returns a 0 then it connected to the database correctly.

A book that has a chapter on ODBC programming in Visual Basic which I learned from is "Teach Yourself Database Programming with Visual Basic 4" from SAMS Publishing.

' 32-bit ODBC API Declares (Remove 32 from dll for 16-bit systems)
  Declare Function SQLAllocEnv Lib "odbc32.dll" (env As Long) As Integer
  Declare Function SQLAllocConnect Lib "odbc32.dll" (ByVal env As Long, hdbc As Long) As Integer
  Declare Function SQLAllocStmt Lib "odbc32.dll" (ByVal hdbc As Long, hstmt As Long) As Integer
  Declare Function SQLConnect Lib "odbc32.dll" (ByVal hdbc As Long, ByVal Server As String, ByVal serverlen As Integer, ByVal uid As      String, ByVal uidlen As Integer, ByVal pwd As String, ByVal pwdlen As Integer) As Integer

' SQL/ODBC Constants
  Public Const SQL_SUCCESS = 0
  Public Const SQL_NO_DATA_FOUND = 100

' ODBC Variables
  Public glHenv As Long           ' Environment Handle
  Public glHdbc As Long           ' Database Connection
  Public glHstmt As Long          ' Statement Handle
  Public gsDataSource As String   ' Data Source Name (You Provide Value)
  Public gsUserID As String       ' User ID (You Provide Value)
  Public gsPassword As String     ' User Password (You Provide Value)

Public Function fODBCConnect() As Integer

    ' Declare Variables
        Dim nResult As Integer

    ' Get Environment Handle
        nResult = SQLAllocEnv(glHenv)
    If nResult = SQL_SUCCESS Then
    ' Get Connection Handle
        nResult = SQLAllocConnect(glHenv, glHdbc)
    If nResult = SQL_SUCCESS Then
    ' Connect to Database
        nResult = SQLConnect(glHdbc, gsDataSource, Len(gsDataSource), gsUserID, Len(gsUserID), gsPassword, Len(gsPassword))
    If nResult = SQL_SUCCESS Then
    ' Get Statement Handle
        nResult = SQLAllocStmt(glHdbc, glHstmt)
    End If
    End If
    End If

    ' Set Return Value
        fODBCConnect = nResult

End Function
0
 
LVL 1

Author Comment

by:mativare
ID: 1426686
Response excelent, but still in your sample ODBC 2,0 used
I was looking for ODBC 3.0 sample. It all begins with
SQLAllocHandle(), SQlAllocEnv() is obsolete in ODBC 3.0
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
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…
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…
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…

762 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

23 Experts available now in Live!

Get 1:1 Help Now