Link to home
Start Free TrialLog in
Avatar of David Spigelman
David SpigelmanFlag for United States of America

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Alternatively, use DLookup:

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

Open in new window


See Jim Dettman's EE article for details about DLookup:
https://www.experts-exchange.com/Microsoft/Development/MS_Access/A_12-Dlookup-and-the-Domain-Functions.html
Avatar of MimicTech
MimicTech

try

GetName = rsResult!([ClientName])

the '!' should be the ticket
sorry

GetName = rsResult![ClientName]

no parens
Avatar of David Spigelman

ASKER

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!!
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
I would have, but once you've already assigned the points, you can't change them.
>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.