Hi,
I am trying to take code from VB6 and MS Access 2003. I wish to create an MS database, create a table and then a number of fields including one which is a primary index and an autoincrement. I have been going around in circles, using Visual Studio 2010. Perhaps someone can help me with the code below and point me in the right direction.
Many thanks in advance,
Lawrence
===========
This part works
===========
Imports ADOX
Imports System.Data.OleDb
Public Class Form1
Dim databaseName As String = "C:\test.mdb"
Dim tableName As String = "index_info"
Dim cat As ADOX.Catalog = New ADOX.Catalog()
Dim ADOXindex As New ADOX.Index
Dim NewTable = New ADOX.Table
Label1.Text = "Creating Database..."
On Error Resume Next
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & databaseName & ";Jet OLEDB:Engine Type=5")
If Err.Number = 0 Then
On Error GoTo 0
'cat = Nothing
Dim con As New OleDb.OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source =" & databaseName)
con.Open()
'Get database schema
Dim dbSchema As DataTable = con.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, tableName, "TABLE"})
con.Close()
con.Open()
Dim cmd As New OleDb.OleDbCommand("CREATE TABLE [" + tableName + "] ( [rec_id] DOUBLE, [Frame_Name] TEXT, [Frame_count] DOUBLE, [Frame_Id] DOUBLE, [Start_Sector] DOUBLE, [Created] DATE, [Amended] DATE, [Last_sync] DATE)", con)
cmd.ExecuteNonQuery()
==============================================================
but I wish to add fields with the With statements. I can't get this next part to work
==============================================================
NewTable.Name=tableName
With NewTable.Columns
.Append "ContractorID", long
End With
==============================================================
This part compiles but the index is not added to the database and I also can't change the "rec_id" to be autoincrement
==============================================================
ADOXindex = New ADOX.Index
With ADOXindex
.Name = "rec_id_index"
.PrimaryKey = True
.Unique = True
.Columns.Append("rec_id")
End With
NewTable.Indexes.Append(ADOXindex)
con.Close()
ADOXindex = Nothing
cat = Nothing
MsgBox("Database created...ok")
Label1.Text = "Creating Database...Done!"
Else
Label1.Text = "Creating Database...Database already exists!"
MsgBox("Database not created")
End If
End Sub
End Class
I am trying to take code from VB6 and MS Access 2003. I wish to create an MS database, create a table and then a number of fields including one which is a primary index and an autoincrement. I have been going around in circles, using Visual Studio 2010. Perhaps someone can help me with the code below and point me in the right direction.
Many thanks in advance,
Lawrence
===========
This part works
===========
Imports ADOX
Imports System.Data.OleDb
Public Class Form1
Dim databaseName As String = "C:\test.mdb"
Dim tableName As String = "index_info"
Dim cat As ADOX.Catalog = New ADOX.Catalog()
Dim ADOXindex As New ADOX.Index
Dim NewTable = New ADOX.Table
Label1.Text = "Creating Database..."
On Error Resume Next
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & databaseName & ";Jet OLEDB:Engine Type=5")
If Err.Number = 0 Then
On Error GoTo 0
'cat = Nothing
Dim con As New OleDb.OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source =" & databaseName)
con.Open()
'Get database schema
Dim dbSchema As DataTable = con.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, tableName, "TABLE"})
con.Close()
con.Open()
Dim cmd As New OleDb.OleDbCommand("CREATE TABLE [" + tableName + "] ( [rec_id] DOUBLE, [Frame_Name] TEXT, [Frame_count] DOUBLE, [Frame_Id] DOUBLE, [Start_Sector] DOUBLE, [Created] DATE, [Amended] DATE, [Last_sync] DATE)", con)
cmd.ExecuteNonQuery()
==============================================================
but I wish to add fields with the With statements. I can't get this next part to work
==============================================================
NewTable.Name=tableName
With NewTable.Columns
.Append "ContractorID", long
End With
==============================================================
This part compiles but the index is not added to the database and I also can't change the "rec_id" to be autoincrement
==============================================================
ADOXindex = New ADOX.Index
With ADOXindex
.Name = "rec_id_index"
.PrimaryKey = True
.Unique = True
.Columns.Append("rec_id")
End With
NewTable.Indexes.Append(ADOXindex)
con.Close()
ADOXindex = Nothing
cat = Nothing
MsgBox("Database created...ok")
Label1.Text = "Creating Database...Done!"
Else
Label1.Text = "Creating Database...Database already exists!"
MsgBox("Database not created")
End If
End Sub
End Class