Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 109
Default Lookup in Access return result to Excel

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!!
  #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!!

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 109
Default Lookup in Access return result to Excel

AE, ECT, and table2 will be in Access, and Range("A" & rws).value is in
excel. I want to loop through the column ERCOT in table2 of Access until I
find my value from Excel and return the corresponding value in column AE in
table2 of Access to a variable in Excel. I think the below searches through
Excel and then places a value into Excel??


"Datasort" wrote:

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!!

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 50
Default Lookup in Access return result to Excel

Brent,

The code below will look at each row in Excel column A, Make a call to
Access to find the lookup value in the table 2 and return the lookup value
into column B in Excel.

I was under the impression that you were passing an Excel value to Access to
return some value that you would store in Excel. That is what this code does.

Try it then let me know if it works for your solution.

--
Stewart Rogers
DataSort Software, L.C.
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
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 03:49 AM.

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

About Us

"It's about Microsoft Excel"