I'm trying to create columns in an existing access database
. The database
has already been created, it contains the empty table named "table1", but it does not contain any columns
, now I want to add a column into it. But it produces some error.
Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.
Imports ADOX
Imports ADOX.DataTypeEnum
Imports ADOX.KeyTypeEnum
Imports System.Data.OleDb
Imports System.Data.SqlClient
Dim database_location As String = Application.UserAppDataPath
Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
insert_columns("DB3", "table1", "Key", adInteger, True, True)
End Sub
Function insert_columns(ByRef Database_file_name As String, ByRef table_name As String, ByRef column_name As String, ByRef data_type As ADOX.DataTypeEnum, Optional ByRef primary_key As Boolean = False, Optional ByRef auto_increment As Boolean = False)
Dim DB_file_name As String = "\" & Database_file_name & ".mdb"
Dim catDB As ADOX.Catalog
Dim tblNew As ADOX.Table
' Dim catstring As String
'Try
catDB = New ADOX.Catalog
' Open the catalog.
catDB.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & database_location & DB_file_name
tblNew = New ADOX.Table
' Create a new Table object.
With tblNew
.Name = table_name
.ParentCatalog = catDB
' Create fields and append them to the
' Columns collection of the new Table object.
With .Columns
.Append(column_name, data_type)
If auto_increment = True Then
.Item(column_name).Properties("AutoIncrement").Value = True
End If
If primary_key = True Then
tblNew.Keys.Append("PrimaryKey", KeyTypeEnum.adKeyPrimary, column_name)
End If
End With
End With
' Add the new Table to the Tables collection of the database.
catDB.Tables.Append(tblNew)
'clean up
catDB = Nothing
tblNew = Nothing
Return True
'Catch ex As Exception
' error_entry("Column creation error. Database name: " & DB_file_name & " Column Name: " & column_name & vbNewLine & ex.Message)
' Return False
'End Try
End Function
Argh, didn't read properly that you are using adox. I'll leave the answer just in case.
I used ACE but it should be the same:
If you want to add columns independently then Add columns to an Access (Jet) table from .NET