Access/VBA Library

QueryRow

Excutes an SELECT-query and returns a Dictionary containing the data from the first record.

Syntax

QueryRow( query )
Parameters
Name Type Description
query String Required. A valid SELECT-statement.
Return value

Dictionary : The data from the first record or Nothing when an error occured or the query has no output.

Code
Function QueryRow(query As String) As Dictionary
    Const cFunctionName = "QueryRow"

On Error GoTo QueryRow_Error

    Dim rs As Recordset
    ' Excute query
    Set rs = CurrentDb.OpenRecordset(query, dbOpenSnapshot, dbReadOnly)
    ' Data found?
    If Not rs.EOF Then
        rs.MoveFirst
        Set QueryRow = FetchRow(rs)
    Else
        Set QueryRow = Nothing
    End If
QueryRow_Exit:
    Exit Function

QueryRow_Error:
    Set QueryRow = Nothing
    ShowError functionName:=cFunctionName
    Resume QueryRow_Exit

End Function

Databasetag:DatabaseSQLtag:SQLDictionarytag:Dictionary

Remarks

  • Use this function when you are sure the query will result in one record.

Changelog

Date Changes
2018-04-02 Performance change: open recordset as read-only snapshot.

See also

  • FetchRow
  • QueryRows
  • ShowError