Link to home
Start Free TrialLog in
Avatar of 2ndHand
2ndHand

asked on

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
Avatar of appari
appari
Flag of India image

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
Avatar of 2ndHand
2ndHand

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of benkam
benkam

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
Avatar of 2ndHand

ASKER

Perfect solution!

Thanks