我写的代码是:
        Do While daReRs.EOF = False
            If Format(daReRs.Fields("日期"), "yyyy-mm-dd") <= DateAdd("d", -2, Date) Then
                RSellGrid.ForeColor = vbBlue
            End If
            If Format(daReRs.Fields("日期"), "yyyy-mm-dd") >= DateAdd("m", -3, Date) Then
                RSellGrid.ForeColor = vbRed
            End If
            If Format(daReRs.Fields("日期"), "yyyy-mm-dd") <= DateAdd("d", -2, Date) And Format(daReRs.Fields("日期"), "yyyy-mm-dd") >= DateAdd("m", -3, Date) Then
                RSellGrid.ForeColor = vbBlack
            End If
            daReRs.MoveNext
        Loop但是实现不了.或者是有别的控件更好的用请推荐一下,谢谢!!

解决方案 »

  1.   

    就这些代码吗?只有对日期进行判断的,并没有把日期写入MSHFlexGrid中的代码啊
      

  2.   

    前面我有用set把数据写入MSHFlexGrid中了
    Format(daReRs.Fields("日期"), "yyyy-mm-dd")  
    这一段不就可以把数据库中的日期转化为yyyy-mm-dd格式的了?
    另外再说一下,数据库我是用access的
      

  3.   

    Format(daReRs.Fields("日期"), "yyyy-mm-dd")生成的是一个字符串,而DateAdd("d", -2, Date)生成的是一个日期,先把Format(daReRs.Fields("日期"), "yyyy-mm-dd")改成CDate(Format(daReRs.Fields("日期"), "yyyy-mm-dd"))才能进行比较。
    另外你的代码只是把数据库中所有的日期判断了一遍,并没有根据判断结果来设置相应单元格的颜色。
      

  4.   

    RSellGrid.ForeColor = vbBlue
    用这个不行的么??
      

  5.   

    我对MSHFlexGrid这个控件也不熟悉,不过你并没有定位到要设置颜色的单元格啊,比如你应该RSellGrid.row=5
    RSellGrid.col=4
    然后才能RSellGrid.ForeColor = vbBlue
    否则你遍历完数据库中的所有日期,设置的也不过是同一个单元格的颜色而已。
    实际上你应该这样做:在将日期写入RSellGrid中的时候,接着就对日期进行判断并设置颜色,比如将代码修改为:
            dim lRow as long,dim lCol as long
            lRow=0:lCol=0
            Do While daReRs.EOF = False
                RSellGrid.row=lRow
                RSellGrid.col=lCol
                RSellGrid.text=Format(daReRs.Fields("日期"), "yyyy-mm-dd")
                If cDate(Format(daReRs.Fields("日期"), "yyyy-mm-dd")) <= DateAdd("d", -2, Date) Then
                    RSellGrid.ForeColor = vbBlue
                End If
                If cDate(Format(daReRs.Fields("日期"), "yyyy-mm-dd")) >= DateAdd("m", -3, Date) Then
                    RSellGrid.ForeColor = vbRed
                End If
                If cDate(Format(daReRs.Fields("日期"), "yyyy-mm-dd")) <= DateAdd("d", -2, Date) And cDate(Format(daReRs.Fields("日期"), "yyyy-mm-dd")) >= DateAdd("m", -3, Date) Then
                    RSellGrid.ForeColor = vbBlack
                End If
                daReRs.MoveNext
                lRow=lRow+1
            Loop
    我是估计着改的,不大清楚MSHFlexGrid这个控件是不是有这些属性,也不知道row和col这两个属性的起始值是0还是1,你运行一下试试看吧,呵呵
      

  6.   

    不好意思,第一句就出现了笔误,应该是
    dim lRow as long,lCol as long
      

  7.   

    当RSellGrid有多行的时候出现" 实时错误'30009': 无效的行值 " 执行到这一行出错: RSellGrid.row = lRow
                     RSellGrid.row =1   lRow = 2当表只有一项的时候到是成功了.
      

  8.   

    忽略了一点,应该是先增加一行再将日期写入,即:
    RSellGrid.rows=RSellGrid.rows+1
    RSellGrid.row=lRow
    RSellGrid.col=lCol
    RSellGrid.text=Format(daReRs.Fields("日期"), "yyyy-mm-dd")
    .......
      

  9.   

    RSellGrid.ForeColor = vbBlue
    问题应该是这个语句,着色是整张表的.还有其他的控件吗?你是用什么控件的啊??
      

  10.   

    把RSellGrid.ForeColor换成RSellGrid.CellForeColor
      

  11.   

    改成RSellGrid.CellForeColor变成默认的黑色
      

  12.   

    不会的啊,我试过了,RSellGrid.CellForeColor=vbBlue可以把当前单元格的字体颜色设置为蓝色。
      

  13.   

    Dim lRow As Long, lCol As Long
                lRow = 0: lCol = 0
                Do While reNewRs.EOF = False
                    RSellGrid.Rows = RSellGrid.Rows + 1
                    RSellGrid.row = lRow
                    RSellGrid.Col = lCol
                    RSellGrid.Text = Format(reNewRs.Fields("日期"), "yyyy-mm-dd")
                    If CDate(Format(reNewRs.Fields("日期"), "yyyy-mm-dd")) >= DateAdd("d", -2, Date) Then
                        RSellGrid.CellForeColor = vbBlue
                    End If
                    If CDate(Format(reNewRs.Fields("日期"), "yyyy-mm-dd")) <= DateAdd("m", -3, Date) Then
                        RSellGrid.CellForeColor = vbRed
                    End If
                    If CDate(Format(reNewRs.Fields("日期"), "yyyy-mm-dd")) < DateAdd("d", -2, Date) And CDate(Format(reNewRs.Fields("日期"), "yyyy-mm-dd")) > DateAdd("m", -3, Date) Then
                        RSellGrid.CellForeColor = vbBlack
                    End If
                    reNewRs.MoveNext
                    lRow = lRow + 1
                Loop           
               Set RSellGrid.DataSource = reNewRs
               lbCount(1).Caption = reNewRs.RecordCount
               Set reNewRs = Nothing
    我的代码是这样写的啊,但是字体还是黑色.........
      

  14.   

    如果我把Set RSellGrid.DataSource = reNewRs这句放到最前面的话第一竖行就有颜色,然后表后面出现很多空白格.
      

  15.   

    Private Sub Form_Load()
       Dim i As Integer
       MSHFlexGrid1.Rows = 100
       For i = 1 To 97 Step 3
         With MSHFlexGrid1
            .TextMatrix(i, 1) = Format(Date - 2, "yyyy-mm-dd")
            .Row = i
            .CellForeColor = vbRed
            .TextMatrix(i + 1, 1) = Format(Date, "yyyy-mm-dd")
            .Row = i + 1
            .CellForeColor = vbGreen
            .TextMatrix(i + 2, 1) = Format(Date + 2, "yyyy-mm-dd")
            .Row = i + 2
            .CellForeColor = vbYellow
         End With
       Next
       
    End Sub
    对某一格用不同颜色时,row值应该改变
      

  16.   

    For i = 1 To reNewRs.RecordCount - 3 Step 3
                With RSellGrid
                If CDate(Format(reNewRs.Fields("日期"), "yyyy-mm-dd")) >= DateAdd("d", -2, Date) Then
                    .Rows = i
                    .CellForeColor = vbBlue
                End If
                If CDate(Format(reNewRs.Fields("日期"), "yyyy-mm-dd")) <= DateAdd("m", -3, Date) Then
                    .Rows = i + 1
                    .CellForeColor = vbRed
                End If
                If CDate(Format(reNewRs.Fields("日期"), "yyyy-mm-dd")) < DateAdd("d", -2, Date) And CDate(Format(reNewRs.Fields("日期"), "yyyy-mm-dd")) > DateAdd("m", -3, Date) Then
                    .Rows = i + 2
                    .CellForeColor = vbBlack
                End If
                End With
            Next我是这样写的,还是不行哦........
      

  17.   

    I服了U,应该是这个样子的……
         with  RSellGrid
            for i=1 to .rows-1
              .row=i
              ……
              ……         If CDateFormat(reNewRs.Fields("日期"), "yyyy-mm-dd")) >= DateAdd("d", -2, Date) Then
                    .CellForeColor = vbBlue
                End If
                If CDate(Format(reNewRs.Fields("日期"), "yyyy-mm-dd")) <= DateAdd("m", -3, Date) Then
                    .CellForeColor = vbRed
                End If
                If CDate(Format(reNewRs.Fields("日期"), "yyyy-mm-dd")) < DateAdd("d", -2, Date) And CDate(Format(reNewRs.Fields("日期"), "yyyy-mm-dd")) > DateAdd("m", -3, Date) Then
                    .CellForeColor = vbBlack
                End If
              next     end with 要区分rows与row 的区别
      

  18.   

    终于解决了.代码是这样写的        For i = 1 To RSellGrid.Rows - 1
                If CDate(Format(reNewRs.Fields("日期"), "yyyy-mm-dd")) >= DateAdd("d", -2, Date) Then
                    RSellGrid.row = i
                    For c = 0 To 18
                        RSellGrid.Col = c
                        RSellGrid.CellForeColor = RGB(0, 0, 255)
                    Next
                End If
                If CDate(Format(reNewRs.Fields("日期"), "yyyy-mm-dd")) <= DateAdd("m", -3, Date) Then
                    RSellGrid.row = i
                    For c = 0 To 18
                        RSellGrid.Col = c
                        RSellGrid.CellForeColor = RGB(255, 0, 0)
                    Next
                End If
                reNewRs.MoveNext
            Next
      

  19.   

    又出现问题了,数据显示到2900多就没有了.....
                    For c = 0 To 18
                        RSellGrid.Col = c
                        RSellGrid.CellForeColor = RGB(255, 0, 0)
                    Next这一句有没有可以实现整行填色的啊?用循环好象很慢.....