如何在 MS Access 中使用 VBA 更改链接共享点列表的地址
How do I use VBA in MS Access to change the address of linked sharepoint lists
在工作中,我们使用 MS Access 数据库使用共享点列表作为链接表,因为我们喜欢这种稳定性。我们的共享点网站的地址将会改变。我想 运行 a VBA sub 将表从旧地址更改为新地址。到目前为止,这是我所拥有的,但是有两个问题:
- 它不会在不询问的情况下自动检测哪些表是共享点列表
- 有些表是'participating in one or more relationships'删除时报错
如何让它变得更好?
Sub ChangeSPTables()
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim MBr As VbMsgBoxResult
Dim N As String
Set db = CurrentDb
For Each tdf In db.TableDefs
If Not (tdf.Name Like "MSys*" Or tdf.Name Like "~*") Then
N = tdf.Name
MBr = MsgBox("Delete and relink " & N & "?", vbYesNoCancel)
If MBr = vbYes Then
DoCmd.DeleteObject acTable, N
DoCmd.TransferSharePointList acLinkSharePointList, "https://redacted", N
End If
End If
Next
Set tdf = Nothing
Set db = Nothing
End Sub
相关的table无法删除,但可以删除。添加 table 也会添加相关的 table,所以我在重复添加时遇到了麻烦。我存储了所有要先删除的名称,然后再重新添加(如果它们尚不存在)。共享点地址是连接字符串的一部分,因此我能够筛选出我想要的 tables。
编辑:使用 DoCmd.TransferSharePointList 会导致包含查找列的列表以只读方式进入。对于这样的列表,请在 运行.
之后手动删除并添加它们
Sub ChangeSPTables()
'Step through all table definitions and check if the old sharepoint address appears in the connection string
'If so, stores the name of the table, deletes all such tables, and re-adds them from the new sharepoint address
'The list is used because sometimes other tables are brought in automatically as relationships and I don't
'want extra copies or instances where the table has a 1 on the end of the name
'Will not work on things that have been renamed since they were brought in from sharepoint
'You should probably only run this on a copy of the database for saftey's sake
Dim db As dao.Database
Dim tdf As dao.TableDef
Dim MBr As VbMsgBoxResult
Dim i As Long: i = 1
Dim ListNames(1 To 20) As String 'Assumed to be 20 or less
'The old and new sharepoint addresses
Dim OldSP As String
Dim NewSP As String
OldSP = "https://old"
NewSP = "https://new"
Set db = CurrentDb
For Each tdf In db.TableDefs
If Not (tdf.Name Like "MSys*" Or tdf.Name Like "~*") And InStr(1, tdf.Connect, OldSP) > 0 Then
ListNames(i) = tdf.Name
i = i + 1
Debug.Print tdf.Name 'For review of which tables were redone
db.Execute "drop table [" & tdf.Name & "]", dbFailOnError 'drop table avoids problems with relationships between tables
End If
Next
i = 1
Do Until ListNames(i) = ""
If IsNull(DLookup("Name", "MSysObjects", "Name='" & ListNames(i) & "'")) Then 'Some things get re-added by relationship
DoCmd.TransferSharePointList acLinkSharePointList, NewSP, ListNames(i)
End If
i = i + 1
Loop
'Hide the sharepoint tables
For Each tdf In db.TableDefs
If Not (tdf.Name Like "MSys*" Or tdf.Name Like "~*") And InStr(1, tdf.Connect, NewSP) > 0 Then
Application.SetHiddenAttribute acTable, tdf.Name, True
End If
Next
Set tdf = Nothing
Set db = Nothing
End Sub
在工作中,我们使用 MS Access 数据库使用共享点列表作为链接表,因为我们喜欢这种稳定性。我们的共享点网站的地址将会改变。我想 运行 a VBA sub 将表从旧地址更改为新地址。到目前为止,这是我所拥有的,但是有两个问题:
- 它不会在不询问的情况下自动检测哪些表是共享点列表
- 有些表是'participating in one or more relationships'删除时报错
如何让它变得更好?
Sub ChangeSPTables()
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim MBr As VbMsgBoxResult
Dim N As String
Set db = CurrentDb
For Each tdf In db.TableDefs
If Not (tdf.Name Like "MSys*" Or tdf.Name Like "~*") Then
N = tdf.Name
MBr = MsgBox("Delete and relink " & N & "?", vbYesNoCancel)
If MBr = vbYes Then
DoCmd.DeleteObject acTable, N
DoCmd.TransferSharePointList acLinkSharePointList, "https://redacted", N
End If
End If
Next
Set tdf = Nothing
Set db = Nothing
End Sub
相关的table无法删除,但可以删除。添加 table 也会添加相关的 table,所以我在重复添加时遇到了麻烦。我存储了所有要先删除的名称,然后再重新添加(如果它们尚不存在)。共享点地址是连接字符串的一部分,因此我能够筛选出我想要的 tables。
编辑:使用 DoCmd.TransferSharePointList 会导致包含查找列的列表以只读方式进入。对于这样的列表,请在 运行.
之后手动删除并添加它们Sub ChangeSPTables()
'Step through all table definitions and check if the old sharepoint address appears in the connection string
'If so, stores the name of the table, deletes all such tables, and re-adds them from the new sharepoint address
'The list is used because sometimes other tables are brought in automatically as relationships and I don't
'want extra copies or instances where the table has a 1 on the end of the name
'Will not work on things that have been renamed since they were brought in from sharepoint
'You should probably only run this on a copy of the database for saftey's sake
Dim db As dao.Database
Dim tdf As dao.TableDef
Dim MBr As VbMsgBoxResult
Dim i As Long: i = 1
Dim ListNames(1 To 20) As String 'Assumed to be 20 or less
'The old and new sharepoint addresses
Dim OldSP As String
Dim NewSP As String
OldSP = "https://old"
NewSP = "https://new"
Set db = CurrentDb
For Each tdf In db.TableDefs
If Not (tdf.Name Like "MSys*" Or tdf.Name Like "~*") And InStr(1, tdf.Connect, OldSP) > 0 Then
ListNames(i) = tdf.Name
i = i + 1
Debug.Print tdf.Name 'For review of which tables were redone
db.Execute "drop table [" & tdf.Name & "]", dbFailOnError 'drop table avoids problems with relationships between tables
End If
Next
i = 1
Do Until ListNames(i) = ""
If IsNull(DLookup("Name", "MSysObjects", "Name='" & ListNames(i) & "'")) Then 'Some things get re-added by relationship
DoCmd.TransferSharePointList acLinkSharePointList, NewSP, ListNames(i)
End If
i = i + 1
Loop
'Hide the sharepoint tables
For Each tdf In db.TableDefs
If Not (tdf.Name Like "MSys*" Or tdf.Name Like "~*") And InStr(1, tdf.Connect, NewSP) > 0 Then
Application.SetHiddenAttribute acTable, tdf.Name, True
End If
Next
Set tdf = Nothing
Set db = Nothing
End Sub