Dim cn As ADODB.Connection Set cn = New ADODB.Connection cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\test.mdb;Persist Security Info=False; Jet OLEDB:Database Password:Database Password=密码" cn.Execute "ALTER TABLE 表 ADD COLUMN 新字段 字段类型" cn.Close Set cn = Nothing
Dim R As New ADODB.Recordset Dim DBc As ADODB.Connection Set DBc = New ADODB.Connection DBc.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\main.mdb;Persist Security Info=False" DBc.Open R.Open "alter table (表名) add (字段) (类型)", DBc, adOpenStatic, adLockPessimistic, adCmdTextok
Sub OpenConnectionX() Dim wrkODBC As Workspace Dim conPubs As Connection Dim conPubs2 As Connection Dim conPubs3 As Connection Dim conLoop As Connection ' Create ODBCDirect Workspace object. Set wrkODBC = CreateWorkspace("NewODBCWorkspace", _ "admin", "", dbUseODBC) ' Open Connection object using supplied information in ' the connect string. If this information were ' insufficient, you could trap for an error rather than ' go to an ODBC Driver Manager dialog box.MsgBox "Opening Connection1..." Set conPubs = wrkODBC.OpenConnection("Connection1", _ dbDriverNoPrompt, , _ "ODBC;DATABASE=pubs;UID=sa;PWD=;DSN=Publishers") ' Open read-only Connection object based on information ' you enter in the ODBC Driver Manager dialog box. MsgBox "Opening Connection2..." Set conPubs2 = wrkODBC.OpenConnection("Connection2", _ dbDriverPrompt, True, "ODBC;DSN=Publishers;") ' Open read-only Connection object by entering only the ' missing information in the ODBC Driver Manager dialog ' box. MsgBox "Opening Connection3..." Set conPubs3 = wrkODBC.OpenConnection("Connection3", _ dbDriverCompleteRequired, True, _ "ODBC;DATABASE=pubs;DSN=Publishers;") ' Enumerate the Connections collection. For Each conLoop In wrkODBC.Connections Debug.Print "Connection properties for " & _ conLoop.Name & ":" With conLoop ' Print property values by explicitly calling each' Property object; the Connection object does not ' support a Properties collection. Debug.Print " Connect = " & .Connect ' Property actually returns a Database object. Debug.Print " Database[.Name] = " & _ .Database.Name Debug.Print " Name = " & .Name Debug.Print " QueryTimeout = " & .QueryTimeout Debug.Print " RecordsAffected = " & _ .RecordsAffected Debug.Print " StillExecuting = " & _.StillExecuting Debug.Print " Transactions = " & .Transactions Debug.Print " Updatable = " & .Updatable End With Next conLoop conPubs.Close conPubs2.Close conPubs3.Close wrkODBC.CloseEnd Sub
在MDB中增加字段比较方便.
& "ADD COLUMN fieldname fieldtype;"
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\test.mdb;Persist Security Info=False; Jet OLEDB:Database Password:Database Password=密码"
cn.Execute "ALTER TABLE 表 ADD COLUMN 新字段 字段类型"
cn.Close
Set cn = Nothing
Dim R As New ADODB.Recordset
Dim DBc As ADODB.Connection
Set DBc = New ADODB.Connection
DBc.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\main.mdb;Persist Security Info=False"
DBc.Open
R.Open "alter table (表名) add (字段) (类型)", DBc, adOpenStatic, adLockPessimistic, adCmdTextok
Dim conPubs As Connection
Dim conPubs2 As Connection
Dim conPubs3 As Connection
Dim conLoop As Connection ' Create ODBCDirect Workspace object.
Set wrkODBC = CreateWorkspace("NewODBCWorkspace", _
"admin", "", dbUseODBC) ' Open Connection object using supplied information in
' the connect string. If this information were
' insufficient, you could trap for an error rather than
' go to an ODBC Driver Manager dialog box.MsgBox "Opening Connection1..."
Set conPubs = wrkODBC.OpenConnection("Connection1", _
dbDriverNoPrompt, , _
"ODBC;DATABASE=pubs;UID=sa;PWD=;DSN=Publishers") ' Open read-only Connection object based on information
' you enter in the ODBC Driver Manager dialog box.
MsgBox "Opening Connection2..."
Set conPubs2 = wrkODBC.OpenConnection("Connection2", _
dbDriverPrompt, True, "ODBC;DSN=Publishers;") ' Open read-only Connection object by entering only the ' missing information in the ODBC Driver Manager dialog
' box.
MsgBox "Opening Connection3..."
Set conPubs3 = wrkODBC.OpenConnection("Connection3", _
dbDriverCompleteRequired, True, _
"ODBC;DATABASE=pubs;DSN=Publishers;") ' Enumerate the Connections collection.
For Each conLoop In wrkODBC.Connections
Debug.Print "Connection properties for " & _
conLoop.Name & ":" With conLoop
' Print property values by explicitly calling each' Property object; the Connection object does not
' support a Properties collection.
Debug.Print " Connect = " & .Connect
' Property actually returns a Database object.
Debug.Print " Database[.Name] = " & _
.Database.Name
Debug.Print " Name = " & .Name
Debug.Print " QueryTimeout = " & .QueryTimeout
Debug.Print " RecordsAffected = " & _
.RecordsAffected
Debug.Print " StillExecuting = " & _.StillExecuting
Debug.Print " Transactions = " & .Transactions
Debug.Print " Updatable = " & .Updatable
End With Next conLoop conPubs.Close
conPubs2.Close
conPubs3.Close
wrkODBC.CloseEnd Sub
ALTER TABLE 表 ADD COLUMN 新字段 字段类型
这个方法测试通过
看来我的SQL资料不够详细
那位介绍一本好的SQL资料另:找到用DAO打开MDB数据库的方法
OpenDatabase("C:\test.mdn", , ,"ms access;pwd=xxxx")
DAO可以用如下建字段
OpenDatabase.TableDefs("表名").CreateField("name",type,size)过几天结帐