VBA connect to Ms Access using ADODB.Recordset with non-alphanumeric table name

gifariz

Board Regular
Joined
May 2, 2021
Messages
128
Office Version
  1. 365
Platform
  1. Windows
I am trying to connect to Microsoft Access using ADODB.Recordset in VBA, but apparently it seems to only accept alphanumeric table name.
Changing table name manually in Access is just additional work (and I need to teach it) for the macro user, so it would be great if it can work without doing this.

Here is my code:
VBA Code:
Sub GetAccess()

Dim DBFullName As String
Dim Connect As String, Source As String, TableName As String
Dim Connection As ADODB.Connection
Dim Recordset As ADODB.Recordset
Dim Data

'Database Path
DBFullName = "C:\Analysis\Sample.mdb"

'Open the Connection
Set Connection = New ADODB.Connection
Connect = "Provider=Microsoft.ACE.OLEDB.12.0;"
Connect = Connect & "Data Source=" & DBFullName & ";"
Connection.Open ConnectionString:=Connect

'Create a RecordSet
Set Recordset = New ADODB.Recordset
TableName = "Table - 1"
With Recordset
    Source = "SELECT * FROM " & TableName
    .Open Source:=Source, ActiveConnection:=Connection 'This can connect only if TableName is only alphanumeric, e.g. Table1
    Data = .GetRows
End With

End Sub

Any idea how to make it work? TIA
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Try enclosing it in square brackets:

Code:
Source = "SELECT * FROM [" & TableName & "]"
 
Upvote 0
Solution

Forum statistics

Threads
1,223,948
Messages
6,175,579
Members
452,653
Latest member
craigje92

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