Solved

Using the keyboard arrow key to move around...

Posted on 2003-11-03
4
506 Views
Last Modified: 2013-12-25
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
0
Comment
Question by:2ndHand
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 39

Expert Comment

by:appari
ID: 9676589
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
 

Author Comment

by:2ndHand
ID: 9681386
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
 
LVL 1

Accepted Solution

by:
benkam earned 150 total points
ID: 9684525
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
 

Author Comment

by:2ndHand
ID: 9689333
Perfect solution!

Thanks
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question