How to auto set an object by a function

kenanyuk

New member
Local time
Tomorrow, 00:52
Joined
Jul 11, 2010
Messages
8
hi,
I'm trying to set a treeview object by function using variables coming from recordset. Variables are string type and there is no problem reading from recordset. While code is trying to run function which has to set treeview object; then run time error '91' happens and says "Object variable or With block variable not set"
here is my code and error atteched
any help will be very much appreciated
thanks
 

Attachments

  • treeview error.jpg
    treeview error.jpg
    101.8 KB · Views: 192
Hi there and welcome to the forum.
What kind of object is rsw? You have omitted the declaration of this object from your image.
From the comment at the top of the image it appears that you might be using a custom class that wraps the MSComCtlLib.Treeview object. If this is the case we might not be able to help unless you provide the code for that class also. People around here will be familiar with how to use the MSComCtlLib.Treeview class, which is a Microsoft product.
Post back with more info,
Cheers,
 
hi,
I'm trying to set a treeview object by function using variables coming from recordset. Variables are string type and there is no problem reading from recordset. While code is trying to run function which has to set treeview object; then run time error '91' happens and says "Object variable or With block variable not set"
here is my code and error atteched
any help will be very much appreciated
thanks

Welcome to AWF!

I have attached and example of an access database that uses a treeview control. It was developed by fell Access VVP Brent Spaulding. I learned a lot from this on how to use a Treeview control.

Also see: Thread on Treeview
 

Attachments

thank you for your kind interest
rsw is my recordset and you are that I have my custom classes and all working properly and having no problem with them.
the function objtree fails only
frmName and objName are variables as string which are coming from recordset and has no problem. it is working

private anysub()
.
.
objtree(frmName,objName).nodes.clear

'while trying to execute objtree function in this sentence it fails and run time error '91' happens and says "Object variable or With block variable not set"



Private Function objtree(frmName, objName) As TreeView

Set objtree = Forms(frmName).Controls(objName).Nodes.Clear

End Function


set objtree =...........I dont know how to write correct syntax or as a string variable frmName and objName causes this error. I also tried variant type but nothing changed.
there should be a proper way to set an object via function using variables coming from anywhere
that is what exactly my problem is
thanks a lot
 
thanks hitech coach
it is really helpfull
but ı dont have any problem in all treeview or other object codes

in my form user can add an additional object whatever possible like treeview. and I'm trying to set every object as per the information coming from recordeset via variables
thanks
 
As lagbolt has alredy requested, would you please post the VBA code for the Class module.
 
here is the code below

Code:
Option Compare Database
Option Explicit
Dim rsw As New RecordsetWrapper
Dim tvp As New TreeViewWrapper
'Dim objtree As TreeView
Dim objName As Variant                          'TreeView Object Name
Dim frmName As String                           'Form Name
Dim TV_rswOpen As String
Dim strText As String
Dim strKey As String
Dim rswCriteria As String
Dim TV_rswCriteria As String
Dim TVassigned_rswOpen As String
Dim TVassigned_rswCriteria As String
Dim TVassigned_rswCriteriaID As String
Dim TVassigned_rswCriteriaField As String
Dim strParentID As String                       'Recordset Parent ID field
Dim strID As String                             'Recordset Table ID field
Dim ExpandNode As Boolean                       'Treeview Object Expanded (True/False)
Public Function AddComment(ADD_ISSUESCOMMENT As String, ISSUEID As Long, CNOW As Date)
'ANLIK FAALİYET KISMINDA KAYIT EKLEME
    
    If rsw.OpenRecordset("IssuesComments") Then
        With rsw.Recordset
            .AddNew
            ![Issue ID] = ISSUEID
            ![History] = ADD_ISSUESCOMMENT
            ![Record Date] = CNOW
            If rsw.Update Then AddComment = True
        End With
    End If
    
End Function
Public Sub IssueList_TVPopulate()
'------------------------------------------------------------------------------
'Issue List FORMUNDA BULUNAN TÜM TREEVIEW NESNELERİNİN POPULATE İŞLEMİNİ YAPMAK
'AMACI İLE IssueList_TVVariables FONKSİYONUNA DEĞİŞKEN GÖNDERİR. BURADA FONKSİYONU SEÇTİREN
'TV_FunctionName.TV_Populate DİR
'------------------------------------------------------------------------------
If Not IsNull(ActiveIssue) Then IssueList_TVVariables
    
   
End Sub
Private Sub IssueList_TVVariables()
Dim objtree As TreeView
'------------------------------------------------------------------------------
'TreeViewWrapper CLASS İÇİNDEKİ FONKSİYONLARA Issue List FORMUNDAKİ TREEVIEW
'NESNELERİ İÇİN DEĞİŞKEN TÜRETİR.
'TV_FunctionName DEĞİŞKENİ FONKSİYON SEÇER
'------------------------------------------------------------------------------
rswCriteria = "[frmName] = 'Issue List' AND [objType] = 'TreeView'"
If rsw.OpenRecordset("Variables", rswCriteria) Then
    With rsw.Recordset
        If .BOF And .EOF Then Exit Sub
        Do While Not .EOF
            objName = ![objName]                                     'TreeView Object Name to be populated
            frmName = ![frmName]                                     'Form Name
            TV_rswOpen = ![TV_rswOpen]                               'Recordset Table Name to be populated
            TVassigned_rswOpen = "Issues" & TV_rswOpen               'Assigned Recordset Table Name
            TVassigned_rswCriteria = "[Issue ID] =" & ActiveIssue    'Assigned Recordset Criteria
            TVassigned_rswCriteriaID = "[" & TV_rswOpen & "]"        'Assigned Field To Be Updated in Recordset Table
            TVassigned_rswCriteriaField = "[" & TV_rswOpen & " ID]"
            strText = "[" & TV_rswOpen & " Name]"                    'Recordset Field To Be populated
            strKey = TV_rswOpen                                      'Unique Key for TreeView Object Records
            strParentID = "[Parent ID]"                              'Recordset Parent ID field
            strID = "[ID]"                                           'Recordset Table ID field
'objtree frmName, objName
objtree(frmName, objName).Nodes.Clear
    tvp.TV_FunctionSelect TV_FunctionName.TV_Populate, objtree(frmName, objName), TV_rswOpen, , strParentID, strID, strText, strKey, _
    , , ExpandNode, TVassigned_rswOpen, TVassigned_rswCriteria, TVassigned_rswCriteriaID, TVassigned_rswCriteriaField
    
        Loop
    End With
End If
End Sub
Private Function objtree(frmName, objName) As TreeView
Set objtree = Forms(frmName).Controls(objName).Nodes.Clear
'Set objtree = Forms(frmName).controls(objName).Object
'Set objtree = "[Forms]![" & frmName & "]![" & objName & "].object"
End Function
 
Last edited by a moderator:
here is the class doing other things
Code:
Option Compare Database
Option Explicit
Dim rsw As New RecordsetWrapper
Dim Li As Long
 
Public Function TV_FunctionSelect(TV_FunctionName As TV_FunctionName, _
                                 Optional objtree As TreeView, _
                              Optional TV_rswOpen As String, _
                          Optional TV_rswCriteria As String, _
                             Optional strParentID As String, _
                                   Optional strID As String, _
                                 Optional strText As String, _
                                  Optional strKey As String, _
                                 Optional frmName As String, _
                                 Optional objName As String, _
                              Optional ExpandNode As Boolean, _
                      Optional TVassigned_rswOpen As String, _
                  Optional TVassigned_rswCriteria As String, _
                Optional TVassigned_rswCriteriaID As String, _
             Optional TVassigned_rswCriteriaField As String, _
                              Optional IDCriteria As Long) As Boolean
        

Select Case TV_FunctionName
    Case 1      'TV_Populate
    
            If TVassigned_rswOpen <> "" Then TV_rswOpen = TVassigned_rswOpen
            If TVassigned_rswCriteria <> "" Then TV_rswCriteria = TVassigned_rswCriteria
            TV_Populate objtree, TV_rswOpen, strParentID, strID, strText, strKey, ExpandNode, TV_rswCriteria
    
    Case 2      'TV_CheckNodesChild
        
        'TV_CheckNodesChild nodChild, nodChecked
    
    Case 3      'TV_CheckNodesParent
        
        'TV_CheckNodesParent nodParent, nodChecked
    
    Case 4      'TV_CheckAssignedOnes
        
        TV_CheckAssignedOnes objtree, TVassigned_rswOpen, TVassigned_rswCriteria, TVassigned_rswCriteriaField, strKey
    Case 5      'TV_UnSelectAllNodes
        
        TV_UnSelectAllNodes objtree
    
    Case 6      'TV_FindSelectedNode
        
        TV_FindSelectedNode objtree
    
    Case 7      'TV_NodesClear
        
        TV_NodesClear objtree
        
    Case 8      'Assign
    
    TV_Assign objtree, TVassigned_rswOpen, TVassigned_rswCriteria, TVassigned_rswCriteriaField, strKey
    Case 9      'Assigned Add
        
        If TVassigned_rswOpen <> "" Then TV_rswOpen = TVassigned_rswOpen
        If TVassigned_rswCriteria <> "" Then TV_rswCriteria = TVassigned_rswCriteria
        TV_AssignedAdd TV_rswOpen, IDCriteria, TVassigned_rswCriteriaField
    Case 10     'Assigned Delete
        
        If TVassigned_rswOpen <> "" Then TV_rswOpen = TVassigned_rswOpen
        If TVassigned_rswCriteria <> "" Then TV_rswCriteria = TVassigned_rswCriteria
        TV_AssignedDelete TV_rswOpen, TV_rswCriteria
    
    
    
    
