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?
LVL 8
d0ughb0yPresident / CEOAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
mbizupConnect With a Mentor Commented:
>>> GetName = rsResult([ClientName])

Try changing this line to:

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

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

Open in new window


See Jim Dettman's EE article for details about DLookup:
http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_12-Dlookup-and-the-Domain-Functions.html
0
 
MimicTechCommented:
try

GetName = rsResult!([ClientName])

the '!' should be the ticket
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
MimicTechCommented:
sorry

GetName = rsResult![ClientName]

no parens
0
 
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!!
0
 
d0ughb0yPresident / CEOAuthor Commented:
That's another way that worked, MimicTech. Sorry - mbizup got it first.
0
 
MimicTechCommented:
any chance of a point split ;-)

Thanks - now we have two ways noted to do the task
0
 
d0ughb0yPresident / CEOAuthor Commented:
I would have, but once you've already assigned the points, you can't change them.
0
 
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.
0
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.