Solved

Next record button based on combo box

Posted on 2014-02-10
14
831 Views
Last Modified: 2014-02-19
I am putting together a database form.  I have a next record button.  Right now the next record button goes to the next record from a table.  But,  I need the next record button to be based on the records shown in the right combo box.  

At the top of the contact form.  There are three combo boxes.  I need the next record button to go to the next record of the records in the third right combo box.  

See attached sample database.
1.accdb
0
Comment
Question by:lehi52
  • 7
  • 7
14 Comments
 
LVL 10

Expert Comment

by:Gozreh
ID: 39848059
you can use ListIndex example (you should change it to your field names)

to go next
ComboName.SetFocus
If ComboName.ListIndex <> ComboName.ListCount - 1 Then
        ComboName.ListIndex = ComboName.ListIndex + 1
      Else
        ComboName.ListIndex = 0
      End If
End Sub

Open in new window

and to go back
ComboName.SetFocus
If ComboName.ListIndex <> 0 Then
        ComboName.ListIndex = ComboName.ListIndex - 1
      Else
        ComboName.ListIndex = ComboName.ListCount - 1
      End If
End Sub

Open in new window

0
 

Author Comment

by:lehi52
ID: 39848120
I get an error when I change the name in the code to the name of the combo box.  This code refers to the "display" combo box.   I am sure I did something wrong. I posted the code into the VBA code.  Does the code need to be put into a specific event procedure to work?

display.SetFocus
If display.ListIndex <> display.ListCount - 1 Then
        display.ListIndex = display.ListIndex + 1
      Else
        display.ListIndex = 0
      End If
End Sub
------------------------------------------------------------
display.SetFocus
If display.ListIndex <> 0 Then
        display.ListIndex = display.ListIndex - 1
      Else
        display.ListIndex = display.ListCount - 1
      End If
End Sub

Open in new window

0
 
LVL 10

Expert Comment

by:Gozreh
ID: 39848301
Where did you put this code ?
Private Sub Command51_Click()
   Me.display.SetFocus
   If Me.display.ListIndex <> Me.display.ListCount - 1 Then
      Me.display.ListIndex = Me.display.ListIndex + 1
   Else
      Me.display.ListIndex = 0
   End If
End Sub

Open in new window

Private Sub Command52_Click()
   Me.display.SetFocus
   If Me.display.ListIndex <> 0 Then
      Me.display.ListIndex = Me.display.ListIndex - 1
   Else
      Me.display.ListIndex = display.ListCount - 1
   End If
End Sub

Open in new window

0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

Author Comment

by:lehi52
ID: 39848355
I just put it into the VBA code.  no where specifically.  I just added it into there.   It did not work with those changes.
0
 

Author Comment

by:lehi52
ID: 39848366
It does work but it spits out an error.  Run-Time Error '7777' You've used the List Index property incorrectly.

It goes to the next record and cycles through only those names,  but the issue is after I click next each time the error pops up.
0
 
LVL 10

Expert Comment

by:Gozreh
ID: 39848422
please upload your sample
0
 

Author Comment

by:lehi52
ID: 39848435
Here you go.
2.accdb
0
 
LVL 10

Expert Comment

by:Gozreh
ID: 39848461
Go to the VBA (Press Alt+F11 from main application), then select debug / compile database.
when you will get an error comment that line, and compile again.
then test the code again.
0
 

Author Comment

by:lehi52
ID: 39848474
Now it cycles through, but it only changes the name in the combo box,  and does not change the entire record displayed on the form.
0
 
LVL 10

Expert Comment

by:Gozreh
ID: 39848477
add after the code display_AfterUpdate
Private Sub Command51_Click()
   Me.display.SetFocus
   If Me.display.ListIndex <> Me.display.ListCount - 1 Then
      Me.display.ListIndex = Me.display.ListIndex + 1
   Else
      Me.display.ListIndex = 0
   End If
   display_AfterUpdate 
End Sub

Open in new window

0
 

Author Comment

by:lehi52
ID: 39848502
Gives an error again after adding that.  Same error as before.
3.accdb
0
 
LVL 10

Accepted Solution

by:
Gozreh earned 500 total points
ID: 39848547
Just change to this
Private Sub Command51_Click()
   On Error Resume Next
   If Me.display.ListIndex <> Me.display.ListCount - 1 Then
      Me.display.SetFocus
      Me.display.ListIndex = Me.display.ListIndex + 1
   Else
      Me.display.ListIndex = 0
   End If
   DoCmd.SearchForRecord , , acFirst, "[ContactID] = " & Nz(Me.display, 0)
End Sub

Open in new window

4.accdb
0
 
LVL 10

Expert Comment

by:Gozreh
ID: 39848560
Just to let you know, you should compact your database regularly, the file you uploaded is 4.9MB and the file i uploaded is 1.5MB.
http://office.microsoft.com/en-us/access-help/compact-and-repair-an-access-file-HP005187449.aspx
0
 

Author Closing Comment

by:lehi52
ID: 39848578
Great solution.   Thank you.
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…

860 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