Solved

Next record button based on combo box

Posted on 2014-02-10
14
797 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
 

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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Familiarize people with the process of utilizing SQL Server views 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 Access…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

758 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

23 Experts available now in Live!

Get 1:1 Help Now