Applies ToAccess for Microsoft 365 Access 2024 Access 2021 Access 2019 Access 2016

The GUIDFromString function converts a string to a GUID, which is an array of type Byte.

Syntax

GUIDFromString ( stringexpression )

The required stringexpression argument is a string expression which evaluates to a GUID in string form.

Remarks

The Microsoft Access database engine stores GUIDs as arrays of type Byte. However, Access can't return Byte data from a control on a form or report. In order to return the value of a GUID from a control, you must convert it to a string. To convert a GUID to a string, use the StringFromGUID function. To convert a string to a GUID, use the GUIDFromString function.

Query example

Expression

Results

SELECT userID,GUIDfromString(userGUID) as GUIDCode FROM GUID_Table;

Displays the "userID", Converts the StringExpression(userGUID) into GUID (array of bytes) and displays in the column GUIDCode. This example only works for a string expression that can evaluate to a GUID.

VBA example

Note: Examples that follow demonstrate the use of this function in a Visual Basic for Applications (VBA) module. For more information about working with VBA, select Developer Reference in the drop-down list next to Search and enter one or more terms in the search box.

The following example uses the GUIDFromString function to convert a string to a GUID. The string is a GUID stored in string form in a replicated Employees table. The field, s_GUID, is a hidden field added to every replicated table in a replicated database.

Sub CheckGUIDType()
    Dim dbsConn As ADODB.Connection
    Dim rstEmployees As ADODB.Recordset
    ' Make a connection to the current database.
    Set dbsConn = Application.CurrentProject.Connection
    Set rstEmployees = New ADODB.Recordset
    rstEmployees.Open "Employees", _
        dbsConn, , , adCmdTable
    ' Print the GUID to the immediate window.
    Debug.Print rst!s_GUID
    Debug.Print TypeName(rst!s_GUID)
    Debug.Print TypeName(GuidFromString(rst!s_GUID))
    Set rstEmployees = Nothing
    Set dbsConn = Nothing
End Sub

Need more help?

Want more options?

Explore subscription benefits, browse training courses, learn how to secure your device, and more.

Communities help you ask and answer questions, give feedback, and hear from experts with rich knowledge.