我有一个重命名表列名的方法。
Public Function Rename_Column(tablename As String, oldcolumn As String, newcolumn As String)
Dim dbs As Database, tdf As TableDef
Set dbs = CurrentDb
For Each tdf In dbs.TableDefs
If tdf.Name = tablename Then
For Each fld In tdf.Fields
If fld.Name = oldcolumn Then
fld.Name = newcolumn
End If
Next
End If
Next
dbs.Close
End Function
我正在其他程序中调用此函数
Public Sub querylistboxitems()
Dim strTableName As String
strTableName = "Table1"
Call Rename_Column(strTableName, "old", "New")
End Sub
但它给出了一个错误“Byref参数类型不匹配”
答案 0 :(得分:21)
还有其他棘手的情况可能会出现此问题。例如,在一行上声明两个(或更多)变量时:
Dim firstSubMenu, secondSubMenu As CommandBarPopup
现在firstSubMenu的类型为Variant,而secondSubMenu的类型为CommandBarPopup。这可能不是您的意图,也可能是上述错误的原因,通过引用它来引用一个需要CommandBarPopup类型的参数的函数。 在这种情况下,解决方案是将它们声明为两行:
Dim firstSubMenu As CommandBarPopup
Dim secondSubMenu As CommandBarPopup
或者,如果你真的想把它挤在一行上,可以这样做:
Dim firstSubMenu As CommandBarPopup, secondSubMenu As CommandBarPopup
请注意,您必须为每个变量重复“As”类型声明。
答案 1 :(得分:2)
因为你没有定义“fld”而失败了。以下内容比通过所有表/字段循环更直接:
Public Function Rename_Column(tablename As String, oldcolumn As String, newcolumn As String)
Dim dbs As Database
Dim tdf As TableDef
Dim fld As Field
Set dbs = CurrentDb
Set tdf = dbs.TableDefs(tablename)
Set fld = tdf.Fields(oldcolumn)
Set fld = Nothing
Set tdf = Nothing
dbs.Close
Set dbs = Nothing
End Function