WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

ADO DAO
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Auto Generate GUIDs

 Keywords: Replication ID GUID 

Question:

I need to automatically generate GUID's in Access, can you help?

Answer:

The following is some sample code submitted by Stan Littlefield.
; generate as many GUID's as you want
; uses the new ObjectType() OLE function

adGUID    = 72
file      = StrCat( dirget(),"GUID.MDB" )

; create a temp MDB to generate GUID's
IF FileExist(file) Then FileDelete(file)
cConn     = "Provider=MicroSoft.Jet.OLEDB.4.0; Data Source=%file%;"

;if Office 97 use
;cConn="Provider=MicroSoft.Jet.OLEDB.4.0; Data Source=%file%;Jet OLEDB:Engine Type=4"

BoxOpen("Creating  ...",cConn)
;creating the object(s)
cnn       = ObjectOpen("ADODB.Connection")
cnn.ConnectionString = cConn
cat       = ObjectOpen("ADOX.Catalog")
cat.Create(cConn)

; create table and columns
BoxText( "Creating Auto-Generating GUID Column" )
tbl       = ObjectOpen("ADOX.Table")
tbls      = cat.Tables
tbl.Name  = "TESTTBL"
tbl.ParentCatalog = cat
tbls.append(tbl)
cols      = tbl.Columns
col       = ObjectOpen("ADOX.Column")
col.Name  = "GUID_ID"
col.Type  = adGUID
col.ParentCatalog = cat
n         = col.Properties("AutoIncrement")
n.Value   = ObjectType("VT_BOOL", 0)         ; would fail if = @FALSE uses
n         = col.Properties("Fixed Length")
n.Value   = ObjectType("VT_BOOL", -1)
n         = col.Properties("Jet OLEDB:AutoGenerate")
n.Value   = ObjectType("VT_BOOL", -1)
n         = col.Properties("Jet OLEDB:Allow Zero Length")
n.Value   = ObjectType("VT_BOOL", -1)
cols.append( col )


ObjectClose(col)
ObjectClose(tbl)
ObjectClose(tbls)
ObjectClose(cat)

RS       = ObjectOpen("ADODB.Recordset")
RS.Open("TESTTBL",cConn,2,3)
BoxText( "Adding 5 Sample Rows" )

For i = 1 To 5
   RS.Addnew()
   RS.Update()
Next
BoxShut()
RS.MoveFirst()
x =1
While ! RS.eof
   fld = RS.Fields(0)
   message( "GUID for Row %x%",fld.Value )
   x = x+1
   RS.MoveNext()
EndWhile

RS.Close()
ObjectClose( RS )
Exit

Article ID:   W15239
File Created: 2002:09:05:13:50:46
Last Updated: 2002:09:05:13:50:46