Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
maacmaac
 
Posts: n/a
Default Code to 'lookup' value in Access database


Please help!!!

I am trying to lookup a value in Access but am having problems with the
code. I have seen the code others have used on this forum, but I just
can't seem to get it working. I keep getting "#VALUE!" as an error

I have two files [1] Excel file called "FrontLine" [2] Access file
called "Frontline" and table within Access file is also called
"Frontline".

I am trying to lookup a value in B5 & B6. B5 should return a value of
"Lettuce"; B6 should return a value of "Orange".

The Excel file looks as follows:

A B C D
1 ProductNumber Package Price UPC
2 252 Orange 5.44 346347245
3 502 Apple 8.73 493245778
4 319 Cucumber 3.99 421188445
5 783 #VALUE! 2.15 945651478
6 252 #VALUE!

code in B5 =dbvlookup("FrontLine","ProductNumber",$A5,"Packag e")
code in B6 =dbvlookup("FrontLine","ProductNumber",$A6,"Packag e")

The Access File looks as follows:

ProductNumber Package FrontLinePrice UPC
252 Orange 3 564987987
502 Apple 6 849789333
319 Cucumber 2 463333798
783 Lettuce 8 442777332



The VBA code is as follows:

Dim strSQL As String

Const DatabasePath As String = "C:\Documents and Settings\Mike\My
Documents\FrontLine.mdb"

Public Function DBVLookUp(FrontLine As String, _
ProductNumber As String, _
A6 As String, _
Package As String) As Variant

If adoCN Is Nothing Then SetUpConnection

Set adoRS = New ADODB.Recordset
strSQL = "SELECT " & ProductNumber & ", " & Package & _
" FROM " & FrontLine & _
" WHERE " & ProductNumber & "=" & A6 & ";"
adoRS.Open strSQL, adoCN, adOpenForwardOnly, adLockReadOnly
If adoRS.BOF And adoRS.EOF Then
DBVLookUp = "Value not Found"
Else
DBVLookUp = adoRS.Fields(Package).Value
End If
adoRS.Close
End Function

Sub SetUpConnection()
On Error GoTo ErrHandler
Set adoCN = New Connection
adoCN.Provider = "Microsoft.Jet.OLEDB.4.0"
adoCN.ConnectionString = DatabasePath
adoCN.Open
Exit Sub
ErrHandler:
MsgBox Err.Description, vbExclamation, "An error occurred"
End Sub


I have also attached the Excel & Access files with same info.

I appreciate any help I can get. Thanks.


+-------------------------------------------------------------------+
|Filename: FrontLineExcel_ZIP.zip |
|Download: http://www.excelforum.com/attachment.php?postid=3829 |
+-------------------------------------------------------------------+

--
maacmaac
------------------------------------------------------------------------
maacmaac's Profile: http://www.excelforum.com/member.php...fo&userid=2959
View this thread: http://www.excelforum.com/showthread...hreadid=468093

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Query Excel database from desktop Lost But Trying Hard Excel Discussion (Misc queries) 1 September 11th 05 02:46 PM
Macro for changing text to Proper Case JPriest Excel Worksheet Functions 3 August 8th 05 10:31 PM
VB Code help - please ! Anthony Excel Discussion (Misc queries) 2 July 25th 05 11:38 AM
how do i create an access code sheet to number my office files an. dana New Users to Excel 1 February 12th 05 04:49 AM
Problem Code: Retrieving Stored Access 03 Query Bettergains Excel Discussion (Misc queries) 2 December 7th 04 05:11 PM


All times are GMT +1. The time now is 05:24 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"