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

gifariz

Board Regular
Joined
May 2, 2021
Messages
112
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 to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Try enclosing it in square brackets:

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

Forum statistics

Threads
1,214,525
Messages
6,120,052
Members
448,940
Latest member
mdusw

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