Dynamic table name

Guinaba

Board Regular
Joined
Sep 19, 2018
Messages
233
Office Version
  1. 2016
Platform
  1. Windows
Hi guys,

Not sure what I am missing in the SQL statement below. The table has a dynamic name based on the date. I keep getting the error: Syntax error in FROM clause.

Any suggestion?

VBA Code:
Dim dbs As Database, rst As Recordset, x As Integer

  Set dbs = CurrentDb
  
  strTableName = "tb0_AldiDailyScan" & "_" & Date
  
  Set rst = dbs.OpenRecordset("SELECT * FROM " & strTableName)

  If Not (rst.EOF And rst.BOF) Then
    Do While Not rst.EOF
       If rst.Fields(1).Value = "" Then 'Delete blank records
          rst.Delete
       End If
    rst.MoveNext                 
    Loop
    rst.Close
    Set rst = Nothing
    Set db = Nothing
  End If
End Sub

1626260739700.png
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
How exactly is your table named?

Insert a MsgBox before that line to show yourself the table name your are building:
VBA Code:
MsgBox strTableName

I am willing to bet that you will find that the name being built does not match your actual table name, because you probably need to format the date part of your table name first (as Date usually contains slashes, and I don't think Access likes using those in table names).
 
Upvote 0
Solution
How exactly is your table named?

Insert a MsgBox before that line to show yourself the table name your are building:
VBA Code:
MsgBox strTableName

I am willing to bet that you will find that the name being built does not match your actual table name, because you probably need to format the date part of your table name first (as Date usually contains slashes, and I don't think Access likes using those in table names).
Thanks @Joe4! Spot on! I've removed the slashes from the date and it worked!
 
Upvote 0
Thanks @Joe4! Spot on! I've removed the slashes from the date and it worked!
Excellent! Glad it worked out for you!

I use that method quite a bit when I am trying to build a dynamic value, and things don't work out as I planned. I will use a MsgBox to see what exactly I am building, and make sure that it matches my expectations.
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,189
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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