Solved

Access Query field to text field on form

Posted on 2013-01-28
9
308 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 200 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
 
LVL 4

Expert Comment

by:MimicTech
ID: 38828483
sorry

GetName = rsResult![ClientName]

no parens
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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 65

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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now