Error 2585 on closing a form (1 Viewer)

cjmitton

Registered User.
Local time
Today, 01:49
Joined
Mar 25, 2011
Messages
70
I have a list box (In a pop up form) where I can select a record and open it in the management form for viewing / editing / etc.

Below is the code thats works for the double click event (on the list box) and a command button I've added to the form.

Code:
Dim strWhere As String
    If Me!List_All_Contacts.ItemsSelected.Count = 0 Then Exit Sub
    For Each varItem In Me!List_All_Contacts.ItemsSelected
        strWhere = strWhere & Me!List_All_Contacts.Column(4, varItem) & ","
    Next varItem
 
    strWhere = Left$(strWhere, Len(strWhere) - 1)
 
    strWhere = "[C_ID] IN (" & strWhere & ")"
    DoEvents
    DoCmd.OpenForm FormName:="frm_Contacts_Mgt", WhereCondition:=strWhere, DataMode:=acFormReadOnly
    DoCmd.Close acForm, Me.Name '*** line which debug highlists ***
    Forms!frm_Contacts_Mgt.SetFocus

But when I press enter on a record in the list box I get run-time error 2585 and in debug the high-light line is my DoCmd.close? Its strange that it works for the other but not the 'enter' I tried adding DoEvents after looking through some previous posts but I'm not sure if I've used it correctly?
 

cjmitton

Registered User.
Local time
Today, 01:49
Joined
Mar 25, 2011
Messages
70
Yep, just tried it again as I've added the 'doEvents' since I tried that one.

Its strange that it only happens on the 'on enter' event for the list box but 'double click' event works fine?
 

boblarson

Smeghead
Local time
Yesterday, 17:49
Joined
Jan 12, 2001
Messages
32,059
I'm thinking you are not understanding what the ENTER event is. It is NOT when you press your Enter/Return key. It is somewhat similar to the Got Focus event. So, here's an explanation from this link:
http://office.microsoft.com/en-us/access-help/order-of-events-for-database-objects-HP005186761.aspx
Events occur for controls on forms when you move the focus (focus: The ability to receive user input through mouse or keyboard actions or the SetFocus method. Focus can be set by the user or by the application. The object that has focus is usually indicated by a highlighted caption or title bar.) to a control, and when you change and update data in a control.
Moving the focus to a control

When you move the focus to a control on a form — for example, by opening a form that has one or more active controls or by moving to another control on the same form — the Enter and GotFocus events occur in this order:
EnterGotFocus

If you are opening a form, these events occur after the events associated with opening the form (such as Open, Activate, and Current), as follows:
Open (form) → Activate (form) → Current (form) → Enter (control) → GotFocus (control)

So what code have you put in the On Enter event of the listbox?
 

cjmitton

Registered User.
Local time
Today, 01:49
Joined
Mar 25, 2011
Messages
70
Bob, Thanks for the reply.

This is where I've got confused! On my list box as well as the button / double click opening the record I want an 'enter'or 'return' to select a record if they decide to use the keyboard to scroll down using there arrow keys. In my lack of knowledge with Access I saw 'On Enter' and thought that was for when someone pressed the enter key! I there for put the code in my original post againest the On Enter Event.

How do I get a 'enter / return' form the keyboard to work then?
 

cjmitton

Registered User.
Local time
Today, 01:49
Joined
Mar 25, 2011
Messages
70
Worked it out now!

Had a look through a book and a quick google to find the ascii code for enter and I have it!

Code:
Private Sub List_All_Contacts_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
        Cmd_Open_Contact_Click
    Else
        Exit Sub
    End If
End Sub

I should have used on keypress and declared the key I need to 'action'.

Thanks folks
 

boblarson

Smeghead
Local time
Yesterday, 17:49
Joined
Jan 12, 2001
Messages
32,059
Actually there is a much easier way. I didn't get a chance to come on here to answer before you used your other method, but the simple solution is to set the property called DEFAULT of the command button to YES. That would have done it completely.
 

Users who are viewing this thread

Top Bottom