Solved

ODBC 3.0 and VB

Posted on 1997-06-01
2
292 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…

920 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

15 Experts available now in Live!

Get 1:1 Help Now