Experts:
I'd like some assistance with streamlining a DROP TABLE procedure based on # (n) number of data source tables.
At the present time, I have 5 data sources tables. Their names are shown below.
Code:
tbl_Source1
tbl_Source2
tbl_Source3
tbl_Source4
tbl_Source5
Please note that all tables have a prefix of "tbl_". This is important as future source tables may be named as follows:
Code:
tbl_Florida
tbl_Illinois
tbl_Maine
tbl_Texas
tbl_Virgnia
Now, based on the current table naming convention (i.e., tbl_Source1 through tbl_Source5), I use the following code to DROP all 5 tables prior to importing them.
Code:
'Declare variables for DROP TABLE statements
Dim strSQL_Drop1 As String
Dim strSQL_Drop2 As String
Dim strSQL_Drop3 As String
Dim strSQL_Drop4 As String
Dim strSQL_Drop5 As String
'Declare variables for generic strCriteria#
Dim strCriteria1 As String
Dim strCriteria2 As String
Dim strCriteria3 As String
Dim strCriteria4 As String
Dim strCriteria5 As String
'*** Update here (if additional data sources are utilized)
'Assign source tables names to strCriteria#
strCriteria1 = "tbl_Source1"
strCriteria2 = "tbl_Source2"
strCriteria3 = "tbl_Source3"
strCriteria4 = "tbl_Source4"
strCriteria5 = "tbl_Source5"
'Define strSQL_Drop# statements (for all data sources)
strSQL_Drop1 = "DELETE " & strCriteria1 & ".* FROM " & strCriteria1 & ";"
strSQL_Drop2 = "DELETE " & strCriteria2 & ".* FROM " & strCriteria2 & ";"
strSQL_Drop3 = "DELETE " & strCriteria3 & ".* FROM " & strCriteria3 & ";"
strSQL_Drop4 = "DELETE " & strCriteria4 & ".* FROM " & strCriteria4 & ";"
strSQL_Drop5 = "DELETE " & strCriteria5 & ".* FROM " & strCriteria5 & ";"
'Execute all strSQL_Drop# statements
CurrentDb.Execute strSQL_Drop1
CurrentDb.Execute strSQL_Drop2
CurrentDb.Execute strSQL_Drop3
CurrentDb.Execute strSQL_Drop4
CurrentDb.Execute strSQL_Drop5
Although the code executes, this entire section appears to be lengthy & maybe even cumbersome.
My question: Is there a ways to change the code above and, e.g., put it into a For Loop so that every table starting with "tbl_" will be dropped? Naturally if new tables will be added to the DB, those also would have to be dropped. Any new source tables would also have the "tbl_" prefix.
If so, what would that VBA code look like?
Thank you,
Tom