'*******************************************************************************
'Function: TableDefExist(strTableDef)
'Description: Returns a Boolean value that indicates whether an table define
' in currently database.
'Example: TableDefExist("TEXT")=True
'*******************************************************************************
Function TableDefExist(ByVal strTableDef As String) As Boolean
On Error GoTo TableDefExist_Err
If CurrentDb.TableDefs(strTableDef).Name = strTableDef Then
TableDefExist = True
End If
TableDefExist = True
Exit Function
TableDefExist_Err:
TableDefExist = False
Exit Function
End Function
Private Sub CreateTRDTableDef()
On Error GoTo Err_CreateTRDTableDef
Dim rstTRDTableSource As DAO.Recordset
Dim rstTableDefine As DAO.Recordset
Dim tdfTable As DAO.TableDef
Dim dbCurrentDatabase As DAO.Database
Dim fldField As Field
Dim intCount As Integer
Dim strTableName As String
DoCmd.Echo True, "Creating table definition......"
Set dbCurrentDatabase = CurrentDb
Set rstTRDTableSource = dbCurrentDatabase.OpenRecordset("SELECT DISTINCT TRD_NAME,TABLE_NAME FROM TBL_TABLE_SOURCE", dbOpenDynaset)
Do While Not rstTRDTableSource.EOF
strTableName = rstTRDTableSource("TRD_NAME") & " - " & rstTRDTableSource("TABLE_NAME")
DoCmd.Echo True, "Creating " & strTableName & " table definition....."
If TableDefExist(strTableName) Then
dbCurrentDatabase.TableDefs.Delete strTableName
End If
Set rstTableDefine = CurrentDb.OpenRecordset("SELECT * FROM TBL_TABLE_SOURCE WHERE TRD_NAME=" & "'" & _
rstTRDTableSource("TRD_NAME") & "' AND TABLE_NAME='" & rstTRDTableSource("TABLE_NAME") & "' ORDER BY SEQUENCE", dbOpenDynaset)
Set tdfTable = dbCurrentDatabase.CreateTableDef(strTableName)
Set fldField = tdfTable.CreateField(rstTableDefine.Fields("FIELD_NAME"), GedFieldType(rstTableDefine.Fields("DATA_TYPE")), rstTableDefine.Fields("FIELD_SIZE"))
tdfTable.Fields.Append fldField
dbCurrentDatabase.TableDefs.Append tdfTable
SetMyProperty fldField, "Caption", dbText, rstTableDefine.Fields("Caption")
SetMyProperty fldField, "Description", dbText, rstTableDefine.Fields("DESCRIPTION")
rstTableDefine.MoveNext
With rstTableDefine
Do While Not .EOF
Set fldField = tdfTable.CreateField(.Fields("FIELD_NAME"), GedFieldType(.Fields("DATA_TYPE")), .Fields("FIELD_SIZE"))
tdfTable.Fields.Append fldField
SetMyProperty fldField, "Caption", dbText, rstTableDefine.Fields("Caption")
SetMyProperty fldField, "Description", dbText, rstTableDefine.Fields("DESCRIPTION")
.MoveNext
Loop
End With
Set tdfTable = Nothing
rstTableDefine.Close
Set rstTableDefine = Nothing
rstTRDTableSource.MoveNext
Loop
rstTRDTableSource.Close
Set rstTRDTableSource = Nothing
DoCmd.Echo True, "Ready"
Exit_CreateTRDTableDef:
Exit Sub
Err_CreateTRDTableDef:
MsgBox "Error: " & Err & vbCrLf & Err.Description
Resume Exit_CreateTRDTableDef
End Sub
'*******************************************************************************
'Function: GedFieldType(strDataType)
'Description: Returns a integer value that indicates data types
'Example: GedFieldType("dbText")=10
'*******************************************************************************
Function GedFieldType(strDataType As String) As Integer
Select Case strDataType
Case "dbText"
GedFieldType = 10
Case "dbDate"
GedFieldType = 8
Case "dbDouble"
GedFieldType = 7
Case "dbFloat"
GedFieldType = 21
Case "dbInteger"
GedFieldType = 3
Case "dbLong"
GedFieldType = 4
Case "dbMemo"
GedFieldType = 12
Case "dbNumeric"
GedFieldType = 6 'old is 19
Case "dbSingle"
GedFieldType = 6
Case "dbTime"
GedFieldType = 22
Case "dbChar"
GedFieldType = 18
Case "dbCurrency"
GedFieldType = 5
Case Else
GedFieldType = 0
End Select
End Function
'*******************************************************************************
'Sub: SetMyProperty(Obj,Name,Type,Setting)
'Description: Custom a user property
'Example: SetMyProperty fldField, "Caption", dbText, "Test Information"
'*******************************************************************************
Sub SetMyProperty(Obj As Object, strName As String, intType As Integer, strSetting As String)
Dim Prp As Property
Const PrpFail As Integer = 3270
On Error GoTo Err_SetMyProperty
Obj.Properties(strName) = strSetting
Obj.Properties.Refresh
Exit_SetMyProperty:
Exit Sub
Err_SetMyProperty:
If Err = PrpFail Then
Set Prp = Obj.CreateProperty(strName, intType, strSetting)
Obj.Properties.Append Prp
Obj.Properties.Refresh
Else
MsgBox "Error: " & Err & vbCrLf & Err.Description
End If
Resume Exit_SetMyProperty
End Sub
|