SAMPLE: AdoGUIDz.exe How to Use GUIDs w/ Access, SQL 6.5 & SQL 7ID: Q197916
|
AdoGUIDz.exe is a self-extracting executable that contains a sample project
that demonstrates using the globally unique identifier (GUID) datatype
with Microsoft Access, SQL 6.5 and SQL 7.0. The sample code may be
particularly helpful if you are attempting to manipulate GUIDs with ODBC
versions 3.51 and below since those versions of ODBC do not support a
native GUID datatype. ODBC versions 3.6 and above include the GUID
datatype. Consequently, the methods for manipulating GUIDs with ODBC 3.6
are simpler.
NOTE: SQL 6.5 does not support a native GUID datatype so in order to
store/retrieve GUIDs in SQL 6.5 you must use the VarBinary datatype and
Byte Arrays.
The following file is available for download from the Microsoft Software
Library:
~ adoGUIDz.exe
Release Date: DEC-29-1998
For more information about downloading files from the Microsoft Software
Library, please see the following article in the Microsoft Knowledge Base:
Q119591 : How to Obtain Microsoft Support Files from Online Services
FileName Size
---------------------------------------------------------
AdoGUID.bas 3KB
AdoGUID.exe 60KB
AdoGUID.frm 25KB
AdoGUID.frx 1KB
AdoGUID.mdb 80KB
AdoGUID.vbp 2KB
Readme.txt 4KB
Sub AccessReQueryADO()
On Error GoTo ErrorMessage
Dim adoCn As adoDb.Connection
Dim adoRs As adoDb.Recordset
Dim strCn As String
Dim strSQL As String
strCn = App.Path & "\adoGUID.mdb"
Set adoCn = New adoDb.Connection
With adoCn
.Provider = "Microsoft.JET.OLEDB.3.51"
.CommandTimeout = 500
.ConnectionTimeout = 500
.Open strCn, "admin", ""
End With
If Option7.Value = True Then
strSQL = "SELECT * FROM GUIDtable WHERE " & _
"Instr(1,[colGUID],'" & strGUID & "')"
Else
strSQL = "SELECT * FROM GUIDtable"
End If
Set adoRs = New adoDb.Recordset
With adoRs
Set .ActiveConnection = adoCn
.LockType = adLockOptimistic
.CursorLocation = adUseServer
.CursorType = adOpenForwardOnly
End With
adoRs.Open strSQL
txtMessage.Text = ""
While Not adoRs.EOF
txtMessage.Text = txtMessage.Text & _
adoRs.Fields("colGUID").Value & " | "
txtMessage.Text = txtMessage.Text & _
adoRs.Fields("colDescription").Value & vbCrLf
adoRs.MoveNext
Wend
GoTo ExitSub
ErrorMessage:
MsgBox Err.Number & " : " & vbCrLf & Err.Description
ExitSub:
Label6.Caption = "- ReQueried AccessADO GUID Table..."
Set adoCn = Nothing
Set adoRs = Nothing
End Sub
typedef struct _GUID
{
unsigned long Data1;
unsigned short Data2;
unsigned short Data3;
unsigned char Data4[8];
} GUID;
* Data1
An unsigned long integer data value.
* Data2
An unsigned short integer data value.
* Data3
An unsigned short integer data value.
* Data4
An array of unsigned characters.
Reversed... Not Reversed...
>----------------<|>---------------<
20C68F83-9593-0011-BFBB-00C04F8F8347 'SQLServer view after table Export.
838FC620-9395-1100-BFBB-00C04F8F8347 'Microsoft Access view.
Sub SQL65InsertGUID()
'Insert GUID record.
On Error GoTo ErrorMessage
Dim adoCn As adoDb.Connection
Dim adoRs As adoDb.Recordset
Dim strGUIDtmp As String
Dim bytGUID() As Byte
Dim strCn As String
Dim strSQL As String
strCn = "Provider=" & strProvider & _
";Driver={SQL Server}" & _
";Server=" & txtServer & _
";Database=" & txtDatabase & _
";Uid=" & txtUserID & _
";Pwd=" & txtPassword
Set adoCn = New adoDb.Connection
With adoCn
.ConnectionString = strCn
.CommandTimeout = 500
.ConnectionTimeout = 500
.Open
End With
strGUIDtmp = strGUID
bytGUID = GUID2ByteArray(FilterGUID(strGUIDtmp))
strSQL = "SELECT * FROM GUIDtable WHERE 1=0"
Set adoRs = New adoDb.Recordset
With adoRs
Set .ActiveConnection = adoCn
.LockType = adLockOptimistic
.CursorLocation = adUseServer
.CursorType = adOpenForwardOnly
End With
adoRs.Open strSQL
adoRs.AddNew
adoRs.Fields("colGUID").Value = bytGUID
adoRs.Fields("colDescription").Value = "This is a test GUID"
adoRs.Update
GoTo ExitSub
ErrorMessage:
MsgBox Err.Number & " : " & vbCrLf & Err.Description
ExitSub:
Label6.Caption = "[ASCII 176] Inserted SQL65 GUID Record..."
Set adoCn = Nothing
Set adoRs = Nothing
End Sub
'======================
Function GUID2ByteArray(ByVal strGUID As String) As Byte()
Dim i As Integer
Dim j As Integer
Dim sPos As Integer
Dim OffSet As Integer
Dim sGUID(0 To 2) As Byte
Dim bytArray() As Byte
ReDim bytArray(0 To 15) As Byte
sGUID(0) = 7
sGUID(1) = 11
sGUID(2) = 15
OffSet = 0
sPos = 0
'AABBCCDD-AABB-CCDD-XXXX-XXXXXXXXXXXX 'Microsoft Access view.
'DDCCBBAA-BBAA-DDCC-XXXX-XXXXXXXXXXXX 'SQLServer view.
'Need to loop through to build the GUID byte array in the Microsoft
'Access storage format since the first eight bytes are reversed.
For i = 0 To UBound(sGUID)
For j = sGUID(i) To (OffSet + 1) Step -2
bytArray(sPos) = "&H" & Mid$(strGUID, j, 2)
sPos = sPos + 1
Next j
OffSet = sGUID(i)
Next i
For i = 17 To 31 Step 2
bytArray(sPos) = "&H" & Mid$(strGUID, i, 2)
sPos = sPos + 1
Next i
GUID2ByteArray = bytArray()
End Function
For more information, please see the following article in the Microsoft
Knowledge Base:
Q176790 : HOWTO: Use CoCreateGUID API to Generate a GUID with VB
Additional query words: kbdse kbfile kbcode
Keywords : kbADO kbJET kbSQLServ kbVBp500 kbVBp600
Version : WINDOWS:1.5,2.0,5.0,6.0
Platform : WINDOWS
Issue type :
Last Reviewed: July 2, 1999