Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Access Query field to text field on form

Posted on 2013-01-28
9
Medium Priority
?
321 Views
Last Modified: 2013-01-28
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?
0
Comment
Question by:d0ughb0y
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 61

Accepted Solution

by:
mbizup earned 800 total points
ID: 38828457
>>> GetName = rsResult([ClientName])

Try changing this line to:

GetName = rsResult("ClientName")
0
 
LVL 61

Expert Comment

by:mbizup
ID: 38828466
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
 
LVL 4

Expert Comment

by:MimicTech
ID: 38828480
try

GetName = rsResult!([ClientName])

the '!' should be the ticket
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 4

Expert Comment

by:MimicTech
ID: 38828483
sorry

GetName = rsResult![ClientName]

no parens
0
 
LVL 8

Author Closing Comment

by:d0ughb0y
ID: 38828485
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
 
LVL 8

Author Comment

by:d0ughb0y
ID: 38828491
That's another way that worked, MimicTech. Sorry - mbizup got it first.
0
 
LVL 4

Expert Comment

by:MimicTech
ID: 38828501
any chance of a point split ;-)

Thanks - now we have two ways noted to do the task
0
 
LVL 8

Author Comment

by:d0ughb0y
ID: 38828864
I would have, but once you've already assigned the points, you can't change them.
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 38829450
>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

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question