Well... its not that I have a problem and need to create tables at run time so much I guess, its just something that I learned/did recently. I was creating massive address lists from different sources where field names/types could be different, and also I was creating 3 additional fields (Filter1, Filter2, and Filter 3). By using a match list I could match the incoming field names with the internal field names and generate a new table using the internal names. Then I could create 3 filter tables ( essentially a list of filter terms (like subdivision) which are used in comboboxes for filter construction) Then the subsequent queries /filtering/reporting modules all work with no code modification.
Following is an example of what I did [u]
IN ACCESS</u>, however, its not that I need to do this right now, what I am wondering is how do I create the tables in code. I guess you could say I have lived in a world (Access), where the database and the rest of the program co-exist in the same package. Now when studying the
VB all references seem to use
VB only to select and manipulate the data, not to create the actual database. Maybe I am asking ridiculous questions... thats possible ... I need someone to set me straight.
I have several access applications that I want to recreate in VB9 with visual studio, and I am asking "OK, where do we build the tables? Is this resource in Visual Studio? Do I create mdb's and then connect to them? Do I build the tables one by one? Do I create new sql database and then add tables to it?
So what your saying is the data source is/should be a separate entity and that the purpose of
vb is to create the UI?
Sorry for the lengthy response.
Private Sub CreateTable()
Dim db As DAO.Database
Dim t_DI As DAO.TableDef 't_Data_Internal
Dim rec As Recordset
Dim fld As DAO.Field
Dim idx As DAO.Index
Dim FieldPosition As Integer
Dim tblExists As Boolean
Dim tbl As DAO.TableDef
FieldPosition = 1
Set db = CurrentDb
DeleteTable ("t_Data_Internal")
Set t_DI = db.CreateTableDef("t_Data_Internal")
Set rec = db.OpenRecordset("t_Data_InternalFieldNames")
rec.MoveFirst
While Not rec.EOF
If Not IsNull(rec(2)) Then
'This is reading a data type table that was created automatically
Set fld = t_DI.CreateField(rec(1), rec(3), rec(4))
fld.OrdinalPosition = FieldPosition
t_DI.Fields.Append fld
FieldPosition = FieldPosition + 1
Else
End If
rec.MoveNext
Wend
rec.Close
db.TableDefs.Append t_DI
RefreshDatabaseWindow
End Sub
Private Sub CreateFilterTables()
Dim db As DAO.Database
Dim tbl As DAO.TableDef
Dim qdf As DAO.QueryDef
Dim rec As DAO.Recordset
Dim fld As DAO.Field
Dim frm As Form
Dim FilterName, tblName, qryName, frmName As String
Dim x As Integer
Dim FiltSQL As String
Dim FieldType, FieldSize As Integer
Set db = CurrentDb()
'check for filter tables and remove them
For x = 1 To 3
tblName = "t_Filt" & x
DeleteTable (tblName)
'fetch the fieldname of the current proposed filter
FiltSQL = "SELECT t_Data_InternalFieldNames.ImportedDFN, t_Data_Inte" _
& "rnalFieldNames.InternalDFT, t_Data_InternalFieldNames.Int" _
& "ernalDFS" & vbNewLine & "FROM t_Data_InternalFieldNames" _
& vbNewLine & "WHERE (((t_Data_InternalFieldNames.InternalDFN)=" _
& Chr$(34) & "FILTER" & x & Chr$(34) & "));"
Set qdf = db.CreateQueryDef("q_GetFiltName")
qdf.SQL = FiltSQL
Set rec = db.OpenRecordset("q_GetFiltName")
rec.MoveFirst
FilterName = rec(0)
FieldType = rec(1)
FieldSize = rec(2)
'rec(0).
rec.Close
db.QueryDefs.Delete "q_GetFiltName"
'now create the table
Set tbl = db.CreateTableDef(tblName)
Set fld = tbl.CreateField(FilterName, FieldType, FieldSize)
tbl.Fields.Append fld
'fld.defa
db.TableDefs.Append tbl
'Insert the list of values into the new table
FiltSQL = "INSERT INTO " & tblName & " (" & FilterName & ")" & vbNewLine _
& "SELECT t_Data_Internal.FILTER" & x & vbNewLine _
& "FROM t_Data_Internal" & vbNewLine _
& "GROUP BY t_Data_Internal.FILTER" & x & ";"
DoCmd.SetWarnings False
DoCmd.RunSQL FiltSQL
DoCmd.SetWarnings True
'now create the query for the table
qryName = "q_Filt" & x
'first delete the old query
DeleteQuery (qryName)
FiltSQL = "SELECT t_Filt" & x & "." & FilterName & vbNewLine & "FROM t_Filt" & x & ";"
'then create the new one
Set qdf = db.CreateQueryDef(qryName)
qdf.SQL = FiltSQL
frmName = "sf_Filt" & x
Next x
RefreshDatabaseWindow
End Sub