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:
Any idea how to make it work? TIA
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