Using the keyboard arrow key to move around...

The following is my example code:

Private Sub Form_Load()
Dim myConnection As String
   
    myConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
            & "C:\MyDatabase.mdb;Persist Security Info=False"

        With adodc1
            .ConnectionString = myConnection
            .CommandType = adCmdUnknown
            .RecordSource = "SELECT * FROM MyTable"
            .Refresh
            .Recordset.MoveFirst
        End With

        Set DataGrid1.DataSource = adodc1
        DataGrid.AllowUpdate = True

End Sub

I can move the cursor go to any cell by using the up & down, and the left & right arrow keys.
However, if I change the cell value, then I can't move the cursor anymore until I push enter.

In Access database, I can move around the cursor by using arrow keys right after I change the cell value. Does anyone know how to modify the above code so that I can move around the cursor by using arrow keys without hitting the enter button?

Thanks
2ndHandAsked:
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.

appariCommented:
once you start  editing in a datagrid cell datagrids editmode is set to true. to come out of editmode either we have to press the enter key or set the edit mode to false.

try something like this, this code changes edit mode to false and focus is in the same cell. if you want to move to next or previous cells use sendkeys and either send left or right arrow keys depending on the key pressed.

Private Sub DataGrid1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyRight Or KeyCode = vbKeyLeft Then
If DataGrid1.EditActive Then
    DataGrid1.EditActive = False
End If
End If
End Sub

code with sendkeys

Private Sub DataGrid1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyRight Or KeyCode = vbKeyLeft Then
If DataGrid1.CurrentCellModified Then
    DataGrid1.EditActive = False
    If KeyCode = vbKeyLeft Then
        SendKeys "{LEFT}"
    Else
        SendKeys "{RIGHT}"
    End If
End If
End If
End Sub
0
2ndHandAuthor Commented:
Thanks for your help.
The above sendkeys code works perfectly. However, I also need the cursor can move up or down as well. I tried to do something like below but it was not working. After I hitted either the up or down arrow key, the cursor disapppeared. Can you tell me what is wrong. How can I fix the problem?


Private Sub DataGrid1_KeyDown(KeyCode As Integer, Shift As Integer)
   
    If KeyCode = vbKeyRight Or KeyCode = vbKeyLeft Or _
       KeyCode = vbKeyUp Or KeyCode = vbKeyDown Then
        If DataGrid1.CurrentCellModified Then
            DataGrid1.EditActive = False
            Select Case KeyCode
                Case vbKeyLeft
                    SendKeys "{LEFT}"
                Case vbKeyRight
                    SendKeys "{RIGHT}"
                Case vbKeyUp
                    SendKeys "{UP}"
                Case vbKeyDown
                    SendKeys "{DOWN}"
            End Select
        End If
    End If
End Sub
0
benkamCommented:
appari is right, but the code will not help you to solve your problem.

Delete the line: DataGrid1.EditActive = False
Replace by: SendKeys "{ENTER}"
Then, you can move the cursor left, right, up and down :-)

Good luck!

--Ben Kam

Private Sub DataGrid1_KeyDown(KeyCode As Integer, Shift As Integer)
   
    If KeyCode = vbKeyRight Or KeyCode = vbKeyLeft Or _
       KeyCode = vbKeyUp Or KeyCode = vbKeyDown Then
        If DataGrid1.CurrentCellModified Then
            ' DataGrid1.EditActive = False
            SendKeys "{ENTER}"
            Select Case KeyCode
                Case vbKeyLeft
                    SendKeys "{LEFT}"
                Case vbKeyRight
                    SendKeys "{RIGHT}"
                Case vbKeyUp
                    SendKeys "{UP}"
                Case vbKeyDown
                    SendKeys "{DOWN}"
            End Select
        End If
    End If
End Sub
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
2ndHandAuthor Commented:
Perfect solution!

Thanks
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.