Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ODBC 3.0 and VB

Posted on 1997-06-01
2
Medium Priority
?
318 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
[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
 

Accepted Solution

by:
markbob061097 earned 200 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library 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…
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…
Suggested Courses

670 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