Problem with Treeview code


G

Guest

Folks, I copied, word for word, code I found from Microsoft's KB article
209898. I'm using Access 2003, not 2000, but that shouldn't matter here.
Here's the code:

Private Sub Form_Load()
On Error GoTo ErrForm_Load

Dim db As Database
Dim rst As Recordset
Dim nodCurrent As Node, nodRoot As Node
Dim objTree As TreeView
Dim strText As String, bk As String

Set db = CurrentDb
Set rst = db.OpenRecordset("Employees", dbOpenDynaset, dbReadOnly)
Set objTree = Me!xTree.Object
rst.FindFirst "[ReportsTo] Is Null"

Do Until rst.NoMatch
strText = rst![LastName] & (", " + rst![FirstName])
Set nodCurrent = objTree.Nodes.Add(, , "a" & rst!EmployeeID, _
strText)
bk = rst.Bookmark
AddChildren nodCurrent, rst

rst.Bookmark = bk
rst.FindNext "[ReportsTo] Is Null"
Loop

ExitForm_Load:
Exit Sub

ErrForm_Load:
MsgBox Err.Description, vbCritical, "Form_Load"
Resume ExitForm_Load

End Sub

Here's my problem; as soon as the program gets to line: Set nodCurrent =
objTree.Nodes.Add(, , "a" & rst!EmployeeID, strText), I get an error 13: Type
Mismatch. Can anyone offer any assistance as to why this isn't working?

Thanks!
 
Ad

Advertisements

G

Guest

Hi Kev,
You can try this

Private Sub Form_Load()
On Error GoTo ErrForm_Load

Dim db As Database
Dim rst As Recordset
Dim nodCurrent As Node, nodRoot As Node
Dim objTree As TreeView
Dim strText As String, bk As String

Dim tmp_str as string

Set db = CurrentDb
Set rst = db.OpenRecordset("Employees", dbOpenDynaset, dbReadOnly)
Set objTree = Me!xTree.Object
rst.FindFirst "[ReportsTo] Is Null"

Do Until rst.NoMatch
strText = rst![LastName] & (", " + rst![FirstName])

I dunno why add method doesn't accept string composed in the call. You have
to pass a variable that contains the comèposed string.

tmp_str="a" & rst!EmployeeID
Set nodCurrent = objTree.Nodes.Add(, , tmp_str, strText)
bk = rst.Bookmark
AddChildren nodCurrent, rst

rst.Bookmark = bk
rst.FindNext "[ReportsTo] Is Null"
Loop

ExitForm_Load:
Exit Sub

ErrForm_Load:
MsgBox Err.Description, vbCritical, "Form_Load"
Resume ExitForm_Load

End Sub
 
Ad

Advertisements

G

Guest

Prefixing the Node with MSComctlLib helped a lot. The rest of my problem
was, apparently, Dimming objTree as a TreeView. It didn't like that. Once I
removed 'As TreeView' from the declaration, everything worked fine. Weird.

Alex Dybenko said:
hi,
could also be that you have other reference to library, where node object
also declared, for example MS Word, then try to declare nodes like:

Dim nodCurrent As MSComctlLib.Node


--
Best regards,
___________
Alex Dybenko (MVP)
http://alexdyb.blogspot.com
http://www.PointLtd.com

KevinPublic said:
Folks, I copied, word for word, code I found from Microsoft's KB article
209898. I'm using Access 2003, not 2000, but that shouldn't matter here.
Here's the code:

Private Sub Form_Load()
On Error GoTo ErrForm_Load

Dim db As Database
Dim rst As Recordset
Dim nodCurrent As Node, nodRoot As Node
Dim objTree As TreeView
Dim strText As String, bk As String

Set db = CurrentDb
Set rst = db.OpenRecordset("Employees", dbOpenDynaset, dbReadOnly)
Set objTree = Me!xTree.Object
rst.FindFirst "[ReportsTo] Is Null"

Do Until rst.NoMatch
strText = rst![LastName] & (", " + rst![FirstName])
Set nodCurrent = objTree.Nodes.Add(, , "a" & rst!EmployeeID, _
strText)
bk = rst.Bookmark
AddChildren nodCurrent, rst

rst.Bookmark = bk
rst.FindNext "[ReportsTo] Is Null"
Loop

ExitForm_Load:
Exit Sub

ErrForm_Load:
MsgBox Err.Description, vbCritical, "Form_Load"
Resume ExitForm_Load

End Sub

Here's my problem; as soon as the program gets to line: Set nodCurrent =
objTree.Nodes.Add(, , "a" & rst!EmployeeID, strText), I get an error 13:
Type
Mismatch. Can anyone offer any assistance as to why this isn't working?

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top