Article ID: 110470
Article Last Modified on 6/12/2001
' function must be declared on one line!<BR/> Declare Function SQLBindCol Lib "odbc.dll" Alias "SqlBindCol"<BR/> (ByVal hstmt As Long, ByVal icol As Integer, ByVal fCType As Integer, rgbValue As Any , ByVal cbValueMax As Long, pcbValue As Long)<BR/> As IntegerEssentially, a pointer to memory is passed to SQLBindCol. The ODBC driver then places data in that memory location specified by the pointer on each fetch. The application can then retrieve the data in the recordset by referencing that memory location.
118643 How to Pass a String or String Arrays Between VB and a C DLL.
mystr = String$(255,0).This allocates a string.
SQLBindCol(hstmt&, 1, SQL_C_CHAR, Byval mystr, cbMax&, cbVal&)
GlobalAlloc: allocates a block of memory and returns a handle to that
memory if successful. You can allocate a chunk of
memory for each column you wish to bind.
GlobalLock: locks the memory allocated with GlobalAlloc and returns a
pointer to the memory. You can use GlobalLock to obtain a
pointer which you can use with SQLBindCol.
hmemcpy: copies memory from one address to another a specified
number of bytes. You can use hmemcpy to copy the bound
memory to a variable length string.
NOTE: When finished with the memory you must call GlobalUnlock and
GlobalFree to free the memory and the selector used by GlobalAlloc. If you
are performing a large number of GlobalAlloc calls you should allocate
fewer but larger chunks of memory and break them apart manually.
Field Type Null? --------- ------------ -------- CustNum int not null Company varchar(30) null Address varchar(30) null City varchar(30) null
Command1.Caption = "Log On"
Command2.Caption = "Execute Query"
Text1.MultiLine = True
Text1.ScrollBars = 3
Option Explicit
'
' ODBC constants
'
Const SQL_NULL_HENV = 0
Const SQL_NULL_HDBC = 0
Const SQL_NULL_HSTMT = 0
Const SQL_DRIVER_PROMPT = 2
Const SQL_ERROR = -1
Const SQL_INVALID_HANDLE = -2
Const SQL_NO_DATA_FOUND = 100
Const SQL_SUCCESS = 0
Const SQL_SUCCESS_WITH_INFO = 1
Const SQL_CHAR = 1
Const SQL_NUMERIC = 2
Const SQL_DECIMAL = 3
Const SQL_INTEGER = 4
Const SQL_SMALLINT = 5
Const SQL_FLOAT = 6
Const SQL_REAL = 7
Const SQL_DOUBLE = 8
Const SQL_VARCHAR = 12
Const SQL_C_CHAR = SQL_CHAR
Const SQL_C_LONG = SQL_INTEGER
Const SQL_C_SHORT = SQL_SMALLINT
Const SQL_C_FLOAT = SQL_REAL
Const SQL_C_DOUBLE = SQL_DOUBLE
Const SQL_CLOSE = 0
Const SQL_DROP = 1
Const SQL_UNBIND = 2
Const SQL_RESET_PARAMS = 3
'
' GlobalAlloc Constants
'
Const GMEM_FIXED = &H0
Const GMEM_MOVEABLE = &H2
'
' ODBC function declarations
'
Declare Function SQLAllocConnect Lib "odbc.dll" (ByVal hEnv&, phdbc&)
As Integer
Declare Function SQLAllocEnv Lib "odbc.dll" (phenv&) As Integer
Declare Function SQLAllocStmt Lib "odbc.dll" (ByVal hdbc&, phstmt&)
As Integer
Declare Function SQLBindCol Lib "odbc.dll" (ByVal hStmt&, ByVal icol%,
ByVal fCType%, rgbValue As Any, ByVal cbValueMax&, pcbValue&)
As Integer
Declare Function SQLExecDirect Lib "odbc.dll" (ByVal hStmt&, ByVal
szSqlStr$,
ByVal cbSqlStr&) As Integer
Declare Function SQLDisconnect Lib "odbc.dll" (ByVal hdbc&) As Integer
Declare Function SQLDriverConnect Lib "odbc.dll" (ByVal hdbc&, ByVal hWnd%,
ByVal szCSIn$, ByVal cbCSIn%, ByVal szCSOut$, ByVal cbCSMax%,
cbCSOut%,
ByVal fDrvrComp%) As Integer
Declare Function SQLFetch Lib "odbc.dll" (ByVal hStmt&) As Integer
Declare Function SQLFreeConnect Lib "odbc.dll" (ByVal hdbc&) As Integer
Declare Function SQLFreeEnv Lib "odbc.dll" (ByVal hEnv&) As Integer
Declare Function SQLFreeStmt Lib "odbc.dll" (ByVal hStmt&, ByVal fOption%)
As Integer
'
' Memory function declarations
'
Declare Sub hMemCpy Lib "Kernel" (hpvDest As Any, hpvSource As Any,
ByVal cbCopy As Long)
Declare Function GlobalAlloc Lib "Kernel" (ByVal wFlags As Integer,
ByVal dwBytes As Long) As Integer
Declare Function GlobalLock Lib "Kernel" (ByVal hMem As Integer) As Long
Declare Function GlobalUnlock Lib "Kernel" (ByVal hMem As Integer) As
Integer
Declare Function GlobalFree Lib "Kernel" (ByVal hMem As Integer) As Integer
Dim hEnv As Long ' environment handle
Dim hConnect As Long ' connect handle
Dim hStmt As Long ' statement handle
Dim rc As Integer ' return code
Sub Command1_Click ()
Dim icbConnStrOut As Integer ' connect vars
Dim sConnectStr As String
Dim sConnectBuffer As String
' initialize handles
hConnect = SQL_NULL_HDBC
hEnv = SQL_NULL_HENV
hStmt = SQL_NULL_HSTMT
' initialize strings
sConnectStr = ""
sConnectBuffer = Space$(255)
' allocate the environment and connect to the source
rc = SQLAllocEnv(hEnv)
rc = SQLAllocConnect(hEnv, hConnect)
' the following should be all on 1 line
rc = SQLDriverConnect(hConnect, Me.hWnd, sConnectStr, Len(sConnectStr),
sConnectBuffer, Len(sConnectBuffer), icbConnStrOut,
SQL_DRIVER_PROMPT)
If rc <> SQL_SUCCESS And rc <> SQL_SUCCESS_WITH_INFO Then
MsgBox "Unable To Connect"
End
Else
MsgBox "Connected To Database"
Me.Command1.Enabled = False
End If
End Sub
Sub Command2_Click ()
' strings to hold column data
Dim sCompany As String
Dim sAddress As String
Dim sCity As String
' length of column data returned
Dim lAddressLen As Long
Dim lCityLen As Long
Dim lCompanyLen As Long
' ptrs to memory to bind to columns
Dim lpAddress As Long
Dim lpCompany As Long
Dim lpCity As Long
' mem handles
Dim hMemAddress As Integer
Dim hMemCompany As Integer
Dim hMemCity As Integer
Dim sQuery As String ' sql statement
' Run query
sQuery = " select Company, Address, City From Customer "
rc = SQLAllocStmt(hConnect, hStmt)
rc = SQLExecDirect(hStmt, sQuery, Len(sQuery))
If rc = SQL_SUCCESS Or rc = SQL_SUCCESS_WITH_INFO Then
'
' initialize string space
'
sCompany = String$(30, " ")
sAddress = String$(30, " ")
sCity = String$(30, " ")
Text1.Text = ""
'
' allocate memory and get ptr to it for each column
'
hMemAddress = GlobalAlloc(GMEM_MOVEABLE, Len(sAddress))
lpAddress = GlobalLock(hMemAddress)
hMemCompany = GlobalAlloc(GMEM_MOVEABLE, Len(sCompany))
lpCompany = GlobalLock(hMemCompany)
hMemCity = GlobalAlloc(GMEM_MOVEABLE, Len(sCity))
lpCity = GlobalLock(hMemCity)
'
' bind the columns to the memory we allocated. The must all be
' on one line!
'
rc = SQLBindCol(hStmt, 1, SQL_C_CHAR, ByVal lpCompany, 29,
lCompanyLen)
rc = SQLBindCol(hStmt, 2, SQL_C_CHAR, ByVal lpAddress, 29,
lAddressLen)
rc = SQLBindCol(hStmt, 3, SQL_C_CHAR, ByVal lpCity, 29, lCityLen)
'
' loop and fetch all records into memory
'
rc = SQLFetch(hStmt)
Do While rc = SQL_SUCCESS Or rc = SQL_SUCCESS_WITH_INFO
'
' copy the string from memory to a VB string
'
Call hMemCpy(ByVal sCompany, ByVal lpCompany, lCompanyLen)
Call hMemCpy(ByVal sAddress, ByVal lpAddress, lAddressLen)
Call hMemCpy(ByVal sCity, ByVal lpCity, lCityLen)
Text1.Text = Trim(Text1.Text) & sCompany & sAddress
Text1.Text = Trim(Text1.Text) & sCity & Chr$(13) & Chr$(10)
rc = SQLFetch(hStmt)
Loop
'
' unbind columns and free memory
'
rc = SQLFreeStmt(hStmt, SQL_UNBIND)
rc = GlobalUnlock(hMemCity)
rc = GlobalUnlock(hMemAddress)
rc = GlobalUnlock(hMemCompany)
rc = GlobalFree(hMemCity)
rc = GlobalFree(hMemAddress)
rc = GlobalFree(hMemCompany)
Else
Text1.Text = "No Records Returned"
End If
'
' free statement handle
'
rc = SQLFreeStmt(hStmt, SQL_CLOSE)
MsgBox "Done"
End Sub
Keywords: KB110470