0

我最近将 2007 年的 Access 文件转换(导出/导入)到 2010 年。除了一种形式外,一切正常。我不断收到错误:

编译错误:未定义用户定义类型

我尝试将“Microsoft ActiveX Data Objects 2.8”添加到我的引用中,但问题仍然存在。 Sub ClearTreeView(tvwTree As TreeView)是 Access 突出显示的问题。

Option Compare Database
Option Explicit

' Clears all nodes on a treeview control
Sub ClearTreeView(tvwTree As TreeView) 'what Access highlighted as the issue
    On Error GoTo EH
    tvwTree.Nodes.Clear
    Exit Sub
EH:
    MsgBox "Error " & Err.Number & ": " & Err.Description
End Sub

' Calls functions to clear and populate a treeview control
' Parameters:
'       strForm         Name of the form
'       strTV           TreeView control name
'       strSourceName   Name of the table or query containing the data used to populate the treeview
'       strChildField   ID field for the child records
'       strParentField  Parent ID Field
'       strTextField    Field containing text that will be used as node labels
'
Sub FillTreeView(tvwTree As Object, strSourceName As String, strChildField As String, strParentField As String, strTextField As String)
    Dim strSQL As String
    Dim rs As DAO.Recordset
    
    On Error GoTo EH
    
    ' Open the recordset using table and fields specified in Sub parameters
    strSQL = "SELECT " & strChildField & ", " & strParentField & ", " & strTextField & " FROM " & strSourceName
    Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
    
    ' Clear any existing data out of the treeview
    ClearTreeView tvwTree
    
    ' Call recursive function to fill in treeview
    AddTreeData tvwTree, rs, strChildField, strParentField, strTextField
    
    ' Close the recordset
    rs.Close
    Set rs = Nothing
    Exit Sub
EH:
    MsgBox "Error " & Err.Number & ": " & Err.Description
End Sub


' Recursive function to populate a treeview control
' Parameters:
'       strFormName     Name of the form
'       strTreeViewName TreeView control name
'       rs              Recordset containing the data used to populate the treeview
'       strChildField   ID field for the child records
'       strParentField  Parent ID Field
'       strTextField    Field containing text that will be used as node labels
'       varParentID     Optional parameter that only gets passed for recursive calls to this function.  Specifies the ID of the current record to be used as a
'                       ParentID when searching the recordset for "grand-children", etc.
Sub AddTreeData(objTV As TreeView, rs As DAO.Recordset, strChildField As String, strParentField As String, strTextField As String, Optional varParentID As Variant)
    Dim nodChild As Node
    Dim nodParent As Node
    Dim strLabel As String
    Dim strNodeID As String
    Dim strCriteria As String
    Dim strBookmark As String
    
    On Error GoTo EH
    
    ' Test for a circular reference
    If rs(strChildField) = rs(strParentField) Then GoTo EH_CircularReference
    
    ' If the optional parameter is missing, then this is the first(non-recursive) call to this function.
    ' Set the critieria to look for a parent id of 0.
    If IsMissing(varParentID) Then
        strCriteria = strParentField & " = 0 "
    Else
        ' Otherwise, extract the childID portion of the node ID, which was passed as an optional parameter.
        strCriteria = strParentField & " = " & Mid(varParentID, InStr(1, varParentID, "C") + 1)
        ' Define the parent node
        Set nodParent = objTV.Nodes("node" & varParentID)
    End If
    
    ' Look for records having the specified "parent"
    rs.FindFirst strCriteria
    Do Until rs.NoMatch
        ' Read node caption from the text field
        strLabel = rs(strTextField)
        ' Create a new node ID in the format ParentID &"C" & ChildID (eg: 4C12)
        strNodeID = "node" & rs(strParentField) & "C" & rs(strChildField)
        
        ' If optional parameter is missing (first call to this function)...
        If Not IsMissing(varParentID) Then
            'add new node to the next higher node for this record
            Set nodChild = objTV.Nodes.Add(nodParent, tvwChild, strNodeID, strLabel)
        Else
            ' Otherwise, add new node to the top level of the tree
            Set nodChild = objTV.Nodes.Add(, , strNodeID, strLabel)
        End If
        
        ' Bookmark our place in the recordset so that we can resume the search from the same point after the recursive call to this function.
        strBookmark = rs.Bookmark
        
        ' call this function recursively for "children"
        AddTreeData objTV, rs, strChildField, strParentField, strTextField, rs(strParentField) & "C" & rs(strChildField)
        
        ' Return to bookmared place in the recordset
        rs.Bookmark = strBookmark
        
        ' Find the next record having the same parentID
        rs.FindNext strCriteria
    Loop
    
    Exit Sub
    
EH_CircularReference:
    MsgBox "Exiting because of a circular reference in which a child record was determined to be it's own parent."
    Exit Sub

    
EH:
    MsgBox "Error " & Err.Number & ": " & Err.Description
End Sub
4

1 回答 1

3

我在 Excel 中的一个用户表单上收到了同样的错误!

如果您的新 2010 Office 包是 64 位,则 2007 版本中使用的以前的 32 位 ActiveX 控件将不兼容。

请参阅此链接: http: //msdn.microsoft.com/en-us/library/ee691831 (office.14).aspx#odc_office2010_Compatibility32bit64bit_ActiveXControlCOMAddinCompatibility

如果您尝试在设计视图中打开表单并且表单上不存在 TreeView 控件,那么这很可能是问题所在。

要查看您是否安装了 64 位版本:打开 Access -> 文件 -> 帮助 -> 并在右侧的“关于 Microsoft Access”下查看 - 它应该在括号中显示 32 位或 64 位

正如链接所解释的,您将不得不替换不兼容的功能 - 因此您将不得不使用新控件。

可能的解决方案:

如果您在设计视图中打开表单,并选择功能区选项卡中控件组上的向下箭头 - 应该有一个名为“ActiveX 控件”的选项(您必须在设计视图中才能选择它)

在此处搜索“CTreeView”控件并尝试使用它而不是传统的 Microsoft TreeView 控件(不应在 64 位访问中列出)。

如果您安装了 32 位办公室,那么我无法找出您发布的内容的问题 - 但我怀疑是这种情况。

此致,

于 2013-10-18T16:25:10.720 回答