Link to home
Start Free TrialLog in
Avatar of misgci
misgci

asked on

Enter key behavior in a ComboBox on a form

I have a combo box in an Access 2003 form in which I would like to be able to  enter multiple lines.  Text boxes have a property called “Enter key behavior” which can be set to “New line in field”, which is what I want, but combo boxes don’t have that property available.

First off, if I’m missing something simple please let me know.

After trying a lot of different things I came up with a solution that “sort of” works.

In the VBA code on the form I added a variable “EnterPressed”:

      Option Compare Database
      Dim EnterPressed As Boolean ‘ set EnterPressed as a global variable
      --------------------------------------------

In the ComboBox On Enter Event I have (the control name is “Item”):

      Private Sub Item_Enter()

      EnterPressed = False

      End Sub



In the KeyDown event I have:

      Private Sub ITEM_KeyDown(KeyCode As Integer, Shift As Integer)
      
      If KeyCode = vbKeyReturn Then
                EnterPressed = True
      End If
      
      End Sub




And in the Exit event I have:

If EnterPressed Then

   DoCmd.CancelEvent
   Me.Item = Me.Item & vbCrLf    ' Add a new line to the field
   DoCmd.GoToControl "Item"       ' Move the focus back to the field
                
   ' Move cursor to the position 1 more than the length of the field
   Me.Item.SelStart = Len(Me.Item) +1                                                      
   ' Make the number of characters  actually  selected = zero
   Me.Item.SelLength = 0                                                                                
                    
   EnterPressed = False
     
End If


The idea is that when a user enters the field a flag is set that indicates they have not hit the Enter key yet.  If they don’t, and Tab or Mouse out of the field, nothing unusual happens.  If they do hit Enter, a flag is set, and when the field is exited a cr/lf is added to the field, focus is set back to the field, the curser is put at the beginning of the new line, and they keep typing.

This works, EXCEPT if there is already a cr/lf at the end of the string when the On Exit event runs.  This happens if the user tries to double space by hitting Enter twice, or had hit Enter once before but did not enter any additional text before leaving the field.

If these cases, the code seems to run all the way through correctly, the cr/lf is added, but the cursor ends up at the beginning of the field and the entire field is selected.

If anyone can help it would be great.  If there is a completely different approach that would be great also.

Ralph
SOLUTION
Avatar of nico5038
nico5038
Flag of Netherlands image

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
Hi,

Combo box in Access was NOT build for this purpose, you'll end up no where, retrofitting this kind of multiline functionality into combo box artificially.

So, it's better for you, please donot waste your precious time in that. If you want to get multline input, use appropiate input control like a textbox etc.


Thanks,
Syed Nabeel.
Avatar of misgci
misgci

ASKER

Nic,

I can live with CTRK+Enter, it's what I usually do myself, I was just trying to make it easier for the people using the form.
They may just have to get used to it.

Syed,

But the fun of doing stuff like this is to make an application do more than it was intended to do!
I've got it so close to working I have to give it at least a little more effort.

The reason I want a combo box is that this is an item description on a purchase order.  The combo box looks up items previously ordered from the selected vendor.  Since these descriptions are often repeated, and are sometimes long and complicated, with item codes, style numbers, etc., it is much easier and more efficient if the user can pick from a list once they put in the first few characters.

So, I'm going to hold out a little more, and keep plugging away at it myself and hopefully have a solution.

Thanks for your comment.

ralph
ASKER CERTIFIED SOLUTION
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 Jeffrey Coachman
misgci,
<But the fun of doing stuff like this is to make an application do more than it was intended to do!>
Not exactly.
Remember the old saying:
Just because something *can* be done, doesn't mean it *should* be done. (even if it is fun)
:)

Like the two previous posters say, what you are asking is Non-standard and counter-intuitive. If your user acclimate themselves to this, they will expect it in other windows base programs, and it won't work!
No combobox, in any other program, works in the way you are describing.
(check the Microsoft help file.)

If you are simply trying to stop users from accidentally pressing Enter and leaving a field, simply turn this functionality off in the options menu:
Tools--> Options-->Keyboard
Change: Move After Enter
To: Don't move

Nico5038 is correct in providing you with a workaround.
harfang is also to be commended for his solution.
(As anyone will tell you,: "The customer is always right")
:)
And they have, in fact, answered your original question.

But just humor me:
What are you ultimately trying to accomplish?
Or, more specifically, what user error are you trying to avoid?
Or am I missing the point altogether?
:)

jeff
Avatar of misgci

ASKER

It's really not that complicated - when users are filling in the description of a line-item on a purchase order, the control on the form is a combo box which looks up previously purchased items from the current vendor.  It is faster and more accurate for the users to be able to pick from a list since many items are repeat orders.

This works fine.  Before it was changed to a combo box, it was a text box.  When it was a text box users could press enter to get a new line which allowed each line-item description to be two or three lines, which makes the purchase orders easier to read.

With it being a combo box, as nico5038 said, the same result could be had by pressing CTRL+Enter, but users are already in the habit of just hitting enter to create a new line.  When doing so they end up in the next field, which is numeric, and end up with errors.  I could change the move after enter option, but that becomes universal for all Access applications, which may not be the desired result.

I was looking for a way to programmatically change an "Enter" to a "CTRL+Enter", which would give  users the same keyboarding method they had before, with the added benefit of having a drop down box.  I tried several ways to do this but nothing worked the way I wanted.

I haven't been able work on this for a few days, but I am probably going to take a look at the approach suggested by harfang and use a separate unbound combo box for a lookup list, but in the meantime, the users are simply using CTRL+Enter, which gives them the results they want.


Hello

In the top of my answer, I also provided a solution for that: have the enter key create a new line in a combo box. It's the first function, just copy-paste and adjust for your combo's name.
[I'm still recomending you change it to an external combo, though ;) ]

Good luck!
(°v°)
OK,

Thanks for the info.
Avatar of misgci

ASKER

Well, when I tried that as is (changing the combo box name) it didn't work.  You could see the cursor momentarily jump to a new line, but then it would go right back to the end of the first line.  The length of the string in the combo box did not change.

If I modify it as follows:

Private Sub cboNotes_KeyDown(KeyCode As Integer, Shift As Integer)

    Dim intPos As Integer

    If KeyCode = vbKeyReturn _
    And (Shift = 0 Or Shift = acCtrlMask) Then
        KeyCode = 0   ' we handle thar ourselves!
        intPos = cboNotes.SelStart
        'cboNotes.SelText = vbCrLf
        cboNotes=cboNotes & vbCrLf
        cboNotes.SelStart = intPos + Len(vbCrLf)
    End If

End Sub

it works, but only if the Enter key is hit once.  If it is hit twice, as if the user wanted to skip a line, it seems to strip the second CrLf off, in the same manor as the original code.

Access apparently is just not going to cooperate with this, so the second approach will be the way to go.
It's probably best, yes. Let's not continue that path. I did test it before  posting, however, and it worked for me. And incidentally, this line does not work, you need to use the .Text property:

      cboNotes.Text = cboNotes.Text & vbCrLf

But as I said, it's best to just drop it.
Cheers!
(°v°)
Avatar of misgci

ASKER

OK well I appreciate all the comments and especially the advice from harfang.
I'll go with the unbound combo box and stop trying to twist Access to do something it shouldn't.

Thanks again for your time and helpfulness.

Ralph