Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse an ALTER TABLE statement to change a column's data type in an Access database in Visual Basic 6
DescriptionThis example shows how to use an ALTER TABLE statement to change a column's data type in an Access database in Visual Basic 6.
KeywordsALTER TABLE, database, ADO, Access, Visual Basic 6
CategoriesDatabase, 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
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated