Results 1 to 13 of 13
  1. #1
    2 Star Lounger
    Join Date
    Apr 2003
    Posts
    139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    List Boxes (2000)

    Hi all,

    I would like my list box in my form to set the focus on the next record after the user clicks the "Save & Add New Record" button. How do I go about doing this?

    Thanks,
    Kindra

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: List Boxes (2000)

    Can you explain how the form and list box work together?

  3. #3
    2 Star Lounger
    Join Date
    Apr 2003
    Posts
    139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: List Boxes (2000)

    The form is where supervisors can input their employee's timecard. The supervisor selects their employee from the list box, fills in their time in a text box, clicks the "Save & Add Record" button to input the next employee. It is that action that I want the list box to automatically give the next employee in the list box the focus so that the supervisor doesn't have to constantly click with their mouse.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: List Boxes (2000)

    Is your form bound or unbound? Does the employee ID (or whatever) get set when the supervisor selects an employee in the list box, or when (s)he clicks the Save & Add Record button?

  5. #5
    2 Star Lounger
    Join Date
    Apr 2003
    Posts
    139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: List Boxes (2000)

    The form is bound to a table.

    The employee ID gets set when the supervisor selects an employee (the save button is really just more for show -- my users are used to things like that).

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: List Boxes (2000)

    You could do something like this in the On Click event of the "Save & Add Record" button, after saving the record:

    If Me.lbxEmployees.ListIndex < Me.lbxEmployees.ListCount - 1 Then
    Me.lbxEmployees = Me.lbxEmployees.ItemData(Me.lbxEmployees.ListIndex + 1)
    lbxEmployees_AfterUpdate
    End If

    where lbxEmployees is the name of the list box, and where I assumed that you have an After Update event procedure for the list box; if you have an On Click procedure, use lbxEmployees_Click.

  7. #7
    2 Star Lounger
    Join Date
    Apr 2003
    Posts
    139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: List Boxes (2000)

    I added the code as you have you listed here. Here's my VB Code:

    Private Sub cmdAdd_Click()
    Dim varID_No As Variant
    Dim varDEPT As Variant
    Dim varWEEK_ENDING As Variant

    'Adds record only if the following conditions are met:
    'The Week Ending date has been entered
    If IsNull(Me.WEEK_ENDING) = True Then
    MsgBox "Please enter a week-ending date"
    'A name must be selected
    ElseIf Me.lstNames.ListIndex = -1 Then
    MsgBox "Please select an employee"
    Else
    'Sets the defaults to whatever the previous record was.
    varDEPT = Me.DEPT
    varWEEK_ENDING = Me.txtWeekEnding
    'Advance to the next record
    DoCmd.GoToRecord , , acNewRec
    'Sets next variables
    Me.DEPT = varDEPT
    Me.txtWeekEnding = varWEEK_ENDING
    'Sets focus to the next record
    If Me.lstNames.ListIndex < Me.lstNames.ListCount - 1 Then
    Me.lstNames = Me.lstNames.ItemData(Me.lstNames.ListIndex + 1)
    lstNames_AfterUpdate
    End If
    End If

    End Sub

    However, it returns to the first record in the list box instead of advancing to the next one? It seems like this code should lead it to the next one, but it doesn't.

    My after_update procedure for the list box is:

    Private Sub lstNames_AfterUpdate()
    'When a new name is chosen, the data is requeried to populate the fields with the correct data
    Me.txtHoursReq.Requery
    End Sub

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: List Boxes (2000)

    Remove the line

    lstNames_AfterUpdate

    I shouldn't have included it; it is superfluous.

  9. #9
    2 Star Lounger
    Join Date
    Apr 2003
    Posts
    139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: List Boxes (2000)

    It's still wanting to go back to the 1st record, even after I remove the after_update. Hmmmm..... It really seems like that should work! Do I have to add an event to the me.lstEmployees to get it to set the focus?

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: List Boxes (2000)

    I'm afraid I can't give more advice without seeing the database.

  11. #11
    2 Star Lounger
    Join Date
    Apr 2003
    Posts
    139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: List Boxes (2000)

    The database is attached...
    Attached Files Attached Files

  12. #12
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: List Boxes (2000)

    The list box lstNames is bound to the ID_NO field. When you move to a new record, ID_NO is blank, so the selection in the list box is cleared. The list index becomes -1, adding 1 sets the list index to 0, i.e. the first item.

    This will get around it:

    Dim lngIndex As Long
    ...
    'Sets the defaults to whatever the previous record was.
    lngIndex = Me.lstNames.ListIndex
    varDEPT = Me.DEPT
    varWEEK_ENDING = Me.txtWeekEnding
    'Advance to the next record
    DoCmd.GoToRecord , , acNewRec
    'Sets focus to the next record
    If lngIndex < Me.lstNames.ListCount - 1 Then
    Me.lstNames = Me.lstNames.ItemData(lngIndex + 1)
    End If
    ...

    I got other errors when I tried to close the form, but that is probably because this is a stripped-down version of the database.

  13. #13
    2 Star Lounger
    Join Date
    Apr 2003
    Posts
    139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: List Boxes (2000)

    Ah! Parfait! Merci beaucoup!!!!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •