2

我将 Access 2007 数据库配置为在导航窗格中使用“自定义”组。我以一种有意义的方式对我的所有表格进行了分组。但是,每当我更新链接表时,它都会丢失其分组。

我一直无法找到避免这种情况的方法。由于这似乎是不可避免的,我想简单地有一个宏,以编程方式将表添加回正确的组。我还没有找到任何关于如何做到这一点的例子。有什么建议么?

4

4 回答 4

5

旧线程,我知道,但我只需要处理这个问题,并认为我会分享我的解决方案:一个将对象移动到组的函数。

Public Function SetNavPaneGroup(strObjName, strGroupName)
    Dim strSql, idObj, idGrp, db
    Set db = CurrentDb
    idObj = DLookup("Id", "MSysNavPaneObjectIDs", "Name='" & strObjName & "'")
    idGrp = DLookup("Id", "MSysNavPaneGroups", "Name='" & strGroupName & "'")

    If DCount("*", "MSysNavPaneGroupToObjects", "GroupID = " & idGrp & " AND ObjectID = " & idObj) > 0 Then
        strSql = "UPDATE MSysNavPaneGroupToObjects SET GroupID = " & idGrp & ", Name='" & strObjName & "' WHERE ObjectID = " & idObj
        db.Execute strSql, dbFailOnError
    Else
        strSql = "INSERT INTO MSysNavPaneGroupToObjects ( GroupID, ObjectID, Name ) " & vbCrLf & _
        "VALUES (" & idGrp & "," & idObj & ",'" & strObjName & "');"
        db.Execute strSql, dbFailOnError
    End If
    RefreshDatabaseWindow
    Set db = Nothing
End Function

享受。

于 2018-04-17T18:03:57.990 回答
2

您应该可以通过编辑系统表 MSysNavPaneGroupToObjects 来做到这一点。您需要来自 MSysNavPaneObjectIDs 的表 ID 和来自 MSysNavPaneGroups 的组 ID(右键单击导航窗格以访问“导航选项”并选中“显示系统对象”)。然后您可以使用如下查询来更改表的组。

UPDATE MSysNavPaneGroupToObjects SET GroupID = 9 WHERE ObjectID = 268

因为您使用系统文件,所以在开始之前备份您的 .accdb 文件。

于 2012-11-01T22:58:45.863 回答
2

类似的问题,虽然 mjoshawa 的建议对我不太奏效,但我通过在 MsSystemNavPaneGroupToObjects 表中插入一个新行来让它工作。

INSERT INTO MSysNavPaneGroupToObjects ( GroupID, ObjectID, Name )
VALUES ( 55, 418, 'TableName' )

这在创建表后立即作为 UI 宏的一部分工作。

于 2013-06-27T11:21:47.080 回答
2

put_into_group(obj_name, group_name)通过在 中追加/更新记录将对象移动到指定组MSysNavPaneGroupToObjects

Function DLookupArr(Expr, Domain, Criteria)
' https://stackoverflow.com/questions/5864160
    Dim arr
    Set rs = CurrentDb.OpenRecordset("SELECT " & Expr & " FROM " & Domain & " WHERE " & Criteria)
    If rs.EOF Then
        DLookupArr = Array()
        Exit Function
    End If
    rs.MoveFirst
    Do While Not rs.EOF
        If IsEmpty(arr) Then ReDim arr(0) Else ReDim Preserve arr(UBound(arr) + 1)
        arr(UBound(arr)) = rs(Expr)
        rs.MoveNext
    Loop
    DLookupArr = arr
End Function

Function in_arr(v, arr) As Boolean
    For Each i In arr
        If i = v Then
            in_arr = True
            Exit Function
        End If
    Next i
End Function

Function get_object_group_id(obj_id) As Long
    obj_group_ids = DLookupArr("GroupID", "MSysNavPaneGroupToObjects", "ObjectID=" & obj_id) ' All object's groups
    groups_ids = DLookupArr("Id", "MSysNavPaneGroups", "GroupCategoryID=3") ' All groups IDs
    For Each i In obj_group_ids
        If in_arr(i, groups_ids) Then 'if it's a user-defined group then move from it to `group_name`
            get_object_group_id = i
            Exit For
        End If
    Next i
End Function

Function put_into_group(obj_name, group_name) As Boolean
    obj_id = DLookup("Id", "MSysObjects", "Name='" & obj_name & "'") ' Object-to-move ID
    If IsNull(obj_id) Then Exit Function
    obj_group_id = get_object_group_id(obj_id)
    group_id = DLookup("Id", "MSysNavPaneGroups", "Name='" & group_name & "' And GroupCategoryID=3") ' Target group ID
    If IsNull(group_id) Then Exit Function
    If obj_group_id = group_id Then ' already in group `group_name`
        put_into_group = True
        Exit Function
    End If
    If obj_group_id <> 0 Then ' if it's a user-defined group then move from it to `group_name`
        sql_s = "UPDATE MSysNavPaneGroupToObjects SET GroupID = " & group_id & _
                " WHERE ObjectID=" & obj_id & " And GroupID=" & obj_group_id
    Else ' if object was not in any user-defined groups then add new entry
        sql_s = "INSERT INTO MSysNavPaneGroupToObjects (GroupID, ObjectID, Name) " & _
                "VALUES (" & group_id & "," & obj_id & ",'" & obj_name & "')"
    End If
    CurrentDb.Execute sql_s, dbFailOnError
    RefreshDatabaseWindow
    put_into_group = True
End Function
于 2019-07-30T11:58:04.597 回答