LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 50
Default Lookup in Access return result to Excel

Brent Try this sub below.

Note if the lookup value is not text then change line:

.CommandText = "Select AE from table2 where ECT = '" &
Cells(iLoopRow, 1) & "'"

with:
.CommandText = "Select AE from table2 where ECT = " &
Cells(iLoopRow, 1)

This code assumes you have an Access database called: C:\temp\yourfile.mdb
And that your lookup data is in column 1(A) and you want to return your data
to Column 2 (B)


Sub Lookup

Dim iLoopRow As Integer

Set cnnConn = New ADODB.Connection
With cnnConn
.ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0"
.Open "C:\temp\yourfile.mdb"
End With

Set cmdcommand = New ADODB.Command
Set cmdcommand.ActiveConnection = cnnConn

Set rstRecordset = New ADODB.Recordset
Set rstRecordset.ActiveConnection = cnnConn

With cmdcommand
.CommandType = adCmdText

'*** Get data for 100 rows
For iLoopRow = 1 To 100
.CommandText = "Select AE from table2 where ECT = '" &
Cells(iLoopRow, 1) & "'"
rstRecordset.Open cmdcommand

'*** Test to see if a lookup value was returned
If rstRecordset.EOF Then
Cells(iLoopRow, 2) = "Unknown"
Else
Cells(iLoopRow, 2) = rstRecordset("AE")
End If '*** No record returned

rstRecordset.Close

Next iLoopRow
End With '*** cmdcommand


'*** Cleanup
Set cmdcommand = Nothing
Set rstRecordset = Nothing
cnnConn.Close
Set cnnConn = Nothing


End Sub


Please post if this was helpful. Good luck.
--
Stewart Rogers
DataSort Software, L.C.


"Brent" wrote:

I am trying to do something like a vlookup in an Excel maco using ADO/VBA,
which looks up a value in an Access table and returns the corresponding value
to a variable in Excel. I have tried the this:

loc = "Select AE from table2 where ECT = Range("A" & rws).value"

where AE and ECT are columns in table2 and rws is a counter-like variable.
Any suggestions (websites, references) would be greatly appreciated!!

 
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
Multiple Lookup return 1 result JHL Excel Discussion (Misc queries) 3 October 1st 09 04:18 PM
Lookup value then return result from other column, varying row num Justin Excel Worksheet Functions 5 April 17th 08 02:39 AM
lookup for a value in multiple columns and return a result Ram Excel Discussion (Misc queries) 16 June 29th 07 11:16 PM
lookup value and return result in column to left Mark M Excel Worksheet Functions 3 April 23rd 06 07:47 PM
I need a Lookup to return more than 1 result joe1182 Excel Worksheet Functions 8 February 1st 06 02:52 PM


All times are GMT +1. The time now is 10:18 PM.

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"