Results 1 to 3 of 3
  1. #1
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Portland, Maine, USA
    Posts
    173
    Thanks
    0
    Thanked 0 Times in 0 Posts

    ListView problem setting forecolor (2002/SP2)

    Can anyone tell me why the code below doesn't change the forecolor of the row that I want red,
    but the the 2nd block of code does change the forecolor red?

    <pre>
    'Dim lvwObj As ListView
    Dim lstItem As ListItem
    Dim rst As New ADODB.Recordset
    Dim fld As ADODB.Field
    Dim iColWidth As Integer
    Dim i As Integer
    Dim strSQL As String

    strSQL = "SELECT * FROM tblLog;"

    'Set lvwObj = Me.lvwLog.Object
    rst.Open strSQL, CurrentProject.AccessConnection, adOpenDynamic, adLockOptimistic

    Me.lvwLog.ListItems.Clear

    'Create Column Headers
    With Me.lvwLog.ColumnHeaders
    .Clear
    For i = 0 To rst.Fields.Count
    For Each fld In rst.Fields
    If i = 0 Then
    iColWidth = 0
    Else
    iColWidth = (lvwLog.Width / (rst.Fields.Count - 1)) - 20
    End If
    .Add , , fld.Name, iColWidth
    i = i + 1
    Next fld
    Next i
    End With

    'If rst.BOF Then
    'No data
    'Else
    rst.MoveFirst
    While Not rst.EOF
    For i = 0 To rst.Fields.Count
    If i = 0 Then
    Set lstItem = Me.lvwLog.ListItems.Add(, , Nz(Trim(rst(i)), ""))
    'Set the color based on criteria
    If rst("log type") = "SUS" Then
    lstItem.ForeColor = vbRed
    End If
    ElseIf i < rst.Fields.Count Then
    lstItem.SubItems(i) = Nz(Trim(rst(i)), "")
    If rst("Log Type") = "SUS" Then
    lstitem.subitem.ForeColor = red
    End If
    End If

    Next i
    rst.MoveNext
    Wend
    'End If

    rst.Close
    </pre>

    ************************************************** *********

    <pre>Set rst = CurrentDb.OpenRecordset("SELECT * FROM Stock;")
    While Not rst.EOF
    Set li = LView.ListItems.Add(Key:="W" & rst!WineID, Text:=Nz(rst!Year))
    li.ForeColor = vbRed
    li.ListSubItems.Add Text:=Nz(rst!Winery)
    li.ForeColor = vbRed
    li.ListSubItems.Add Text:=Nz(rst!Wine)
    li.ListSubItems.Add Text:=Nz(rst!Colour)
    li.ListSubItems.Add Text:=Format(rst!Date, "Short Date")
    li.ForeColor = vbRed
    rst.MoveNext
    Wend
    rst.Close
    </pre>

    *********************************************

    I would much rather use the 1st block style because it is much more dynamic. I would just have to change the sql source.

    Thank you for any assistance with this one.
    Don

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 28 Times in 28 Posts

    Re: ListView problem setting forecolor (2002/SP2)

    In

    lstItem.SubItems(i) = Nz(Trim(rst(i)), "")
    If rst("Log Type") = "SUS" Then
    lstitem.subitem.ForeColor = red
    End If

    subitem and red are not valid VB. Replace the above code with

    With lstItem.ListSubItems.Add(Text:=Nz(Trim(rst(i)), ""))
    If rst("Log Type") = "SUS" Then
    .ForeColor = vbRed
    End If
    End With

  3. #3
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Portland, Maine, USA
    Posts
    173
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: ListView problem setting forecolor (2002/SP2)

    Works great now! Thanks!

Posting Permissions

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