End Select
End Function
Public Function TV_Populate(objtree As TreeView, _
                         TV_rswOpen As String, _
                        strParentID As String, _
                              strID As String, _
                            strText As String, _
                             strKey As String, _
                Optional ExpandNode As Boolean, _
            Optional TV_rswCriteria As String = "1=1")
'ENUM 1 POPULATE
 
If rsw.OpenRecordset(TV_rswOpen, TV_rswCriteria) Then
    TV_PopulateBranch objtree:=objtree, rsw:=rsw, tvKey:=strKey, tvParentID:=strParentID, tvID:=strID, _
    tvText:=strText, tvExpandNodes:=ExpandNode
End If
TV_Populate = True
End Function
Sub TV_PopulateBranch(objtree As TreeView, _
                          rsw As RecordsetWrapper, _
                        tvKey As String, _
                 tvParentID As String, _
                       tvID As String, _
                     tvText As String, _
     Optional tvExpandNodes As Boolean, _
Optional tvVariantAdditional As Variant)
'OK
Dim rswBookmark As String
Dim strKey As String
Dim strText As String
Dim strCriteria As String
Dim nodParent As Node
Dim nodCurrent As Node
If IsMissing(tvVariantAdditional) Then  ' Root Branch.
    strCriteria = tvParentID & " Is Null"
Else  ' Search for records pointing to parent.
    strCriteria = BuildCriteria(tvParentID, rsw.Recordset.Fields(tvParentID).Type, "=" & tvVariantAdditional)
    Set nodParent = objtree.Nodes(tvKey & tvVariantAdditional)
    nodParent.Expanded = tvExpandNodes
End If
rsw.Recordset.FindFirst strCriteria
Do Until rsw.Recordset.NoMatch
    strText = rsw.Recordset(tvText)
    strKey = tvKey & rsw.Recordset(tvID)
        If Not IsMissing(tvVariantAdditional) Then  'add new node to the parent
            Set nodCurrent = objtree.Nodes.Add(nodParent, tvwChild, strKey, strText)
        Else    ' Add new node to the root.
            Set nodCurrent = objtree.Nodes.Add(, , strKey, strText)
        End If
    nodCurrent.Expanded = tvExpandNodes
    rswBookmark = rsw.Recordset.Bookmark
    TV_PopulateBranch objtree, rsw, tvKey, tvParentID, tvID, tvText, tvExpandNodes, rsw.Recordset(tvID)
    rsw.Recordset.Bookmark = rswBookmark          ' Return to last place and continue search.
    rsw.Recordset.FindNext strCriteria
Loop
exitTV_PopulateBranch:
Exit Sub
End Sub
Public Function TV_CheckNodesChild(ByRef nodChild As Node, ByVal nodChecked As Boolean) As Boolean
'YOU MAY CANCEL UP TO YOUR POPULATE CONDITION TOGETHER CheckNodesToParent
'OK
Dim oNode As Node
If nodChecked Then Exit Function
Set oNode = nodChild.Child
    Do While Not oNode Is Nothing
        oNode.Checked = nodChecked
            If TV_CheckNodesChild(oNode, nodChecked) Then
            End If
        Set oNode = oNode.Next
    Loop
TV_CheckNodesChild = True
    
End Function
Public Function TV_CheckNodesParent(ByRef nodParent As Node, ByVal nodChecked As Boolean) As Boolean
'OK
If nodParent.Checked = False Then Exit Function
    Do While Not nodParent.Parent Is Nothing
        nodParent.Parent.Checked = nodChecked
        Set nodParent = nodParent.Parent
    Loop
TV_CheckNodesParent = True
End Function
Public Function TV_CheckAssignedOnes(objtree As TreeView, _
                          TVassigned_rswOpen As String, _
                      TVassigned_rswCriteria As String, _
                 TVassigned_rswCriteriaField As String, _
                                      strKey As String) As Boolean
'OK
Dim Li As Long
Dim IDCriteria As Integer
Dim TV_IDCriteria As Long
TV_IDCriteria = Len(strKey)
 
If rsw.OpenRecordset(TVassigned_rswOpen, TVassigned_rswCriteria) Then
    If rsw.Recordset.BOF And rsw.Recordset.EOF Then Exit Function
        If rsw.MoveFirst Then
            Do While Not rsw.Recordset.EOF
            rsw.Bookmark
                For Li = 1 To TV_ListCount(objtree)
                  IDCriteria = Mid(objtree.Nodes.Item(Li).Key, TV_IDCriteria + 1, Len(objtree.Nodes.Item(Li).Key))
                       If IDCriteria = rsw.Recordset.Fields(TVassigned_rswCriteriaField).Value Then
                          objtree.Nodes.Item(Li).Checked = True
                          Exit For
                       Else
                       End If
                Next
            rsw.MoveNext
            Loop
        End If
    End If
