Article ID: 150786
Article Last Modified on 7/13/2004
Option Explicit
Dim hen As Long ' environment handle.
Dim hdbc As Long ' connect handle.
Dim hstmt As Long ' statement handle.
Dim intRc As Integer ' return value.
Const ROWSETSIZE As Long = 1
Private Sub Form_Load()
' load driver manager.
intRc = SQLAllocEnv(hen) ' allocate environment handle.
intRc = SQLAllocConnect(ByVal hen, hdbc) ' allocate connection
' handle.
' load driver.
Dim strDSN As String, strUID As String, strPWD As String
strDSN = "pubs"
strUID = "sa"
strPWD = ""
intRc = SQLConnect(hdbc, strDSN, Len(strDSN), strUID, _
Len(strUID), strPWD, Len(strPWD))
End Sub
Private Sub Command1_Click()
intRc = SQLAllocStmt(hdbc, hstmt) ' allocate a statement handle.
intRc = SQLSetStmtOption(hstmt, SQL_CONCURRENCY, SQL_CONCUR_ROWVER)
intRc = SQLSetStmtOption(hstmt, SQL_CURSOR_TYPE, _
SQL_CURSOR_KEYSET_DRIVEN)
intRc = SQLSetStmtOption(hstmt, SQL_ROWSET_SIZE, ROWSETSIZE)
intRc = SQLSetCursorName(hstmt, "C1", SQL_NTS)
Dim strSQL As String
strSQL = "Select au_lname from authors"
intRc = SQLExecDirect(hstmt, strSQL, Len(strSQL))
Debug.Print intRc
Dim szField1_Value(256) As Byte
Dim cbField1_Value As Long
intRc = SQLBindCol(hstmt, 1, SQL_C_CHAR, szField1_Value(0), 256, _
cbField1_Value)
Dim irow As Long
Dim pcrow As Long
Dim rgfRowStatus(ROWSETSIZE) As Integer
intRc = SQLExtendedFetch(ByVal hstmt, SQL_FETCH_NEXT, 0, pcrow, _
rgfRowStatus(0))
Debug.Print BytesToString(szField1_Value())
irow = 1
intRc = SQLSetPos(hstmt, irow, SQL_POSITION, SQL_LOCK_NO_CHANGE)
Dim hstmtU As Long
intRc = SQLAllocStmt(hdbc, hstmtU) ' alloc statement handle for
' update.
strSQL = "UPDATE AUTHORS SET AU_LNAME=? WHERE CURRENT OF C1"
intRc = SQLPrepare(hstmtU, strSQL, Len(strSQL))
Call StringToBytes("John Doe", 256, szField1_Value())
Debug.Print BytesToString(szField1_Value())
intRc = SQLBindParameter(hstmtU, 1, SQL_PARAM_INPUT, SQL_C_CHAR, _
SQL_CHAR, 256, 0, szField1_Value(0), 0, SQL_NTS)
intRc = SQLExecute(hstmtU)
intRc = SQLFreeStmt(hstmtU, SQL_DROP)
End Sub
Private Function BytesToString(byte_array() As Byte) As String
' convert byte array to string.
Dim Data As String, StrLen As String
Data = StrConv(byte_array(), vbUnicode)
StrLen = InStr(Data, Chr(0)) - 1
BytesToString = Left(Data, StrLen)
End Function
Private Sub StringToBytes(Data As String, ByteLen As Integer, _
return_buffer() As Byte)
' convert string to byte array.
Dim StrLen As Integer, Count As Integer
For Count = 0 To Len(Data) - 1
return_buffer(Count) = Asc(Mid(Data, Count + 1, 1))
Next Count
For Count = Len(Data) To ByteLen
return_buffer(Count) = 0
Next Count
End Sub
Private Sub Form_Unload(Cancel As Integer)
intRc = SQLDisconnect(ByVal hdbc)
intRc = SQLFreeEnv(ByVal hen)
intRc = SQLFreeStmt(hstmt, SQL_DROP)
End Sub
Additional query words: kbVBp kbdse kbDSupport kbVBp400 kbODBC
Keywords: kbhowto KB150786