Setting Default Value in List Box

P

padmajakrishnan

I have a form (form1) that has a list box called "Code" (sorted
ascending) and its details from a table. Clicking "New" button on this
form1 opens Form2. After entering the Code and its details, I save this
records. The new record is added to the table and the list box is
refreshed to reflect the new record. When it refreshes, it sorts the
values in ascending. Form2 closes and we are back in Form1.

Now, I want the default selection in the List Box to be the new record
that we added. I wouldn't know the Item index because the data is
sorted and the new value could be anywhere in the list box.

In the Save click event of Form2, I tried using:
Forms!Form1!Code.DefaultValue=new code

But it didn't work. How do I set the default to the new record?

Thanks a lot in advance!
 
J

Jeff L

If your bound column in the List Box is the ID, you would need to know
the ID of the newly entered code for it to work properly.
 
G

Guest

Here is a plan that will make that happen.

Immediately after you requery the list box:

intItems = Me.Code.ListCount - 1
For intCtr = 0 to intItems
If Me.Code.ItemData(intCtr) = new code Then
Me.Code.Selected(intCtr) = True
Exit For
End If
Next intCtr

I don't know how you are getting "new code", but the above will show the new
code value as selected.
 
P

padmajakrishnan

Klatuu, thank you very much! It worked! Thanks for your reply to my
previous posting too!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top