Link to home
Start Free TrialLog in
Avatar of Robert Batchelor
Robert BatchelorFlag for United States of America

asked on

How do I modify an Access Combo Box to show the next selection than the line above?

User generated imageI have a subform with a combo box that lists the names of the rescued horses in alphabetical order.  I want the first line to work like it works now, starting with names beginning with the letter "A".  Let say I pick "Baby Girl."  When I go to the next line's combo box, after I click on the combo box'es down arrow, I want the combo box to start with the NEXT horse's name, "Baby Ray", as shown in the attached graphic.  That way I don't have to scroll through the "A" again (but I could if I need to).  Any ideas?
Avatar of mbizup
mbizup
Flag of Kazakhstan image

This can't be done with a combo box.

You can approximate it, I think, using a textbox, a command button, and a listbox all set up to *appear* like a combo box.   You would need VBA behind the command button to show/hide the listbox when needed and to highlight the 'next item' in the listbox.
Try this sample. As mbizup stated, it is impossible with pure combobox.
DBHorses.accdb
Try this: for 3 combo boxes, expand for extra.
Table a(aID as string), if integer remove single quotes from code bellow.
Private Sub cboAid1_AfterUpdate()
    Dim sql As String
    sql = cboAid1.RowSource
    sql = Replace(sql, "From a", "From a WHERE aID > '" & cboAid1 & "'")
    cboAid2.RowSource = sql
    cboAid2.Requery
End Sub

Private Sub cboAid2_AfterUpdate()
    Dim sql As String
    sql = cboAid1.RowSource
    sql = Replace(sql, "From a", "From a WHERE aID > '" & cboAid2 & "'")
    cboAid3.RowSource = sql
    cboAid3.Requery
End Sub

Open in new window

filter-next-selection.accdb
For me, I am just curious:

< Let say I pick "Baby Girl."  When I go to the next line's combo box, after I click on the combo box'es down arrow, I want the combo box to start with the NEXT horse's name, "Baby Ray", as shown in the attached graphic. >

...how are you sure that you will always want the next Name alphabetically?
Why not: "Baby Girl.", then "Secretariat", ..Then "Citation"?

...again, just curious here, ...or perhaps I am just not understanding something...

;-)

Jeff
Avatar of Robert Batchelor

ASKER

User generated imageJeff, I may or may not want to choose the next name in alphabetical order.  All I want to do is have a shorter list of names presented so I do not have to search through the whole list again.  A better example would be if the first horse was "Theodora (Dora)" in the first line.  The next horse that the donor wants to allocate money to is "Willow.”  Thus the combo box would "index" or "move to" or "automatically scroll to" (not sure of the technical term if, one exists, but it is as if the Row Source of the Combo Box changes) the horse after Willow which would be "Tiny Belle" and the horse that I need to choose would be "Willow", the second to the last name in the list.  Some donors can become quite specific and specify a dozen horses with each getting something different.  Rescues are under a lot of scrutiny these days and the donor's receipt needs to reflect their wishes and we need to spend their money the way they want it spent.

Thanks for asking for the clarification and I hope you have an idea.
http://bhfer.org/
I forgot to add that I enter the horses names alphabetically (the names are rarely consecutive)since most donors make their lists that way too.
@bobbat: have you tested my sample and sample from hnasr?
I will tonight, sorry for the delay but we had five adopted horses unexpectedly returned to us and turning them around for re-adoption has taken all my attention.
User generated imageHi Alex,  I get the attached error in MS Access 2007 Service Pack 3.  What can I do to correct this error?
Sorry, old version was uploaded (you can delete whole sub with error). Test this one.
DBHorses.accdb
User generated imageYour example works great!  In my database I get the error message shown in the graphic.  Any ideas?

Query 1 Reads: SELECT tblHorses.lngHorsesID, tblHorses.strHorseBarnName
FROM tblHorses
ORDER BY tblHorses.strHorseBarnName;

Query 2 Reads: SELECT TOP 1 Max(tblHorses.strHorseBarnName) AS MaxOfstrHorseBarnName
FROM tblDonationsDetails INNER JOIN tblHorses ON tblDonationsDetails.lngHorsesID = tblHorses.lngHorsesID
ORDER BY Max(tblHorses.strHorseBarnName) DESC;
If you open your Query2ForDonationDetailsForm, you will see correct field for name: MaxOfstrHorseBarnName, so you should change code to:
Me.cboHorsesID = DLookup("[IngHorsesID]",   "Query1ForDonationDetailsForm",   "[strHorseBarnName] > " & chr(34) & DLookup("[MaxOfstrHorseBarnName]", "Query2ForDonationDetailsForm")  & chr(34))

Open in new window

User generated imageAlex,  the RowSource to my Horse ID Combo Box (cboHorsesID) is:
SELECT tblHorses.lngHorsesID, tblHorses.strHorseBarnName
FROM tblHorses
ORDER BY tblHorses.strHorseBarnName;

The OnGotFocus Code is:
    If Me.NewRecord Then
       
    'New Als315, 8/28/12:
        Me.cboHorsesID = DLookup("[lngHorsesID]", "Query1ForDonationDetailsForm", "[strHorseBarnName] > " _
        & Chr(34) & DLookup("[MaxOfstrHorseBarnName]", "Query2ForDonationDetailsForm") & Chr(34))
       
    End If

But the combo box shows all the horses like before.  Any ideas?
I think in Query2ForDonationDetailsForm you should limit list to some of IDs (lngDonationsID or lngEventsID). Will be good if you upload sample DB with this form and related table with sample data
No feedback on my comment http:#38308866
Will load sample DB today.
Hi Alex, sample database attached.
DB-Horse-1-for-EE.accdb
Try this to see if I understand the problem.

Table tblDonationsDetails modified:
Added field strHorseBarnName selected from Combo34, field lngHorsesID is filled from Combo34 column(1) in after update event

Any clarifications?
DB-Horse-2-for-EE.accdb
SOLUTION
Avatar of als315
als315
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@ als315:
The values in the table are just a way to help the OP continue his work until a better solution is found. It is not used for any further processing because ID is stored.  If you go to table and change name value the ID will stay the same.
I  wanted OP comments for me to better understand his problem.
In current version if you go back end edit the first record, you notice that the current value not displayed in combo.
I don't have to assume things.

Does he need to re-edit the previous values?
@ bobbat:

Try this version. It does not use an extra field in table, instead it uses a query "idName"
Combo Box in form header.
Move record selector to relevant record, and select from combo box.

Code used in Combo34_AfterUpdate and Form_Current events.

Feedback!
DB-Horse-4-for-EE-.accdb
hnasr: Your DB-Horse-2-for-EE works OK but a horse's name has changed.  It is rare, but it has happened.  It may not matter since I use the horse's ID number when I use Queries.
Alex's other comments apply but perhaps I can live with those comments.  

Alex: Your DB Horse-3-For-EE works OK.  

It is rare, but sometimes I have to re-edit the horse.

hnasr: Your DB-Horse-4-for-EE works OK but has an awkward feel to it.  I can live with it but my wife does not like it and I am trying to get her or our Treasurer (another lady) to start using the database.

Suggestions for the next step?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks guys for your help.  The horses thank you too!
http://bhfer.wordpress.com/
Welcome!