G
George Hester
This particular statement executes each time the program it is in runs:
Call dbHosts(Cat, "MyDatabase.mdb", "Table1")
This is the Sub:
Option Explicit
Const MAX_CHARACTERS = 255
Public Sub dbHosts(Cat1 As ADOX.Catalog, dB As String, strTableName As String, Optional strKeyField As String = "Key_Field", Optional strField As String)
Dim Cn
Dim objTable(1 To 2) As New ADOX.Table
Set Cn = CreateObject("ADODB.Connection")
Cn.Open ("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & dB)
Set objTable(1).ParentCatalog = Cat1
Set Cat1.ActiveConnection = Cn
objTable(1).Name = strTableName
If strKeyField <> "Key_Field" Then
objTable(1).Columns.Append strField, adWChar, 10
objTable(1).Columns.Append strKeyField, adWChar, MAX_CHARACTERS
objTable(1).Columns(strKeyField).Properties("Jet OLEDB:Allow Zero Length") = False
objTable(1).Keys.Append "PrimaryKey", adKeyPrimary, strKeyField
Else
objTable(1).Columns.Append strKeyField, adInteger
objTable(1).Columns(strKeyField).Properties("AutoIncrement") = True
objTable(1).Columns.Append "strLocation", adWChar, MAX_CHARACTERS ' 255
objTable(1).Keys.Append "PrimaryKey", adKeyPrimary, strKeyField
End If
Cat1.Tables.Append objTable(1)
'
Set objTable(1) = Nothing
Set objTable(2) = Nothing
Cn.Close
Set Cn = Nothing
End Sub
This works fine. But my trouble is why don't I get an error if this Table already exists in the database? It seems that once the table is made this particular Sub dbHosts gets ignored. Can anyone shed some light on what is going on here? Thanks.
Call dbHosts(Cat, "MyDatabase.mdb", "Table1")
This is the Sub:
Option Explicit
Const MAX_CHARACTERS = 255
Public Sub dbHosts(Cat1 As ADOX.Catalog, dB As String, strTableName As String, Optional strKeyField As String = "Key_Field", Optional strField As String)
Dim Cn
Dim objTable(1 To 2) As New ADOX.Table
Set Cn = CreateObject("ADODB.Connection")
Cn.Open ("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & dB)
Set objTable(1).ParentCatalog = Cat1
Set Cat1.ActiveConnection = Cn
objTable(1).Name = strTableName
If strKeyField <> "Key_Field" Then
objTable(1).Columns.Append strField, adWChar, 10
objTable(1).Columns.Append strKeyField, adWChar, MAX_CHARACTERS
objTable(1).Columns(strKeyField).Properties("Jet OLEDB:Allow Zero Length") = False
objTable(1).Keys.Append "PrimaryKey", adKeyPrimary, strKeyField
Else
objTable(1).Columns.Append strKeyField, adInteger
objTable(1).Columns(strKeyField).Properties("AutoIncrement") = True
objTable(1).Columns.Append "strLocation", adWChar, MAX_CHARACTERS ' 255
objTable(1).Keys.Append "PrimaryKey", adKeyPrimary, strKeyField
End If
Cat1.Tables.Append objTable(1)
'
Set objTable(1) = Nothing
Set objTable(2) = Nothing
Cn.Close
Set Cn = Nothing
End Sub
This works fine. But my trouble is why don't I get an error if this Table already exists in the database? It seems that once the table is made this particular Sub dbHosts gets ignored. Can anyone shed some light on what is going on here? Thanks.