I'm trying to include a bmp image with the child node in a TREE GRID. I see it in the examples but I can't make it work. Here is my code and Access Database.
Regards
Mark
MASTER_DB.zip
(129kb) downloaded 6 time(s).Private Sub Form_Load()
On Error GoTo ErrHandler
Debug.Print "Form_Load: Starting"
' 1. Open the database
Debug.Print "Form_Load: Checking database file"
If Dir("C:\Users\User\Documents\000_AUTOCAD_ELECTRICAL_TOOLS\000_LIB\009_MASTER_DATABASE\MASTER_DB.accdb") = "" Then
MsgBox "Database file not found!", vbCritical
Exit Sub
End If
Debug.Print "Form_Load: Opening connection"
Set cnn = New ADODB.Connection
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\User\Documents\000_AUTOCAD_ELECTRICAL_TOOLS\000_LIB\009_MASTER_DATABASE\MASTER_DB.accdb;"
If cnn.State <> adStateOpen Then
MsgBox "Failed to connect to database!", vbCritical
Exit Sub
End If
Debug.Print "Form_Load: Connection opened"
' 2. Open recordset and extract fields from SYMBOL table
Debug.Print "Form_Load: Opening recordset"
Dim sSQL As String
sSQL = "SELECT ID, SYMBOL_NAME, DIR_PATH FROM SYMBOL ORDER BY SYMBOL_NAME"
Set rs = New ADODB.Recordset
rs.Open sSQL, cnn, adOpenStatic, adLockReadOnly
If rs.EOF And rs.BOF Then
MsgBox "No records found in SYMBOL table!", vbInformation
rs.Close
Exit Sub
End If
' 3. Use iGrid30 for TREEGRID
With iGrid30
Debug.Print "Form_Load: BeginUpdate"
.BeginUpdate
' 4. Set columns (ID, SYMBOL_NAME, DIRECTORY, Image)
.ColCount = 4
.ColHeaderText(1) = "ID"
.ColHeaderText(2) = "SYMBOL_NAME"
.ColHeaderText(3) = "DIRECTORY"
.ColHeaderText(4) = "Image"
' Set tree column to 2 (SYMBOL_NAME)
.TreeCol = 2
' Optimize row height
On Error Resume Next
.DefaultRowHeight = .GetOptimalCellHeight(bCheckVisible:=True)
If Err.Number <> 0 Then
Debug.Print "Form_Load: GetOptimalCellHeight failed: " & Err.Number & " - " & Err.Description
.DefaultRowHeight = 20
Err.Clear
End If
On Error GoTo ErrHandler
' Load PNG into ImageList for child nodes
Debug.Print "Form_Load: Loading PNG"
LoadPicture Picture1, "C:\Users\User\Documents\000_AUTOCAD_ELECTRICAL_TOOLS\000_LIB\009_MASTER_DATABASE\portrait-lighting.png"
ImageList1.ListImages.Clear
ImageList1.ListImages.Add 1, "pngIcon", Picture1.Picture
.ImageList = ImageList1 ' Try .ImageListCells if images don't display
' 5. Add root node "ALARMS"
.AddRow
.CellValue(1, 2) = "ALARMS"
Dim rootRow As Long
rootRow = 1
' 6. Add child nodes from SYMBOL_NAME with image
Dim childRow As Long
While Not rs.EOF
.AddRow vRowParent:=rootRow
childRow = .RowCount
.CellValue(childRow, 1) = rs!id
.CellValue(childRow, 2) = rs!SYMBOL_NAME
.CellValue(childRow, 3) = rs!DIR_PATH
.CellIcon(childRow, 4) = 1 ' Assign PNG to image column
.CellAlignH(childRow, 4) = igAlignHCenter
.CellAlignV(childRow, 4) = igAlignVCenter
rs.MoveNext
Wend
' Auto-width columns
.AutoWidthCol 1
.AutoWidthCol 2
.AutoWidthCol 3
.AutoWidthCol 4
.EndUpdate
Debug.Print "Form_Load: EndUpdate"
End With
rs.Close
Set rs = Nothing
Exit Sub
ErrHandler:
MsgBox "Error in Form_Load: " & Err.Number & " - " & Err.Description, vbCritical
Debug.Print "Form_Load: Error: " & Err.Number & " - " & Err.Description
If Not rs Is Nothing Then
If rs.State = adStateOpen Then rs.Close
Set rs = Nothing
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
On Error Resume Next
If Not rs Is Nothing Then
If rs.State = adStateOpen Then rs.Close
Set rs = Nothing
End If
If Not cnn Is Nothing Then
If cnn.State = adStateOpen Then cnn.Close
Set cnn = Nothing
End If
End Sub