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,
swami_varahagirAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PePiCommented:
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


0
swami_varahagirAuthor Commented:
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,
0
PePiCommented:
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?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

swami_varahagirAuthor Commented:
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
0
PePiCommented:
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
0
swami_varahagirAuthor Commented:
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,

0
PePiCommented:
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.


0
swami_varahagirAuthor Commented:
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,
0
PePiCommented:
even if you use leavecell or rowcolchange event, when set the focus on cell2 and move to cell3, those event will fire.

i have tested the code i gave you and when i type or enter numberic values, leavecell doesn't fire. did you change your flexgrid's KeyPress event codes to the above code?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
swami_varahagirAuthor Commented:
Yes I did....

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

swami_v3@yahoo.com.

Thanks
0
atheekurrahmanCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.