有网友提到如何用动态修改列名?想把列名作为一个变量随时修改。
这里我们用DAO代码去处理。首先或者列名,再作修改。
如下图,获取现列名,填入新列名,按修改按钮。
修改列名:
Private Sub Command0_Click()
If IsNull(Me.新列名) Then
MsgBox "填入新列名"
Else
DoCmd.Close acTable, "表1", acSaveNo
Dim oldName As String
Dim newName As String
Dim db As DAO.Database
Dim td As DAO.TableDef
Set db = CurrentDb
Set td = db.TableDefs("表1")
oldName = td.Fields(2).Name
newName = Me.新列名
td.Fields(oldName).Name = newName
Me.现列名 = td.Fields(2).Name
Me.新列名 = Null
MsgBox "名称已改为" & td.Fields(2).Name
DoCmd.OpenTable "表1"
End If
End Sub
获取列名:
Private Sub Form_Load()
Dim db As DAO.Database
Dim td As DAO.TableDef
Set db = CurrentDb
Set td = db.TableDefs("表1")
Me.现列名 = td.Fields(2).Name
DoCmd.OpenTable "表1"
End Sub