Sub ShowCalls()
' This is how you call the functions
' This deletes the records from the destination table unless you wish to keep them
Call Assembler("delete", destTBLname)
' This appends a source table to the destination table
Call Assembler("append", destTBLname, srcTBLname)
' Technically this is cleanup - after appending, I didn't need the original table anymore
If ObjectExists("Table", srcTBLname) Then
DoCmd.DeleteObject acTable, srcTBLname
End If
End Sub
Public Function Assembler(ByVal strMode As String, ByVal tblName As String, _
Optional ByVal tblSource As String)
Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Set dbs = CurrentDb()
Select Case strMode
Case "delete"
strSQL = "DELETE * FROM " & tblName
Case "append"
strSQL = "INSERT INTO " & tblName & " ( " & GetFlds(tblName) & " ) "
strSQL = strSQL & "SELECT " & GetFlds(tblSource) & " FROM " & tblSource
Case "make"
strSQL = "SELECT " & GetFlds(tblSource) & " INTO " & tblName & " FROM " & tblSource
Case Else:
End Select
DoCmd.RunSQL strSQL
Set rs = Nothing
Set dbs = Nothing
End Function
Public Function GetFlds(ByVal myTable As String, Optional ByVal myType As String) As String
Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Dim x As Integer
Set dbs = CurrentDb()
strSQL = "SELECT * FROM " & myTable
Set rs = dbs.OpenRecordset(strSQL, dbOpenSnapshot)
With rs
For x = 0 To .Fields.Count - 1
Select Case .Fields(x).Name
Case "Date", "Now", "Field"
GetFlds = GetFlds & "[" & .Fields(x).Name & "], "
Case Else:
GetFlds = GetFlds & .Fields(x).Name & ", "
End Select
Next x
End With
' This cleans up the results removing the final comma and space
GetFlds = Trim(Left(GetFlds, Len(GetFlds) - 2))
End Function
Function ObjectExists(ByVal strObjectType As String, _
ByVal strObjectName As String) As Boolean
Dim db As Database
Dim tbl As TableDef
Dim qry As QueryDef
Dim i As Integer
On Error GoTo HandleErr
Set db = CurrentDb()
ObjectExists = False
If strObjectType = "Table" Then
For Each tbl In db.TableDefs
If tbl.Name = strObjectName Then
ObjectExists = True
Exit Function
End If
Next tbl
ElseIf strObjectType = "Query" Then
For Each qry In db.QueryDefs
If qry.Name = strObjectName Then
ObjectExists = True
Exit Function
End If
Next qry
ElseIf strObjectType = "Form" Or strObjectType = "Report" Or strObjectType = "Module" Then
For i = 0 To db.Containers(strObjectType & "s").Documents.Count - 1
If db.Containers(strObjectType & "s").Documents(i).Name = strObjectName Then
ObjectExists = True
Exit Function
End If
Next i
ElseIf strObjectType = "Macro" Then
For i = 0 To db.Containers("Scripts").Documents.Count - 1
If db.Containers("Scripts").Documents(i).Name = strObjectName Then
ObjectExists = True
Exit Function
End If
Next i
Else
MsgBox "Invalid Object Type passed, must be Table, Query, Form, Report, Macro, or Module"
End If
End Function