Facing issues while importing data from MS Access to MS Excel

BizBoy

Board Regular
Joined
Jul 26, 2012
Messages
118
Hi,

I am trying to edit the code from below mentioned website to suite my requirements.
However, before I could finish editing, I could see few of the code lines are reflecting in red.

I have mentioned 'Code is reflecting in red line below' just above the problematic code.
Can anyone please help me in resolving this. I do not have any idea why these lines are reflecting in red.


https://www.exceltrainingvideos.com/get-data-from-access-database-using-excel-vba/

Code:
Option Explicit
Sub getDataFromAccess()
    'Click on Tools, References and select
    'the Microsoft ActiveX Data Objects 2.0 Library
    
    Dim DBFullName As String
    Dim Connect As String, Source As String
    Dim Connection As ADODB.Connection
    Dim Recordset As ADODB.Recordset
    Dim Col As Integer
    
    Cells.Clear
    
    'Database path info
    
    'Your path will be different
    DBFullName = "\\A\B\C\My Documents\Database4.accdb"
    'Open the connection
    Set Connection = New ADODB.Connection
    
    'Code is reflecting in red line below
    Connect = “Provider=Microsoft.ACE.OLEDB.12.0;”
    
    'Code is reflecting in red line below
    Connect = Connect & “Data Source=” & DBFullName & “;”
    Connection.Open ConnectionString:=Connect
    
    'Create RecordSet
    Set Recordset = New ADODB.Recordset
    With Recordset
    'Filter Data
    
    'Code is reflecting in red line below
    Source = “SELECT * FROM Orders WHERE [Shipper ID] = 3 ”
    .Open Source:=Source, ActiveConnection:=Connection
    
    'Write field names
    
    'Code is reflecting in red line below
    For Col = 0 To Recordset.Fields.Count – 1
        Range(“A1”).Offset(0, Col).Value = Recordset.Fields(Col).Name
    Next
    
    'Write recordset
    Range(“A1”).Offset(1, 0).CopyFromRecordset Recordset
    End With
    ActiveSheet.Columns.AutoFit
    Set Recordset = Nothing
    Connection.Close
    Set Connection = Nothing
 End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hi,

I guess, I am able to edit the code now.


This character was creating problems while doing copy paste. Thanks.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,221,672
Messages
6,161,199
Members
451,688
Latest member
Gregs44132

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