PC Review


Reply
Thread Tools Rate Thread

Create GUID in Access 2003 Application

 
 
=?Utf-8?B?ZGJ1c2NoaQ==?=
Guest
Posts: n/a
 
      27th Sep 2004
Hi All,

I need to insert a new record with a GUID Primary Key into a table.

Does anyone know how to create the new GUID in Access to pass it into the
INSERT script or is there a way to retrieve the GUID that is created by the
db (newid() function)?

Thanks heaps for any help, tipp,...
 
Reply With Quote
 
 
 
 
StCyrM
Guest
Posts: n/a
 
      27th Sep 2004
Hello

Simply copy and paste the following in a new module.

Best Regards

Maurice St-Cyr
Micro Systems Consultants, Inc.





Option Compare Database

' Samples:
' {3201047B-FA1C-11D0-B3F9-004445535400}
' {0547C3D5-FA24-11D0-B3F9-004445535400}

Option Explicit
DefLng A-Z

' The following is from Topic: Windows Conferencing API, GUID, MSDN April
1997
' typedef struct _GUID {
' unsigned long Data1;
' unsigned short Data2;
' unsigned short Data3;
' unsigned char Data4[8];
'} GUID;
'
'Holds a globally unique identifier (GUID), which identifies a particular _
object class and interface. This identifier is a 128-bit value.
'
'For more information about GUIDs, see the Remote Procedure Call (RPC) _
documentation or the OLE Programmer's Reference.
'

Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(0 To 7) As String * 1
End Type

Declare Function CoCreateGuid Lib "ole32.dll" (tGUIDStructure As GUID) As
Long

Const mciLen As Integer = 4 'each part's length

Public Function CreateGUID() As String
Dim sGUID As String 'store result here
Dim tGUID As GUID 'get into this structure
If CoCreateGuid(tGUID) = 0 Then 'use API to get the GUID
With tGUID 'build return string
sGUID = "{" & PadLeft(Hex(.Data1), mciLen * 2) & "-"
sGUID = sGUID & PadLeft(Hex(.Data2), mciLen) & "-"
sGUID = sGUID & PadLeft(Hex(.Data3), mciLen) & "-"
sGUID = sGUID & FormatGUIDData4(.Data4())
End With
sGUID = sGUID & "}" 'ending brace
CreateGUID = sGUID
End If
End Function

Private Function FormatGUIDData4(aryData4() As String * 1) As String
Dim i As Integer 'loop thru the array
Dim sGUID As String 'store result here
Dim sTemp1 As String 'first part here
Dim sTemp2 As String 'second part here
For i = LBound(aryData4()) To UBound(aryData4()) 'process string array
If i < 2 Then 'first part
sTemp1 = sTemp1 & Hex(Asc(aryData4(i)))
Else 'second part
sTemp2 = sTemp2 & Hex(Asc(aryData4(i)))
End If
Next
sGUID = PadLeft(sTemp1, mciLen) & "-" & PadLeft(sTemp2, mciLen * 3) 'pad
left with zeros
FormatGUIDData4 = sGUID 'return what we created
End Function

Private Function PadLeft(sString As String, iLen As Integer) As String
' Pad with left zreos if needed
Dim sTemp As String
sTemp = Right$(String$(iLen, "0") & sString, iLen)
PadLeft = sTemp
End Function




>I need to insert a new record with a GUID Primary Key into a table.
>
>Does anyone know how to create the new GUID in Access to pass it into the
>INSERT script or is there a way to retrieve the GUID that is created by the
>db (newid() function)?
>
>Thanks heaps for any help, tipp,...
>
>
>
>
>
>



 
Reply With Quote
 
=?Utf-8?B?ZGJ1c2NoaQ==?=
Guest
Posts: n/a
 
      28th Sep 2004
Thanks Maurice, this did the trick

"StCyrM" wrote:

> Hello
>
> Simply copy and paste the following in a new module.
>
> Best Regards
>
> Maurice St-Cyr
> Micro Systems Consultants, Inc.
>
>
>
>
>
> Option Compare Database
>
> ' Samples:
> ' {3201047B-FA1C-11D0-B3F9-004445535400}
> ' {0547C3D5-FA24-11D0-B3F9-004445535400}
>
> Option Explicit
> DefLng A-Z
>
> ' The following is from Topic: Windows Conferencing API, GUID, MSDN April
> 1997
> ' typedef struct _GUID {
> ' unsigned long Data1;
> ' unsigned short Data2;
> ' unsigned short Data3;
> ' unsigned char Data4[8];
> '} GUID;
> '
> 'Holds a globally unique identifier (GUID), which identifies a particular _
> object class and interface. This identifier is a 128-bit value.
> '
> 'For more information about GUIDs, see the Remote Procedure Call (RPC) _
> documentation or the OLE Programmer's Reference.
> '
>
> Private Type GUID
> Data1 As Long
> Data2 As Integer
> Data3 As Integer
> Data4(0 To 7) As String * 1
> End Type
>
> Declare Function CoCreateGuid Lib "ole32.dll" (tGUIDStructure As GUID) As
> Long
>
> Const mciLen As Integer = 4 'each part's length
>
> Public Function CreateGUID() As String
> Dim sGUID As String 'store result here
> Dim tGUID As GUID 'get into this structure
> If CoCreateGuid(tGUID) = 0 Then 'use API to get the GUID
> With tGUID 'build return string
> sGUID = "{" & PadLeft(Hex(.Data1), mciLen * 2) & "-"
> sGUID = sGUID & PadLeft(Hex(.Data2), mciLen) & "-"
> sGUID = sGUID & PadLeft(Hex(.Data3), mciLen) & "-"
> sGUID = sGUID & FormatGUIDData4(.Data4())
> End With
> sGUID = sGUID & "}" 'ending brace
> CreateGUID = sGUID
> End If
> End Function
>
> Private Function FormatGUIDData4(aryData4() As String * 1) As String
> Dim i As Integer 'loop thru the array
> Dim sGUID As String 'store result here
> Dim sTemp1 As String 'first part here
> Dim sTemp2 As String 'second part here
> For i = LBound(aryData4()) To UBound(aryData4()) 'process string array
> If i < 2 Then 'first part
> sTemp1 = sTemp1 & Hex(Asc(aryData4(i)))
> Else 'second part
> sTemp2 = sTemp2 & Hex(Asc(aryData4(i)))
> End If
> Next
> sGUID = PadLeft(sTemp1, mciLen) & "-" & PadLeft(sTemp2, mciLen * 3) 'pad
> left with zeros
> FormatGUIDData4 = sGUID 'return what we created
> End Function
>
> Private Function PadLeft(sString As String, iLen As Integer) As String
> ' Pad with left zreos if needed
> Dim sTemp As String
> sTemp = Right$(String$(iLen, "0") & sString, iLen)
> PadLeft = sTemp
> End Function
>
>
>
>
> >I need to insert a new record with a GUID Primary Key into a table.
> >
> >Does anyone know how to create the new GUID in Access to pass it into the
> >INSERT script or is there a way to retrieve the GUID that is created by the
> >db (newid() function)?
> >
> >Thanks heaps for any help, tipp,...
> >
> >
> >
> >
> >
> >

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
create a guid Mr. X. Microsoft VB .NET 8 25th Jan 2010 02:13 PM
GUID field does not retrieve data properly - Access 2003 BUG? =?Utf-8?B?SmVmZg==?= Microsoft Access Forms 2 3rd May 2005 05:53 PM
How to get GUID of an application ? Giuseppe Porcelli Microsoft Dot NET Compact Framework 4 29th Dec 2004 03:41 PM
MS Access - Create a GUID for Import into SQL Ken Imhof Microsoft Access VBA Modules 1 23rd Jun 2004 07:28 PM
New GUID(guidstring) fails to create GUID - Help Please!!! Chris Ericoli Microsoft Dot NET 1 29th Oct 2003 06:04 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:22 PM.