TV_CheckAssignedOnes = True
End Function
Public Function TV_UnSelectAllNodes(objtree As TreeView) As Boolean
'OK
Dim Li As Long
    For Li = 1 To TV_ListCount(objtree)
            If objtree.Nodes.Item(Li).Selected = True Then
               objtree.Nodes.Item(Li).Selected = False
            End If
    Next
TV_UnSelectAllNodes = True
End Function
Public Function TV_FindSelectedNode(objtree As TreeView) As String
Dim Li As Long
    For Li = 1 To TV_ListCount(objtree)
        If objtree.Nodes.Item(Li).Selected = True Then
          TV_FindSelectedNode = objtree.Nodes.Item(Li).Key
        Exit For
        Else
        End If
    Next
End Function
Public Function TV_Assign(objtree As TreeView, _
               TVassigned_rswOpen As String, _
           TVassigned_rswCriteria As String, _
      TVassigned_rswCriteriaField As String, _
                           strKey As String) As Boolean

Dim IDCriteria As Long
If rsw.OpenRecordset(TVassigned_rswOpen, TVassigned_rswCriteria) Then
    With rsw.Recordset
        If .BOF And .EOF Then GoTo UpdateAll
            Do While Not rsw.Recordset.EOF
                If TV_AssignedDelete(TVassigned_rswOpen, TVassigned_rswCriteria) Then rsw.MoveNext
            Loop
    End With
End If
UpdateAll:
        
        For Li = 1 To TV_ListCount(objtree)
            IDCriteria = Mid(objtree.Nodes.Item(Li).Key, Len(strKey) + 1, Len(objtree.Nodes.Item(Li).Key))
            If objtree.Nodes.Item(Li).Checked Then
            If TV_AssignedAdd(TVassigned_rswOpen, IDCriteria, TVassigned_rswCriteriaField) Then
            End If
            Else
End If
        Next
End Function
Public Function TV_AssignedAdd(TV_rswOpen As String, IDCriteria As Long, TVassigned_rswCriteriaField As String) As Boolean
'![Issue ID] = ActiveIssue      İLGİLİ YAZILIMDA HER KULLANIMDA YENİDEN TANIMLANMALI
If rsw.OpenRecordset(TV_rswOpen) Then
    With rsw.Recordset
        .AddNew
        ![Issue ID] = ActiveIssue         'İLGİLİ YAZILIMDA HER KULLANIMDA YENİDEN TANIMLANMALI
        .Fields(TVassigned_rswCriteriaField).Value = IDCriteria
     If rsw.Update Then TV_AssignedAdd = True
    End With
End If
End Function
Public Function TV_AssignedDelete(TV_rswOpen As String, TV_rswCriteria As String) As Boolean
If rsw.OpenRecordset(TV_rswOpen, TV_rswCriteria) Then
    With rsw.Recordset
        .Delete
    End With
TV_AssignedDelete = True
End If
End Function
 
Public Function TV_NodesClear(objtree As TreeView)
objtree.Nodes.Clear
End Function
Public Function TV_ListCount(objtree As TreeView) As Long
TV_ListCount = objtree.Nodes.Count
End Function
 
Last edited by a moderator:
For heaven's sake, when you post code like that use CODE TAGS to make it readable! (I went in and modified your posts to add them in)


See here how easy it is to add them:


codetag001.png
 
I don't believe the Clear method of the MSComCtlLib.Nodes object returns an object. This code, therefore, will fail for that reason...
Code:
Private Function objtree(frmName, objName) As TreeView
[COLOR="Green"]  'the Clear method simply clears the Nodes collection[/COLOR]
  Set objtree = Forms(frmName).Controls(objName).Nodes.Clear
End Function

And did you write this TreeviewWrapper class?
 
Boblarson I'm so sorry its first time I'm in a forum like this I will be carefull next time
but I also send the class and module as ziped

loagbolt yes I myself write this class, if this is your question

thanks a lot
 
I don't believe the Clear method of the MSComCtlLib.Nodes object returns an object. This code, therefore, will fail for that reason...
Code:
Private Function objtree(frmName, objName) As TreeView
[COLOR=green] 'the Clear method simply clears the Nodes collection[/COLOR]
  Set objtree = Forms(frmName).Controls(objName).Nodes.Clear
End Function

And did you write this TreeviewWrapper class?


ok
Set objtree = Forms(frmName).Controls(objName)
what I need exactly
 

Users who are viewing this thread

Back
Top Bottom