Private Function RSLoop(tblName As String)
Dim rs, rsFields As Recordset
Dim db As Database
Dim fldLoop As Field
Set db = CurrentDb
Set rs = db.OpenRecordset(tblName)
'create a table called "Fields" to use this with a FieldID, FieldType & FieldSize fields
Set rsFields = db.OpenRecordset("tblFields", dbOpenDynaset)
For Each fldLoop In db.TableDefs(tblName).Fields
With rsFields
.FindFirst ("FieldID = '" & fldLoop.Name & "'")
If .NoMatch Then
.AddNew
!FieldID = fldLoop.Name
!FieldType = FieldType(fldLoop.Type)
!FieldSize = fldLoop.Size
'optional
!FieldRequired = IIf(fldLoop.Required, "Not Null", "")
.Update
End With
Next fldLoop
End Function
Private Function MkTableQ()
Dim rsFields As Recordset
Dim db As Database
Dim strMkTableQ As String
Set db = CurrentDb
Set rsFields = db.OpenRecordset("tblFields")
strMkTableQ = "create table MyBigFatTable ("
With rsFields
.MoveLast
.MoveFirst
If .RecordCount > 0 Then
Do Until .EOF
strMkTableQ = strMkTableQ & !FieldID & " " & !FieldType & "(" & !FieldSize & ") " & !FieldRequired
.MoveNext
If Not .EOF Then
strMkTableQ = strMkTableQ & ", "
Else
strMkTableQ = strMkTableQ & "; "
End If
Loop
DoCmd.RunSQL strMkTableQ
End Function
Private Function AppendQueries(tblName As String)
Dim AppendQ As String
'not sure if this bit works, dont have the time to test it
AppendQ = ""
AppendQ = "INSERT INTO MyBigFatTable SELECT " & tblName & ".* FROM " & tblName & ";"
DoCmd.RunSQL AppendQ
End Function
Private Sub Test_Click()
'place this sub on some button
RSLoop ("Table1")
RSLoop ("Table2")
RSLoop ("Table3")
RSLoop ("Table4")
RSLoop ("Table5")
'might need to make the table name a bit more dynamic
'or for user to pick
MkTableQ
AppendQueries ("Table1")
AppendQueries ("Table2")
AppendQueries ("Table3")
AppendQueries ("Table4")
AppendQueries ("Table5")
End Sub
Function FieldType(intType As Integer) As String
Select Case intType
Case dbBoolean
FieldType = "Boolean"
Case dbByte
FieldType = "Byte"
Case dbInteger
FieldType = "Integer"
Case dbLong
FieldType = "Long"
Case dbCurrency
FieldType = "Currency"
Case dbSingle
FieldType = "Single"
Case dbDouble
FieldType = "Double"
Case dbDate
FieldType = "Date"
Case dbText
FieldType = "Text"
Case dbLongBinary
FieldType = "LongBinary"
Case dbMemo
FieldType = "Memo"
Case dbGUID
FieldType = "GUID"
End Select
End Function