How to use SELECT * FROM to copy data from access using excel VBA

swerider

New Member
Joined
Jul 22, 2013
Messages
11
Hi
I found some code that should do what I need, but can't figure out how to get it to work.
I need to copy a field entry "Request_Number" from an Access table "Activity_Requests" using excel vba and then store it as a string "OldRequestNo".
I want to use the automatic ID field in Access as a reference in determining which record I want info from. In this case, the highest ID.

Please help me adapt it

Code:
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim sSQL As String
     
     '// Use the Tools/References menu to add a reference to Microsoft DAO 3.xx Object Library
     '// Change the filename/path as required
    Set db = OpenDatabase("\\Work Request System.accdb")
     
     '// Selecting all records where Field1 = Cell1 and Field2 = Cell2
    sSQL = "Select * From Activity_Requests Where [XXXXX] = " & Range("C3").Value & _
    " And  [XXXXX] = " & Range("B3").Value
     
     '// Get records
    Set rs = db.OpenRecordset(sSQL, dbOpenSnapshot)
     
     '// Anything found?
    If Not rs.EOF Then
         
         '// Paste recordset into worksheet - Change Cell address as needed
        ActiveSheet.Range("A21").CopyFromRecordset rs
         
    End If
     
     '// Tidy up...
    rs.Close
    Set rs = Nothing
    db.Close
    Set db = Nothing

I don't want anything to be copied to any cells in excel, just to be stored as a variable.
Thanks!
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

Forum statistics

Threads
1,221,831
Messages
6,162,242
Members
451,756
Latest member
tommyw

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