Solved

Next record button based on combo box

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

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql statement - 9 24
Column Layout in Access Xp VBA Report 3 19
Should I keep recordsets open? 3 23
Field naming convntions - MS Access 7 20
When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

786 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