Default Value in combo box

O

O....

How do I make the default value in my combo box the last record entered into
the table
 
D

Dale Fye

Do you want this just for the current session of the database, or for the
future as well?

If you only want it for the current session, then in the AfterUpdate event
of the combo box, just set that controls defalutvalue to the current value.
Something like:

Private Sub cbo_YourCombo_AfterUpdate

me.cbo_YourCombo.DefaultValue = me.cbo_YourCombo

End Sub

If you want this value to be the default for that combo the next time you
open the database, you will need to store the value somewhere in your
database. You could put it in a table, or store it as a property of the
database.
1. I usually include a db_Parameters table (in the backend database) which
contains a single record and fields for some of the values that I want to
persist for all users using the database. I also have a local_Parameters
table, for those values that are apply to each user. This table is
maintained within the front-end of the database. If you use this technique,
then when you open the form, you need to set the parameters value. Something
like:

Private Sub Form_Open()

me.cbo_YourCombo.DefaultValue = DLOOKUP("fieldname", "local_Parameters")

End Sub

Keep in mind that you also need to save this value when the form closes.

2. Post back if you want to try the second option

--
HTH
Dale

Don''t forget to rate the post if it was helpful!

email address is invalid
Please reply to newsgroup only.
 
O

O....

The First option did not work. I want the last entered to to be the default
value
 
D

Dale Fye

What do you mean, didn't work?

The combo box will only display the default value on a new record, but the
code I gave you for the AfterUpdate event will execute any time you make a
change to a value in that combo box, and will persist until you close that
form (I think I incorrectly indicated database in my previous post).

Another way to persist that value from one instance of opening the form to
the next is to create a global variable (I don't like to do this) in the
forms Open event, set the default value to the global variable.

--
HTH
Dale

Don''t forget to rate the post if it was helpful!

email address is invalid
Please reply to newsgroup only.
 
O

O....

Sorry, Let me start over. I have a form with a combo box(Customer Name ) and
a add a new record button, if the value is not in the combo list, the user
clicks on add a new record which brings them to a customer entry form once
the fill out the required field they click on the save button and it returns
them back to the main form with the combo box. I want the default value in
the combo box to be the customer record just entered(Customer Name ).
 
D

Dale Fye

Much better explaination.

I generally do this by hiding the second form, rather than closing it. If
you open the form using the acDialog parameter of the OpenForm method, then
when you hide it, the code will continue to run. You can then refer to the
value on the second form.

1. In the first forms command button

Private Sub cmdNotInList_Click

docmd.OpenForm "formName",,,,,acDialog

me.cbo_CustomerName.requery
me.cbo_CustomerName = Forms("formName").txt_CustomerName
docmd.close acform, "formName"

End Sub

This code would open the form titled "formName". I generally remove all of
the ways to close a form other than a "Close" button in the bottom right
corner of the form. When they click on this button, I change the forms
Visible property to False, which allows the code in the main forms
cmdNotInList event to continue processing.

Since your combo now needs to be requeried so that it contains the new value
you entered, I do that, then I set the value of the combo box to that in the
2nd form. Finally, I close the second form.

--
HTH
Dale

Don''t forget to rate the post if it was helpful!

email address is invalid
Please reply to newsgroup only.
 

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