Hi everyone,
I built a treeview form so It is dynamic.
I have a table like this:
https://www.accessforums.net/attachm...1&d=1326883315
I had never used this but I took the code from the net and I have created a form, that on the event "On Load" I have written the following code:
Option Compare Database
Option Explicit
Private varHash As Variant
Private Sub Form_Load()
On Error GoTo Error_Trap
Dim objNode As Node, strKey As String
Dim rst As DAO.Recordset, intKey As Integer
varHash = Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J")
With TreeView0
.Nodes.Clear
Set rst = CurrentDb.OpenRecordset("qry_Menu")
If rst.RecordCount > 0 Then
While Not rst.EOF
intKey = rst.Fields("IDTree").Value
If rst.Fields("Parent") = 0 Then
Set objNode = .Nodes.Add(, , NumberToString(intKey), rst.Fields("Description"))
objNode.Expanded = True
Else
Set objNode = .Nodes.Add(NumberToString(rst.Fields("Parent")), tvwChild, NumberToString(intKey), rst.Fields("Description"))
End If
rst.MoveNext
Wend
End If
End With
rst.Close
Set rst = Nothing
Error_Exit:
Exit Sub
Error_Trap:
MsgBox ("Error Code:" & Err.Number & " Error Description:" & Err.Description)
Resume Error_Exit
End Sub
Private Sub TreeView0_NodeClick(ByVal selectedNode As Object)
On Error GoTo Error_Trap
Dim intIDTree As Integer
Dim strSql As String
intIDTree = StringToNumber(selectedNode.Key)
strSql = "Select * from tbl_tree"
strSql = strSql & " where IDTree=" & intIDTree
Error_Exit:
Exit Sub
Error_Trap:
MsgBox ("Error Code:" & Err.Number & " Error Description:" & Err.Description)
Resume Error_Exit
End Sub
Private Function StringToNumber(strIn As String) As Integer
On Error GoTo Error_Trap
Dim i, j As Integer
Dim strReturn As String
For i = 0 To Len(Trim(strIn)) - 1
For j = 0 To UBound(varHash) - 1
If varHash(j) = Left(Right(strIn, Len(strIn) - i), 1) Then
strReturn = strReturn & j
Exit For
End If
Next
Next
StringToNumber = CInt(strReturn)
Error_Exit:
Exit Function
Error_Trap:
MsgBox ("Error Code:" & Err.Number & " Error Description:" & Err.Description)
Resume Error_Exit
End Function
Private Function NumberToString(intKey As Integer) As String
On Error GoTo Error_Trap
Dim i As Integer, strReturn As String
For i = 0 To Len(Trim(intKey)) - 1
strReturn = strReturn & varHash(Left(Right(intKey, Len(intKey) - i), 1))
Next
NumberToString = strReturn
Error_Exit:
Exit Function
Error_Trap:
MsgBox ("Error Code:" & Err.Number & " Error Description:" & Err.Description)
Resume Error_Exit
End Function
Untill now, everithing was going well and the treeview was working just fine. The problem is that my tree table has grown and the IDTree is now over 99. And I am having a "Error Code:13 Error Description; Type Mismatch". I have tested the several MsgBox that I have and the error is on the Private Function NumberToString,
particularly on this code (I think): For i = 0 To Len(Trim(intKey)) - 1
strReturn = strReturn & varHash(Left(Right(intKey, Len(intKey) - i), 1))
Next
Can anyone help me?
Thanks,
Daniela