Access Query field to text field on form

I want to have a function that will return a single field-value from a recordset. I can get the SQL query to produce the recordset, but I don't know how to specify just that field.

In other words, given the following function:
Private Function GetName (Client)
Dim strSQL as String
Dim rsResult as Recordset
Dim DB as Database

strSQL = "SELECT TOP 1 ClientID, ClientName, FROM tblClients " & _
              "WHERE (ClientID = " & Client & ");"

Set DB = CurrentDb
Set rsResult = DB.OpenRecordset(strSQL)
GetName = rsResult([ClientName])
End Function

Open in new window

Note that the GetName at the end is trying to put return just the ClientName field for the recordset rsResult. But I'm not sure how to get it to do that. This doesn't work - it tells me that the item isn't in the collection. I've also tried GetName = rsResult.ClientName, but that doesn't work either, because it's not an object.

How can I get that value to be returned by the function?
d0ughb0yPresident / CEOAsked:
Who is Participating?
>>> GetName = rsResult([ClientName])

Try changing this line to:

GetName = rsResult("ClientName")
Alternatively, use DLookup:

GetName = DLookup("ClientName", "tblClients", "ClientID = " & Client )

Open in new window

See Jim Dettman's EE article for details about DLookup:

GetName = rsResult!([ClientName])

the '!' should be the ticket
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions


GetName = rsResult![ClientName]

no parens
d0ughb0yPresident / CEOAuthor Commented:
There are some additional complications that would cause DLOOKUP to be a bit more difficult to use, in this case. I didn't include them, because I figured that I knew what I needed, and if I could just get that, I'd be fine. And I was right! Thanks!!
d0ughb0yPresident / CEOAuthor Commented:
That's another way that worked, MimicTech. Sorry - mbizup got it first.
any chance of a point split ;-)

Thanks - now we have two ways noted to do the task
d0ughb0yPresident / CEOAuthor Commented:
I would have, but once you've already assigned the points, you can't change them.
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>any chance of a point split ;-)
MimicTech - Posting the same answer as another expert five minutes after they did is considered acceptable, but whoring for points in this manner is not.    Especially since the experts whom you are attempting to whore points from will likely be the ones answering your future questions.  Or then again, maybe not.  

It's not worth it dude.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.