I like DLookup but it seems a bit limited in the sense that I don't think it accepts a complex SELECT query.
I'd like to know if Access VBA allows you return a single value from a Command object. (VB.Net has a Command.ExecuteScalar function which is much like DLookup). As I wasn't sure, for the moment I'm using a recordset instead of a command object. In other words here's my VBA version of DLookup.
Private Function ExecuteScalar(ByVal query As String, ByVal ColName As String) As Variant
Dim rs As New ADODB.Recordset
rs.Open query, CurrentProject.Connection
Dim recCount As Integer
Do While Not rs.EOF
recCount = recCount + 1
rs.MoveNext
Loop
If recCount > 1 Then Err.Raise "Your custom Execute Scalar method has returned more than one value."
If recCount = 0 Then
ExecuteScalar = Null
Else: rs.MoveFirst
ExecuteScalar = rs(ColName)
End If
rs.Close
Set rs = Nothing
End Function
Is there a way to do this in VBA without the overhead of a recordset?
I'd like to know if Access VBA allows you return a single value from a Command object. (VB.Net has a Command.ExecuteScalar function which is much like DLookup). As I wasn't sure, for the moment I'm using a recordset instead of a command object. In other words here's my VBA version of DLookup.
Private Function ExecuteScalar(ByVal query As String, ByVal ColName As String) As Variant
Dim rs As New ADODB.Recordset
rs.Open query, CurrentProject.Connection
Dim recCount As Integer
Do While Not rs.EOF
recCount = recCount + 1
rs.MoveNext
Loop
If recCount > 1 Then Err.Raise "Your custom Execute Scalar method has returned more than one value."
If recCount = 0 Then
ExecuteScalar = Null
Else: rs.MoveFirst
ExecuteScalar = rs(ColName)
End If
rs.Close
Set rs = Nothing
End Function
Is there a way to do this in VBA without the overhead of a recordset?
Last edited: