Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Access Query field to text field on form

Posted on 2013-01-28
9
Medium Priority
?
319 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
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 …
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…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

730 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