lookup from Excel into Access

acash85

New Member
Joined
Oct 22, 2010
Messages
1
Hi all, I need some professional advice. I'm new to VBA and have very limited knowledge. But I need to create a macro that will automate some information me. See below for my problem/situation.

I have multiple accounts that have different product codes on there such as…

Act 1.
Prod Code MFR Unit of Measure List Price
098765 XXXX Each $10.00
123456 YYYY Pack $50.00

And I have a large database in Access which has over 12 million product codes, too much for Excel to handle.

Prod Code List Price MFR Unit of Measure
098765 $10.00 XXXX Each
123456 $50.00 YYYY Pack
165786 …. …… ………
987654 …. …… ………

I need to create a macro in Excel via VBA that will go into my access database and pull information for specific product codes only.
For example, if all I have is the prod code number I need to run an automated macro via VBA that will go into access and do vlookup (or DBVlookup) to fill in the rest of the blanks….

Act 1.
Prod Code MFR Unit of Measure List Price
098765 ??? ???? ?????
123456 ??? ???? ?????

Is there a way to create a macro that will pull data for a specific cell only. Its basically a Vlookup formula, but instead of pulling data into Excel from Excel, it pulls data into Excel from Access.

I appreciate your help.
Thanks
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Something like this.
I would be grateful if you would let me know if the code works best (faster) with or without the "POPULATE TABLE" section. It will happen automatically without the code, but I wondered if there might be more of a delay with a big table one way or the other. Make sure your lookup field is indexed - preferable "no duplicates" assuming this is the case.

Code:
'===============================================================================
'- LOOK UP ACCESS DATA TO EXCEL WORKSHEET
'- Lookup data in column A. Returns Access fields to columns
'- To speed this up make sure that the Access lookup field is indexed & "no duplicates"
'- Needs Tools\Reference to Microsoft Access xx Library & Microsoft DAO Object Library
'- Brian Baulsom October 2010
'===============================================================================
Sub ACCESS_LOOKUP()
    Dim ws As Worksheet
    Dim MyRow As Long
    Dim LastRow As Long
    Dim MyValue As String
    '---------------------------------------------------------------------------
    Dim MyDB As DAO.Database          ' Access .mdb file
    Dim MyTable As DAO.Recordset     ' Access Table
    '---------------------------------------------------------------------------
    '- OPEN THE ACCESS TABLE
    '---------------------------------------------------------------------------
    Application.Calculation = xlCalculationManual
    Set MyDB = Workspaces(0).OpenDatabase("F:\XL_MACROS\Access\Lookup.mdb")
    Set MyTable = MyDB.OpenRecordset("AccessLookupTable", dbOpenDynaset)
    '---------------------------------------------------------------------------
    '- POPULATE THE TABLE
    '- ** nb. Test with & without this code to check speed of operation
    With MyTable
        .MoveLast
        .MoveFirst
    End With
    '---------------------------------------------------------------------------
    '- WORKSHEET LOOP
    '---------------------------------------------------------------------------
    Set ws = ActiveSheet
    LastRow = ws.Range("A65536").End(xlUp).Row
    For MyRow = 2 To LastRow
        Application.StatusBar = " Row " & MyRow & "\" & LastRow
        MyValue = CStr(ws.Cells(MyRow, "A").Value)
        '-----------------------------------------------------------------------
        '- look up "PRODUCT" FIELD IN THE TABLE. RETURN "Field1" .. etc.
        With MyTable
            MyTable.FindFirst "PRODUCT='" & MyValue & "'"
            If .NoMatch Then
                ws.Cells(MyRow, 2).Value = "no match"
            Else
                ws.Cells(MyRow, 2).Value = .Fields("Field2").Value
                ws.Cells(MyRow, 3).Value = .Fields("Field3").Value
                ws.Cells(MyRow, 4).Value = .Fields("Field4").Value
            End If
        End With
        '-----------------------------------------------------------------------
    Next
    '---------------------------------------------------------------------------
    '- FINISH
    MsgBox ("done")
    Application.Calculation = xlCalculationAutomatic
    Application.StatusBar = False
    Set MyTable = Nothing
    Set MyDB = Nothing
End Sub
'--------------------------------------------------------------------------------
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,824
Members
449,050
Latest member
Bradel

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top