Solved

Using the keyboard arrow key to move around...

Posted on 2003-11-03
4
492 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
  • 2
4 Comments
 
LVL 39

Expert Comment

by:appari
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Perfect solution!

Thanks
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now