Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim cmd As ADODB.Command
Sub main()
Set cn = New ADODB.Connection
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0Data Source=E:\demo.xlsExtended Properties=Excel 8.0Persist Security Info=False"
cn.Open
cn.Execute "ALTER TABLE [Sheet1$] ADD COLUMN NewField long"
Set rs = New ADODB.Recordset
rs.ActiveConnection = cn
rs.Open "SELECT * FROM [Sheet1$]"
Debug.Print rs.Fields(rs.Fields.Count - 1).Name
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub
追问:我按照你说的在form
load中添加了adoquestion.Recordset
alter
table
xz3
add
colum
answer
可是被告知缺少语句结束啊!
回答:
列关键字错了是column试下adoquestion.Recordset
alter
table
xz3
add
column
answer
你用那个rs.open
sql(修改语句),conn(连接数据库语句)
vb好像有这样的写法
追问:
不行,我还是试下修改语句,修改语句可以新增列吗?
回答:
alter
table
xz3
add
answer
nvarchar(50)改为这样子
追问:
缺少语句结束,我用了option
explicit
追问:
而且光标停在
xz3处
回答:
xz3是表名不?
列数是自动的,就是记录集的字段数'Ctrl+t找Microsoft DataGrid Control....
'菜单"工程"->"引用" 找 Microsoft ActiveX Data Object....
Dim cn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim SqlStr As String
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0Data Source=" &App.Path &"\db1.mdbMode=ReadWritePersist Security Info=FalseJet OLEDB:Database Password=111"
SqlStr = "Select * From 某个表"
rst.CursorLocation = adUseClient
rst.Open SqlStr, cn, adOpenDynamic, adLockOptimistic, adCmdText '打开记录集
Set DataGrid1.DataSource = rst '给DataGrid修改数据源
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)