|
|
Title | Use an ALTER TABLE statement to change a column's data type in an Access database in Visual Basic 6 |
Description | This example shows how to use an ALTER TABLE statement to change a column's data type in an Access database in Visual Basic 6. |
Keywords | ALTER TABLE, database, ADO, Access, Visual Basic 6 |
Categories | Database, Office |
|
|
The program executes a SQL statement similar to:
ALTER TABLE People ALTER COLUMN Age DECIMAL(5,2) NOT NULL
When you click the Execute button, the following code creates a connection to the database. It uses the connection object's Execute method to execute the SQL command and then closes the connection.
|
|
' Add a reference to "Microsoft ActiveX Data Objects 2.0
' Library"
' (or whatever version you have).
Private Sub btnExecute_Click()
Dim conn As ADODB.Connection
Dim query As String
' Open the connection.
Set conn = New ADODB.Connection
conn.ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Persist Security Info=False;" & _
"Data Source=" & txtDatabase.Text
conn.Open
' Modify the field.
On Error GoTo AlterFieldError
conn.Execute txtStatement.Text
conn.Close
MsgBox query & vbCrLf & "Ok"
Exit Sub
AlterFieldError:
MsgBox "Error " & Err.Number & _
" executing statement" & _
vbCrLf & Err.Description
conn.Close
Exit Sub
End Sub
|
|
|
|
|
|