|
ADO是接口,SQL是语言,前者要使用后者来操作数据库,你也可以不用ADO,而是用DAO,DAO也可以使用SQL语言操作数据库,下面两个是ADO、DAO删除字段的程序:
- Sub ADO删除字段()
- On Error GoTo hhh
- Dim mydata As String, myTable As String, SQL As String
- Dim cnn As ADODB.Connection
- Dim rs As ADODB.Recordset
- mydata = ThisWorkbook.Path & "\职工管理.mdb"
- myTable = "职工基本信息"
- If MsgBox("本操作将删除数据表字段!" & vbCrLf & "是否要删除字段?", _
- vbQuestion + vbYesNo, "删除字段") = vbNo Then Exit Sub
- Set cnn = New ADODB.Connection
- With cnn
- .Provider = "microsoft.jet.oledb.4.0"
- .Open mydata
- End With
- Dim myArray As Variant
- myArray = Array("工资", "备注")
- For i = 0 To UBound(myArray)
- SQL = "alter table " & myTable & " drop " & myArray(i)
- Set rs = cnn.Execute(SQL)
- Next i
- MsgBox "已经将数据表的两个<工资>和<备注>删除", vbInformation
- cnn.Close
- Set rs = Nothing
- Set cnn = Nothing
- Exit Sub
- hhh:
- MsgBox Err.Description, vbCritical
- End Sub
复制代码
- Sub DAO删除字段()
- On Error GoTo hhh
- Dim mydata As String, myTable As String, SQL As String
- Dim myDb As DAO.Database
- mydata = ThisWorkbook.Path & "\职工管理.mdb"
- myTable = "职工基本信息"
- If MsgBox("本操作删除数据表字段!" & vbCrLf & "是否要删除字段?", _
- vbQuestion + vbYesNo, "删除字段") = vbNo Then Exit Sub
- Set myDb = OpenDatabase(mydata)
- SQL = "alter table " & myTable & " drop 工资"
- myDb.Execute (SQL)
- SQL = "alter table " & myTable & " drop 备注"
- myDb.Execute (SQL)
- MsgBox "已经将数据表的两个字段<工资>和<备注>删除!", vbInformation
- myDb.Close
- Set myDb = Nothing
- Exit Sub
- hhh:
- MsgBox Err.Description
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|