Hi!
I'm using VBA in Access 2007.
I am looping though a list of all table names in all (selected) external databases and need to append the external data to my current database table (if the table exists in my current database) or make a new table with the data (if the table does not exist in my current database). I'm missing one component which is a "refresh" after I make the new table.
Currently, when the table does not exist, it makes the table, but when I get to the next database and it looks to see if that table exists, it says it doesn't, and keeps making a new one.
Thanks in advance!
I'm using VBA in Access 2007.
I am looping though a list of all table names in all (selected) external databases and need to append the external data to my current database table (if the table exists in my current database) or make a new table with the data (if the table does not exist in my current database). I'm missing one component which is a "refresh" after I make the new table.
Currently, when the table does not exist, it makes the table, but when I get to the next database and it looks to see if that table exists, it says it doesn't, and keeps making a new one.
Thanks in advance!
Code:
'Count records in external table strSQL = "SELECT [" & strTableName & "].*" strSQL = strSQL & "FROM [" & strTableName & "] " strSQL = strSQL & "IN '" & strDatabasePath & "'" intRecordcount = funRecordCount(strSQL) 'Does external table exist in current database blnTableExists = funTableExists(strTableName) 'If external database exists in current database then append If blnTableExists = True Then AppendToExistingTable 'If external database does not exist in current database then make table ElseIf blnTableExists = False Then MakeNewTable End If
Code:
Public Function funTableExists(strNewTableName) As Boolean On Error GoTo Err_funTableExists Dim db As Database Dim tdf As TableDef Set db = DBEngine(0)(0) funTableExists = False For Each tdf In db.TableDefs Debug.Print tdf.Name If tdf.Name = strNewTableName Then funTableExists = True Next tdf Set db = Nothing Exit_funTableExists: Exit Function Err_funTableExists: MsgBox Err.Description Resume Exit_funTableExists End Function
Code:
Sub MakeNewTable() On Error GoTo Err_MakeNewTable DoCmd.SetWarnings False strSQL = "SELECT [" & strTableName & "].* " strSQL = strSQL & "INTO [" & strTableName & "] " strSQL = strSQL & "FROM [" & strTableName & "] " strSQL = strSQL & "IN '" & strDatabasePath & "'" DoCmd.RunSQL (strSQL) DoCmd.SetWarnings True Exit_MakeNewTable: Exit Sub Err_MakeNewTable: MsgBox Err.Description Resume Exit_MakeNewTable End Sub
Comment