Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 5th, 2009, 05:01 AM
ppenn
Guest
 
Posts: n/a
Smile VBA - Lookup

Hi
My previous post about loop through datasets probably was not that clear

What I am trying to do is perform the same action as Excel Lookup function in VBA.

Where a field is searched and an associated field returned.

For example if a text box in a form shows a certain value that value is searched and if found an associated value returned to another text box.

Hope that might be clearer

Regards
 
Old November 9th, 2009, 07:18 PM
Friend of Wrox
 
Join Date: Feb 2007
Posts: 163
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Let's assume you want to find [Your_Value] in MatchingField_Name and wish to return ReturnField_Name. You would set the text box Control Source to:
=GetFieldValue([Your_Value],"MatchingField_Name","ReturnField_Name")

Notice that [Your_Value] comes from the current bound table for the form which has a field named "Your_Value". Also notice I'm passing two field names in quotes without the [] because I'm passing the field name itself and not it's value.

Next, place this code in the form's code mod:
Code:
Private Function GetFieldValue(sPassed As String, sSource As String, sTarget As String)

'Retreives first instance in DB of sTarget field where sPassed is value matching sSource in table
  Dim rsTable As Recordset
  Set rsTable = CurrentDb.OpenRecordset("Select * From Table1 Where " & sSource & " = """ _
    & sPassed & """")
  If rsTable.RecordCount > 0 _
    Then GetFieldValue = rsTable(sTarget).Value _
    Else GetFieldValue = ""
  rsTable.Close

End Function
You didn't give any field names or table names so I am using 'Table1' as the lookup table name.

Hope this helps point in the right direction.
 
Old November 25th, 2009, 10:33 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

Have you looked into the DLookUp() function?
__________________
Greg Serrano
Michigan Dept. of Environmental Quality
Air Quality Division
 
Old November 25th, 2009, 11:56 AM
ppenn
Guest
 
Posts: n/a
Default

Hello Greg
My apologies for not getting back sooner, I have been heavily involved in another problem.
I did get the Dllookup to work and it does do what I needed

Thanks very much for you assistance in this. I will have alook at your comments on the other post this evening

Best regards





Similar Threads
Thread Thread Starter Forum Replies Last Post
LookUp and Hyperlink frankjuel Excel VBA 1 January 12th, 2007 03:03 AM
Excel/VBA Multi-Column Lookup - Round 2 RollingWoodFarm Excel VBA 4 August 3rd, 2006 07:28 PM
lookup using VB karebear VB How-To 1 August 2nd, 2006 04:32 PM
lookup function Vince_421 Access VBA 14 May 19th, 2006 07:27 AM
File name lookup acdsky Classic ASP Basics 3 November 22nd, 2003 11:49 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.