Link to home
Start Free TrialLog in
Avatar of swami_varahagir
swami_varahagir

asked on

Comparing the values in 2 cells in a MSFLEXgrid control..

Hi ,

I have a flex grid control with 4 columns and I have to check wheather the value entered in 2nd column is greater than the value entered in the 3rd column of each row.

I am writing the code on the leavecell event but the code fires everytime I press a key. I want to execute it only when I leave the cell and move to another one or move the focus to another control.

My code is


Private Sub MSFlexGrid1_LeaveCell(index As Integer)
If MSFlexGrid1(index).Col = 3 Then
    If MSFlexGrid1(index).TextMatrix(MSFlexGrid1(index).Row, 2) > MSFlexGrid1(index).TextMatrix(MSFlexGrid1(index).Row, 3) Then
        MsgBox "Wrong Value"
    End If
End If
End Sub

Thanks,
Avatar of PePi
PePi

how are you entering data into the flexgrid? MSFlexGrid has no Cell editing feature.

anyways, try putting your code in the RowColChange event. I am assuming that initally, column 2 or 3 has the focus. you may also have to code the LostFocus event

let me know how it turned out


Avatar of swami_varahagir

ASKER

In RowColchange event also it remains the same.

I am using Keypress and KeyUp events to put the value I typed in into the cells. It is not an edit box but made it a kind of.

Let me hv some guide lines how to approach it.

Thanks,
i assume that you are using up, down, left & right arrow to move from one cell to another. can you show me your key_press and key_up codes so that i can have a better understanding on what you are doing?
These are the events I have written for the flex grid control.

Thanks,


Private Sub MSFlexGrid1_KeyPress(index As Integer, KeyAscii As Integer)
    Dim oldCol
    oldCol = MSFlexGrid1(index).Col
    MSFlexGrid1(index).Col = 0
       MSFlexGrid1(index).Col = oldCol
        If MSFlexGrid1(index).Col = 0 Or MSFlexGrid1(index).Col = 1 Then
            KeyAscii = 0
        End If
        If (KeyAscii = 8) Then
            If (Len(MSFlexGrid1(index).Text) > 0) Then
                MSFlexGrid1(index).Text = Left(MSFlexGrid1(index).Text, Len(MSFlexGrid1(index).Text) - 1)
            Else
                KeyAscii = 0
            End If
        ElseIf Len(MSFlexGrid1(index).Text) < 5 Then
        Select Case KeyAscii
            Case 48 To 57:
                MSFlexGrid1(index).Text = MSFlexGrid1(index).Text + Chr(KeyAscii)
            Case Else
                MsgBox "Only Numbers allowed"
        End Select
        End If
End Sub


Private Sub MSFlexGrid1_KeyUp(index As Integer, KeyCode As Integer, Shift As Integer)
    If Not (MSFlexGrid1(index).Col = 0 Or MSFlexGrid1(index).Col = 1) Then
        If KeyCode = 46 Then
            MSFlexGrid1(index).Text = ""
        End If
    End If
End Sub


Private Sub MSFlexGrid1_RowColChange(index As Integer)
If MSFlexGrid1(index).Col = 3 Then
    If MSFlexGrid1(index).TextMatrix(MSFlexGrid1(index).Row, 2) > MSFlexGrid1(index).TextMatrix(MSFlexGrid1(index).Row, 3) Then
        MsgBox "Wrong Value"
    End If
End If
End Sub
try this:

Private Sub flexgrid_KeyPress(KeyAscii As Integer)
    If flexgrid.Col = 0 Or flexgrid.Col = 1 Then
        KeyAscii = 0
    End If
   
    Select Case KeyAscii
        Case 8
            If (Len(flexgrid.Text) > 0) Then
                flexgrid.Text = Left(flexgrid.Text, Len(flexgrid.Text) - 1)
            Else
                KeyAscii = 0
            End If
        Case 48 To 57
            If Len(flexgrid.Text) < 5 Then
                flexgrid.Text = flexgrid.Text + Chr(KeyAscii)
            Else
                KeyAscii = 0
            End If
        Case Else
            MsgBox "Only Numbers allowed"
    End Select
End Sub

Private Sub flexgrid_LeaveCell()
    With flexgrid
        If Val(.TextMatrix(.Row, 2)) > Val(.TextMatrix(.Row, 3)) Then
            MsgBox "Col 2 is > Col 3"
        ElseIf Val(.TextMatrix(.Row, 2)) < Val(.TextMatrix(.Row, 3)) Then
            MsgBox "Col 3 is > Col 2"
        Else
            MsgBox "Col 3 = Col 2"
        End If
    End With
End Sub
Hi Pepi,

This LeaveCell event does not work. I have tried it before , also submitted the method before. Plz find some other alternative.

Thanks for the suggetion.

Thanks,

I've tested the code I've provided above and it works just fine. I think the problem you are having is that your code in the KeyPress event is a bit off. Try and change your KeyPress event code with the one I provided above. Now, using the up, down, left or right arrow will trigger the leavecell event.

let me know how it turned out.


Hi Pepi,

I modified the code as u have instructed bit the problem is every time I hit the key it fires the LeaveCell event.

If say cell2 contains 45 and I move to cell3 and try to enter something, it fires the leavecell event and then puts the va;ue into the cell.

I want it to check when I complete the data entry and navigate to another cell or change my focus to another control in the form.

Thanks,
ASKER CERTIFIED SOLUTION
Avatar of PePi
PePi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes I did....

If you could zip it and send to my email it will; be appreciated.

swami_v3@yahoo.com.

Thanks
compare the values for every event that cause the grid to lose focus.
i.e for
        form_click() , msflexgrid_click , and for all other controls in the form(buttons and stuff)

if u r using a text box to edit the grid then use this as well

Private Sub txtValueEditor_KeyDown(KeyCode As Integer, Shift As Integer)
   With flexgrid
      Select Case KeyCode
         Case 13   'ENTER
            .SetFocus
         Case 27   'ESC
            txtValueEditor.Visible = False
            .SetFocus
         Case 38   'Up arrow
            .SetFocus
            DoEvents
            .Row = IIf(.Row > .FixedRows, .Row - 1, .Row)
         Case 40   'Down arrow
            .SetFocus
            DoEvents
            .Row = .Row + 1
      End Select
   End With
End Sub