Results 1 to 27 of 27

Thread: [RESOLVED] Loading from DB into array / treeview

  1. #1

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Resolved [RESOLVED] Loading from DB into array / treeview

    I am completely new to Access and Databases (and limited ability with VB6 as well) but don't let that put you off.

    I am using VB6 and Access 2002

    I have downloaded the tutorial suggested in the FAQ's and run it in a practise project and got it all to work as should be.

    I have now tried to put this code into my project, which is making a Code Library for myself.

    The first thing I am trying to accomplish is to load the list in the attached image under the title tvwNodes into an array, to then use to name the Nodes in my treeview (tvwCode)

    I have the following code where I get an error after all the 6 records have been added to the array
    "Run-time error 94"
    "Invalid use of Null"
    My code is:

    vb Code:
    1. 'declare it as a ADO-DB
    2. Set cn = New ADODB.Connection
    3.  
    4. 'set the connection
    5. cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    6. "Data Source=C:\Documents and Settings\Andrew\My Documents\VB6\New Code Library\DBCL.mdb"
    7. cn.Open
    8. Set rs = New ADODB.Recordset
    9. rs.Open "tbl_Nodes", cn, adOpenKeyset, adLockPessimistic, adCmdTable
    10.  
    11. 'move to first record
    12. rs.MoveFirst
    13. 'loop to add items to array
    14. ReDim tvw_Nodes(0)
    15. Do Until rs.EOF = True
    16.     tvw_Nodes(UBound(tvw_Nodes)) = rs.Fields("tvwNodes")
    17.     Debug.Print tvw_Nodes(UBound(tvw_Nodes))
    18.     rs.MoveNext
    19.     ReDim Preserve tvw_Nodes(UBound(tvw_Nodes) = 1)
    20. Loop

    Could this be because the list in the second field is longer?
    I wouldn't have thought so, but as I said in my first line of this thread ...
    Quote Originally Posted by Me
    I am completely new to Access and Databases
    Attached Files Attached Files
    Last edited by aikidokid; Mar 10th, 2007 at 09:43 AM. Reason: better describes what is answered below
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Loading from DB into array

    Quote Originally Posted by aikidokid
    Could this be because the list in the second field is longer?
    Yes it is.

    For several rows tvwNodes is Null (not just an empty string, but no value at all), which causes problems if not used properly.

    The simplest way to fix this is to append an empty string to the value (other methods are discussed in the FAQ article about it), eg:
    Code:
    tvw_Nodes(UBound(tvw_Nodes)) = rs.Fields("tvwNodes") & ""
    ..however in this case (based on my interpretation of your data) you don't need that, you need to do something else instead.

    What you need to do is store the value for tvwNodes in every row, as currently VBA Excel etc do not have "parent" nodes.

    You seem to have made a common error, which is to assume that the order or records actually means something (or will be returned in that order next time), which is not the case - a table is a group (or Set) of records, not an ordered list. If the database decides to, it can give you those records in a completely random order each time.

  3. #3

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Loading from DB into array

    Quote Originally Posted by si_the_geek
    Yes it is.

    For several rows tvwNodes is Null (not just an empty string, but no value at all), which causes problems if not used properly.
    Ok, this I follow, thanks.

    Quote Originally Posted by si_the_geek
    The simplest way to fix this is to append an empty string to the value (other methods are discussed in the FAQ article about it), eg:
    [code]tvw_Nodes(UBound(tvw_Nodes)) = rs.Fields("tvwNodes") & ""
    Again, I think I follow this, but just to understand what you have said here,
    when loading the array elements from the database, on each loop add an empty string to the row contents.
    Surley this would increase the size of the array elements? Or do you intend this, and then when using the array use something like If Not = "" then ....

    Quote Originally Posted by si_the_geek
    however in this case (based on my interpretation of your data) you don't need that, you need to do something else instead.

    What you need to do is store the value for tvwNodes in every row, as currently VBA Excel etc do not have "parent" nodes.
    I have attached a picture in the document below to show what I am tring to achieve. This has been coded, but with a fixed number of nodes. I am trying to make it dynamic, so the user (me) can add a new node at any time.

    Quote Originally Posted by si_the_geek
    You seem to have made a common error, which is to assume that the order or records actually means something (or will be returned in that order next time), which is not the case - a table is a group (or Set) of records, not an ordered list. If the database decides to, it can give you those records in a completely random order each time.
    An interesting point, which obviously I didnt know. So If I wanted the nodes to be loaded A-Z then I would have to sort the array elements first would I?
    Attached Files Attached Files
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Loading from DB into array

    Quote Originally Posted by aikidokid
    Again, I think I follow this, but just to understand what you have said here, when loading the array elements from the database, on each loop add an empty string to the row contents.
    Surley this would increase the size of the array elements? Or do you intend this, and then when using the array use something like If Not = "" then ....
    It doesn't increase the size, as you what you are appending doesn't have any length - it's just a cunning trick to convert a Null into an empty string ("") which VB is happy with, while not having any effect on non-Null values.

    You could use the IsNull function instead as also shown in the article in the Database FAQs, but this method is simpler and quicker.

    So If I wanted the nodes to be loaded A-Z then I would have to sort the array elements first would I?
    You could, but it is easier (and faster) to ask the database to sort it for you.. to do that, you need to use an SQL statement instead of just a table name. To do that, replace line 9 of your code above with this:
    Code:
    Dim strSQL as String
      strSQL = "SELECT * FROM tbl_Nodes ORDER BY tvwNodes, tvwChildNodes"
      rs.Open strSQL, cn, adOpenKeyset, adLockPessimistic, adCmdText
    (while the strSQL variable isn't strictly necessary, it is useful for finding bugs!)

  5. #5

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Loading from DB into array

    Thanks again si

    Quote Originally Posted by si_the_geek
    It doesn't increase the size, as you what you are appending doesn't have any length - it's just a cunning trick to convert a Null into an empty string ("") which VB is happy with, while not having any effect on non-Null values.

    You could use the IsNull function instead as also shown in the article in the Database FAQs, but this method is simpler and quicker.
    I have changed the two parts of the code you suggested, adding the empty string to the end and the sql statement to sort the array.

    When I now do this, I get 13 empty elements in the array before I get to the actual elements I am after.

    have I missed something, or done it incorrectly?

    vb Code:
    1. 'declare it as a ADO-DB
    2. Set cn = New ADODB.Connection
    3.  
    4. 'set the connection
    5. cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    6. "Data Source=C:\Documents and Settings\Andrew\My Documents\VB6\New Code Library\DBCL.mdb"
    7. cn.Open
    8. Set rs = New ADODB.Recordset
    9. Dim strSQL As String
    10. strSQL = "SELECT * FROM tbl_Nodes ORDER BY tvwNodes, tvwChildNodes"
    11. rs.Open strSQL, cn, adOpenKeyset, adLockPessimistic, adCmdText
    12. ''''rs.Open "tbl_Nodes", cn, adOpenKeyset, adLockPessimistic, adCmdTable
    13.  
    14. 'move to first record
    15. rs.MoveFirst
    16. 'loop to add items to array
    17. ReDim tvw_Nodes(0)
    18. Do Until rs.EOF = True
    19.     tvw_Nodes(UBound(tvw_Nodes)) = rs.Fields("tvwNodes") & ""
    20.     Debug.Print tvw_Nodes(UBound(tvw_Nodes))
    21.     rs.MoveNext
    22.     ReDim Preserve tvw_Nodes(UBound(tvw_Nodes) = 1)
    23. Loop
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Loading from DB into array

    Have you added values to all rows the tvwNodes field?

  7. #7

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Loading from DB into array

    Quote Originally Posted by si_the_geek
    Have you added values to all rows the tvwNodes field?
    No, the entries in the tvwNodes field are as before, where the second field is longer.

    Before, did you mean to add "" to the database?
    I thought you meant to add it to the array - I think - getting confused now
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Loading from DB into array

    You should have values in every row of tvwNodes, as explained in the second half of post 2 - otherwise the child nodes do not have a parent.

    Every row should contain all of the information that it needs to be 'complete' (or a method of linking to that information).
    Quote Originally Posted by aikidokid
    Before, did you mean to add "" to the database?
    Nope, just add it to the value when you are reading it from the recordset - your code is fine, and will stop the original error from occurring again.

  9. #9

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Loading from DB into array

    So, should my database look something like this

    Field 1 Field 2 Field 3
    Parent tvwChild tvwgrandchild
    Parent tvwchild tvwgrandchild
    Parent tvwchild
    Parent tvwchild tvwgrandchild


    So, in each row is stored the whole lot for the one main node, ie,

    Source Code > VB Code > Functions
    Source Code > VB Code > Modules
    Source Code > VBA Code > Access
    Source Code > Notes
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Loading from DB into array

    That's right.

    You could go for a different database design to stop the duplication of data (eg: having one table for nodes, one for child nodes, and one for grand-child nodes)), but it would add more complexity to the database work - so you may want to avoid that!

  11. #11

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Loading from DB into array

    Thanks alot si.

    So if I change my database to this

    Source Code > VB Code > Functions

    and then would I do something like this

    vb Code:
    1. tvw_Nodes(UBound(tvw_Nodes)) = rs.Fields("tvwNodes") & rs.Fields("tvwChildNodes") & ""

    Sorry if this is obvious but I did say it was all new to me
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  12. #12
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Loading from DB into array

    That's not the way I'm afraid, you need to add each child node to the listview separately. Is there a reason for using the tvw_Nodes array, rather than adding items directly to the listview?


    Probably the easiest way is to store the "previous" text of tvwNodes and tvwChildNodes in strings, and store the added nodes as separate variables. Then within the loop:

    If the tvwNodes value for the current record is different, and a new "parent" node which you store to your variable (and reset the value of the tvwChildNodes "previous" string).

    Then the same for the tvwChildNodes field (except you need to add the node to the current parent), and finally (if it isn't blank) add the tvwgrandchild node to the child node.

  13. #13

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Loading from DB into array

    Quote Originally Posted by si_the_geek
    Is there a reason for using the tvw_Nodes array, rather than adding items directly to the listview?
    No, not really. It's just because I don't know any other way to do it, so I assumed it was this way.
    From what you are saying, it would be easier to add directly to the listview.

    I don't usually ask this, but could you rustle up a quick demo sometime please, as I have been struggling with this for quite some time.

    This would be from a database set out as above:

    Source Code > VB Code > Functions
    Source Code > VB Code > Modules
    Source Code > VBA Code > Access
    Source Code > Notes
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  14. #14
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Loading from DB into array

    Ok.. this is a very "quick and dirty" demo tho, so there may be mistakes:
    vb Code:
    1. 'declare & set up the connection
    2. Dim cn as ADODB.Connection
    3.   Set cn = New ADODB.Connection
    4.   cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    5. "Data Source=C:\Documents and Settings\Andrew\My Documents\VB6\New Code Library\DBCL.mdb"
    6.   cn.Open
    7.  
    8. 'declare and set up the recordset
    9. Dim rs as ADODB.Recordset
    10.   Set rs = New ADODB.Recordset
    11. Dim strSQL As String
    12.   strSQL = "SELECT * FROM tbl_Nodes ORDER BY tvwNodes, tvwChildNodes, tvwGrandChild"
    13.   rs.Open strSQL, cn, adOpenKeyset, adLockPessimistic, adCmdText
    14.   '(note that MoveFirst is not needed after opening a recordset - it is just likely to cause problems!)
    15.  
    16. 'add items to the TreeView
    17. Dim lngNodeParent As Long, strNodeParent As String
    18. Dim lngNodeChild As Long, strNodeChild As String  
    19.   With TreeView1
    20.     Do While Not rs.EOF  '(basically the same as you had before, I just prefer this style!)
    21.         'if needed, add the parent
    22.       If strNodeParent <> rs.Fields("tvwNodes").Value Then
    23.         strNodeParent = rs.Fields("tvwNodes").Value
    24.         lngNodeParent = lngNodeParent + 1
    25.         .Nodes.Add , , CStr(lngNodeParent), strNodeParent
    26.       End If
    27.  
    28.         'if needed, add the child
    29.       If strNodeChild <> rs.Fields("tvwChildNodes").Value & "" Then
    30.         strNodeChild = rs.Fields("tvwChildNodes").Value & ""
    31.         If strNodeChild <> "" Then  '(dont add Null/blank child nodes)
    32.           lngNodeChild = lngNodeChild + 1
    33.           .Nodes.Add CStr(lngNodeParent), tvwChild, CStr(lngNodeChild), strNodeChild
    34.         End If
    35.       End If
    36.  
    37.       '(grand-child is similar to child, but you dont need to store the text/key!)
    38.  
    39.       rs.MoveNext
    40.     Loop
    41.   End With
    42.     'close the recordset
    43.   rs.Close
    44.   Set rs = Nothing
    45.  
    46. 'if not needed after this, close the connection here too

  15. #15

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Loading from DB into array

    Thanks si,

    I will try it out in a practise project.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  16. #16

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Loading from DB into array

    OK, I have put this code into a form_load event.

    I am getting the error "Type mismatch" on this line:

    vb Code:
    1. lngNodeParent = rs.Fields("tvwNodes").Value

    Which is in the Do While Loop.

    Another thing that may help, is the first field in the table is the ID table, so maybe this would help when identifying the keys?

    Would this

    vb Code:
    1. strSQL = "SELECT * FROM tbl_Nodes ORDER BY  tvwNodes, tvwChildNodes"rs.Open strSQL, cn, adOpenKeyset, adLockPessimistic, adCmdText

    then become this

    vb Code:
    1. strSQL = "SELECT * FROM tbl_Nodes ORDER BY ID, tvwNodes, tvwChildNodes"rs.Open strSQL, cn, adOpenKeyset, adLockPessimistic, adCmdText

    vb Code:
    1. Private Sub Form_Load()
    2. 'declare & set up the connection
    3. Dim cn As ADODB.Connection
    4.  
    5. Set cn = New ADODB.Connection
    6.  
    7. cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    8. "Data Source=C:\Documents and Settings\Andrew\My Documents\VB6\New Code Library\DBCL.mdb"
    9.  
    10. cn.Open
    11.  
    12. 'declare and set up the recordset
    13. Dim rs As ADODB.Recordset
    14. Set rs = New ADODB.Recordset
    15. Dim strSQL As String
    16.  
    17. strSQL = "SELECT * FROM tbl_Nodes ORDER BY tvwNodes, tvwChildNodes"
    18. rs.Open strSQL, cn, adOpenKeyset, adLockPessimistic, adCmdText
    19.  
    20. '(note that MoveFirst is not needed after opening a recordset - it is just likely to cause problems!)
    21.  
    22. 'add items to the TreeView
    23. Dim lngNodeParent As Long, strNodeParent As String
    24. Dim lngNodeChild As Long, strNodeChild As String
    25.  
    26. With tvwCode
    27.     Do While Not rs.EOF
    28.         '(basically the same as you had before, I just prefer this style!)
    29.         'if needed, add the parent
    30.         If lngNodeParent <> rs.Fields("tvwNodes").Value Then
    31.             lngNodeParent = rs.Fields("tvwNodes").Value
    32.             lngNodeParent = lngNodeParent + 1
    33.             .Nodes.Add , , CStr(lngNodeParent), strNodeParent
    34.         End If
    35.        
    36.         'if needed, add the child
    37.         If strNodeChild <> rs.Fields("tvwChildNodes").Value & "" Then
    38.             strNodeChild = rs.Fields("tvwChildNodes").Value & ""
    39.                 If strNodeChild <> "" Then  '(dont add Null/blank child nodes)
    40.                     lngNodeChild = lngNodeChild + 1
    41.                     .Nodes.Add CStr(lngNodeParent), tvwChild, CStr(lngNodeChild), strNodeChild
    42.                 End If
    43.         End If       '(grand-child is similar to child, but you dont need to store the text/key!)
    44.        
    45.         rs.MoveNext
    46.     Loop
    47. End With
    48. 'close the recordset
    49. rs.Close
    50. Set rs = Nothing
    51. 'if not needed after this, close the connection here too
    52. cn.Close
    53. End Sub

    The values in the DB are as below:

    Database View.gif
    Attached Images Attached Images  
    Last edited by aikidokid; Mar 10th, 2007 at 09:46 AM.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  17. #17
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Loading from DB into array / treeview

    That's understandable - the variable lngNodeParent is declared with a data type of Long, and you are trying to put a String value into it.

    You've made a little mistake in copying the code - check the difference between lines 22 & 23 of my code, and lines 30 & 31 of yours.

  18. #18

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Loading from DB into array / treeview

    I did actually look at that, thought it might have been this, but then left it as I had it

    I am now getting the error "Invalid Key" on this line (line number 25 of your code):

    vb Code:
    1. .Nodes.Add , , CStr(lngNodeParent), strNodeParent

    Would this be easier here to use the ID field here?
    Or is this nothing to do with it?

    Been here most of the day and getting confused here now
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  19. #19
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Loading from DB into array / treeview

    I think that will be my fault!

    I'm guessing that the Key (the value of CStr(lngNodeParent)) is already in use, as the Child version does similar things & doesn't use different numbers. Keys need to be unique (as they can be used to return items), so this is not allowed.

    We need to make sure that the numbers are unique, and to do that I think your can just add the following after line 24 of my code (untested I'm afraid!):
    Code:
       If lngNodeParent <= lngNodeChild Then lngNodeParent = lngNodeChild + 1
    ..and this after line 32:
    Code:
       If lngNodeChild <= lngNodeParent Then lngNodeChild = lngNodeParent + 1

  20. #20

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Loading from DB into array / treeview

    I am still getting the same error, on the same line as before!

    vb Code:
    1. .Nodes.Add , , CStr(lngNodeParent), strNodeParent

    My code now is:

    vb Code:
    1. Do While Not rs.EOF
    2.         '(basically the same as you had before, I just prefer this style!)
    3.         'if needed, add the parent
    4.         If strNodeParent <> rs.Fields("tvwNodes").Value Then
    5.             strNodeParent = rs.Fields("tvwNodes").Value
    6.             lngNodeParent = lngNodeParent + 1
    7.             If lngNodeParent <= lngNodeChild Then lngNodeParent = lngNodeChild + 1
    8.             .Nodes.Add , , CStr(lngNodeParent), strNodeParent
    9.         End If
    10.        
    11.         'if needed, add the child
    12.         If strNodeChild <> rs.Fields("tvwChildNodes").Value & "" Then
    13.             strNodeChild = rs.Fields("tvwChildNodes").Value & ""
    14.                 If strNodeChild <> "" Then  '(dont add Null/blank child nodes)
    15.                     lngNodeChild = lngNodeChild + 1
    16.                     If lngNodeChild <= lngNodeParent Then lngNodeChild = lngNodeParent + 1
    17.                     .Nodes.Add CStr(lngNodeParent), tvwChild, CStr(lngNodeChild), strNodeChild
    18.                 End If
    19.         End If       '(grand-child is similar to child, but you dont need to store the text/key!)
    20.        
    21.         rs.MoveNext
    22.     Loop
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  21. #21
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Loading from DB into array / treeview

    I just looked at the help for that error and it clearly says that numeric-only keys (even if they are converted to strings) are not valid - you need to append an character too, eg:
    Code:
    .Nodes.Add , , "P" & CStr(lngNodeParent), strNodeParent
    ...
    .Nodes.Add "P" & CStr(lngNodeParent), tvwChild, "C" & CStr(lngNodeChild), strNodeChild

  22. #22

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Loading from DB into array / treeview

    Quote Originally Posted by si_the_geek
    I just looked at the help for that error and it clearly says that numeric-only keys (even if they are converted to strings) are not valid - you need to append an character too, eg:
    Code:
    .Nodes.Add , , "P" & CStr(lngNodeParent), strNodeParent
    ...
    .Nodes.Add "P" & CStr(lngNodeParent), tvwChild, "C" & CStr(lngNodeChild), strNodeChild
    I was SO close to sorting this then.

    I appended a character to the parent node - but I used the same for the child node as well

    Thanks alot for this - again.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  23. #23

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Loading from DB into array / treeview

    You are a star si_the_geek

    It now all works as I was after
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  24. #24

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: [RESOLVED] Loading from DB into array / treeview

    If it is a help to anybody, the attached project is what I ended up with.

    Obviously you would have to change the path for the Database, and you can see how my DB-Table is layed out in previous posts. (#16)

    I hope it helps
    Last edited by aikidokid; Apr 23rd, 2007 at 03:10 PM.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  25. #25

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: [RESOLVED] Loading from DB into array / treeview

    Si_the_geek

    Help me out here please

    I have cut and paste this code and put it into my Code Library project.

    The treeview is called the same, and the database is the same, but for some reason I am getting the error:

    Element not found
    On this line inside the Do While Loop
    vb Code:
    1. .Nodes.Add , , "P" & CStr(lngNodeParent), strNodeParent, "Close"

    I have looked through this and cannot see any reason why it won't work - everything is the same in both the practise project and my actual project!

    It gives the correct strNodeParent, which is "API" and the lngNodeParent is returning "P1", which is also expected!

    EDIT
    I have tried moving the code to the form_Activate, from Form_Load, but that isn't it either!

    To save you looking back, here is the final code I had:

    vb Code:
    1. 'declare & set up the connection
    2. Dim cn As ADODB.Connection
    3.  
    4. Set cn = New ADODB.Connection
    5.  
    6. cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    7. "Data Source=C:\Documents and Settings\Andrew\My Documents\VB6\New Code Library\DBCL.mdb"
    8.  
    9. cn.Open
    10.  
    11. 'declare and set up the recordset
    12. Dim rs As ADODB.Recordset
    13. Set rs = New ADODB.Recordset
    14. Dim strSQL As String
    15.  
    16. strSQL = "SELECT * FROM tbl_Nodes ORDER BY tvwNodes, tvwChildNodes"
    17. rs.Open strSQL, cn, adOpenKeyset, adLockPessimistic, adCmdText
    18.  
    19. '(note that MoveFirst is not needed after opening a recordset - it is just likely to cause problems!)
    20.  
    21. 'add items to the TreeView
    22. Dim lngNodeParent As Long, strNodeParent As String
    23. Dim lngNodeChild As Long, strNodeChild As String
    24.  
    25. With tvwCode
    26.     Do While Not rs.EOF
    27.         '(basically the same as you had before, I just prefer this style!)
    28.         'if needed, add the parent
    29.         If strNodeParent <> rs.Fields("tvwNodes").Value Then
    30.             strNodeParent = rs.Fields("tvwNodes").Value
    31.             lngNodeParent = lngNodeParent + 1
    32.             If lngNodeParent <= lngNodeChild Then lngNodeParent = lngNodeChild + 1
    33.             .Nodes.Add , , "P" & CStr(lngNodeParent), strNodeParent, "Close"
    34.         End If
    35.        
    36.         'if needed, add the child
    37.         If strNodeChild <> rs.Fields("tvwChildNodes").Value & "" Then
    38.             strNodeChild = rs.Fields("tvwChildNodes").Value & ""
    39.                 If strNodeChild <> "" Then  '(dont add Null/blank child nodes)
    40.                     lngNodeChild = lngNodeChild + 1
    41.                     If lngNodeChild <= lngNodeParent Then lngNodeChild = lngNodeParent + 1
    42.                     .Nodes.Add "P" & CStr(lngNodeParent), tvwChild, "C" & CStr(lngNodeChild), strNodeChild, "Close"
    43.                 End If
    44.         End If       '(grand-child is similar to child, but you dont need to store the text/key!)
    45.        
    46.         rs.MoveNext
    47.     Loop
    48. End With
    49. 'close the recordset
    50. rs.Close
    51. Set rs = Nothing
    52. 'if not needed after this, close the connection here too
    53. cn.Close
    Last edited by aikidokid; Mar 12th, 2007 at 12:52 PM.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  26. #26
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] Loading from DB into array / treeview

    The "Close" part is the image parameter, which (from the help) specifies "The index of an image in an associated ImageList control".

    I presume you either haven't associated the ImageList, or you don't have an image in it which can be referenced using "Close".


    ps: there's no need for a PM on the same day as the latest reply in a thread, as I tend to leave a couple of threads open if I'm not sure of the answer & need to check them out in more depth.

  27. #27

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: [RESOLVED] Loading from DB into array / treeview

    Right again.

    I had given the image the name "Close" in the practise project, whereas in my main project it was "Folder Closed".

    All working again now, thanks
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width