How to modify the field name in ACCESS in VB 6.0? I need the complete code, thanks!
Since the ACCESS JET engine does not support modifying table names and column names, field names cannot be modified by running SQL data definition queries.
The following is the complete code for using the ADO data access object extension class library ADOX to modify the column name. Note that the open data table must be closed before modifying the table name, otherwise the modification will fail.
First you need to reference ADOX and ADO
Quotation method:
Open the VBE window
Quote ADOX——Menu "Project"-gt ; "Quote"--gt; Check Microsoft ADO Ext.2.7 for DDL ado Security or higher, click the OK button
Quote ADO——Menu "Project"-gt; "Quote"-- gt; Check Microsoft ActiveX Data Objects 2.1 Library or higher and click the OK button
Private Sub Command1_Click()
On Error GoTo ErrHandler
Dim Cat As New ADOX.Catalog
Dim Cnn As New ADODB.Connection
'Connect to the Sample.mdb ACCESS database in the current directory
Cnn.Open "Provider=' Microsoft.Jet.OLEDB.4.0'" & _
"Data Source= '" & App.Path & "\Sample.mdb'"
Set Cat .ActiveConnection = Cnn
Cat.tables("CCC").Columns("AAA") = "BBB" 'Modify the table name
MsgBox "The table column name has been modified"
'Release ADO object
Cnn.Close
Set Cat = Nothing
Set Cnn = Nothing
Exit Sub
ErrHandler: 'Error handling
Set Cat = Nothing
If Not Cnn Is Nothing Then
If Cnn.State = adStateOpen Then Cnn.Close
End If
Set Cnn = Nothing
If Err lt;gt; 0 Then
MsgBox Err.Source amp; "--gt;" amp; Err.Description, , "Error"
End If
End Sub