UtterAccess HomeUtterAccess Wiki

Welcome Guest ( Log In | Register )

Custom Search
Edit Discussion
> NewGUID    


NewGUID() Function

The NewGUID function will return a string that represents a GUID (Globally Unique Identifier). The resultant string can be passed into that Access function GUIDFromString() function to convert the string into a byte array that contains the value of a GUID. In addition, if you have a Field that is data typed as Number/Replication ID, you can write the resultant string into that field and the Jet/ACE database engine will understand it as a GUID value.



There are no arguments or parameters for this function.

Public Function NewGUID() As String
'Returns a GUID string built by using the Jet/ACE database engine GUID generator.
' http://www.utteraccess.com/wiki/index.php/CreateGUID
' Code courtesy of UtterAccess Wiki
' Licensed under Creative Commons License
' http://creativecommons.org/licenses/by-sa/3.0/
' You are free to use this code in any application,
' provided this notice is left unchanged.
' rev  date                          brief descripton
' 1.0  2012-06-13                    Initial Release
   Const cTable As String = "USys_GUID"
   Dim strSQL As String
   Dim db As Object 'DAO.Database
   Set db = CurrentDb
   'Is the GUID table present, if not create it.
   If DCount("*", "MSysObjects", "Type IN (1,4,6) And [Name]='" & cTable & "'") = 0 Then
       With db
           Dim tdf As Object 'DAO.TableDef
           Set tdf = .CreateTableDef(cTable)
           With tdf
               Dim fld As Object 'DAO.Field
               Set fld = .CreateField("MyGUID", dbGUID)
               fld.Attributes = (fld.Attributes Or 8132) '8132 = dbSystemField)
               tdf.Fields.Append fld
           End With
           .TableDefs.Append tdf
       End With
   End If
   'Now let the db engine create the GUID
   db.Execute "DELETE FROM " & cTable 'There shouldn't be any records anyway.
   With db.OpenRecordset("SELECT * FROM " & cTable & " WHERE 1=0")
       NewGUID = Replace(Replace(.Fields(0), "{guid ", ""), "}}", "}")
   End With
End Function

Return Value



Insert a new record into a table named tblMyTable that has the following fields:

RecordId: Autonumber/Long Integer (Primary Key)
SomeText: Text(50) (Required = True)
RecordGUID: Number/Replication ID (Required = True)

Sub InsertNewRecord()
   Dim strGUID As String
   strGUID = NewGUID
   CurrentDb.Execute "INSERT INTO tblMyTable (SomeText, RecordGUID)" & _
                     " VALUES ('my text value'," & strGUID & ")", dbFailOnError
End Sub

Creative Commons License
NewGUID by UtterAccess Wiki is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Editing and revision of the content is freely encouraged; for details, see Expected Usage.

Edit Discussion
Custom Search
Thank you for your support!
This page has been accessed 7,027 times.  This page was last modified 17:48, 13 June 2012 by Brent Spaulding.   Disclaimers