Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assign last record in combo as default

Status
Not open for further replies.

antonyx

Technical User
Jan 19, 2005
204
0
0
GB
Me!ComboName.DefaultValue = Me!ComboName.ItemData(0)


this assigns the first value in the combo box to the default.

can this be changed to always assign the last possible record in the list as the default.
 
actually i do need this.. anyone know how to do this??
 
Oh this combo list post

I am not sure but you may need the equal and maybe the hypen if it is a string
Me!ComboName.DefaultValue = "='" & Me!ComboName.ItemData(me!comboname.listcount-1) & "'"
 
yes that worked perfectly thank you
 
ok i prefer this way, which automatically shows the first value

Me!bookf.DefaultValue = Me!bookf.ItemData(0)

when the form opens as a new record, the correct first record in the combo is displayed.

if the user just tabs past it tho, then it wont store that default value to my new record.

you still have to select it. i actually need the default value to be assigned to the record automatically when the form starts??

can i do this?
 
so basically a new record is created as soon as the form loads and the default values of the combo boxes are automatically assigned to that new record
 
Perhaps something like:
Code:
If Me.bookf & "" = "" Then
  Me.bookf = Me!bookf.ItemData(0)
End If
This assumes that bookf is bound to a field in your table and is text.
 
yes it is bound to a field and it is text..

where would i put this tho.. in the form Onload event??
 
On Current was the one I tested with.
 
so by pasting this code
Code:
If Me.bookf & "" = "" Then
  Me.bookf = Me!bookf.ItemData(0)
End If

into the oncurrent event of my form (and as long as my combo 'bookf' is assigned to a field and is in text format) then as soon as the form opens... a new record will be created and the first value in the combo list will automatically be assigned to that field..???? that is what i need to happen..


so this code places the first value of my combo as the default setting..
Code:
Me!ComboName.DefaultValue = Me!ComboName.ItemData(0)
would i even need to use this, or does the first code do this and put that into the respective field on the form..?
 
If you wish the field in your table to have a value assigned, you do not need a default value, which is a suggestion to the user. As for a new record being created, that depends on how you have set up your form, as Data Entry, perhaps.
 
ok, well my forms are all data entry forms based on tables..

I agree, i do not need the suggestion, i simply need that default value to be stored in that field..

so how could i make my form..on open.. create a record and auto populate the chosen field with the first value in the respective combo box??
 
You can remove the default value.

Consider using this code because it was made to test the new record. Plus it is more generic to work in all of your code. You would not be able to use the same if test after the load command. Say for instance if you need to know if its a new or edit record in the beforeupdate event.

Code:
    If (Me.NewRecord) Then
        Me.bookf = Me!bookf.ItemData(0)
    End If
 
so are you sayin that i should have one form for entry that uses the above code in the forms 'oncurrent' event and automatically adds a record..

and i should make a separate form for review that does not automatically add a record when it opens?

 
This code will only fill the field when the record is new. Form_Current gets called everytime you change records in the form. So if they are going to add mulitple records then yes. No extra form is necessary to review, because records that are for edit will not pass the .newrecord test.
 
If you are using a switchboard, you are offered a choice of "Open Form in Add Mode" and "Open Form in Edit Mode". You can also use code (DoCmd.OpenForm) to similar effect. A third method is to set the form's Data Entry property to Yes. If your form(s) are always for data entry, the last method is probably the most suitable; if you have a switchboard, the first method may be best. There are numerous other ways, but these three are a start. It is all about choosing what is best for you and your users.

If you post a few lines about how you see this working, you are likely to get at some suggestions.
 
Antony,
Just to clarify,
Open in Add Mode, opens form to new record
Open in Edit Mode, open form to first record
Open in Data Entry Mode, open form and only create new records
 
ok it works, the bookf auto populates the field..
im tryin to apply this rule to another combo aswell with this code, but the accountchoose doesnt do it

Code:
Private Sub Form_Current()
If (Me.NewRecord) Then
    Me.bookf = Me!bookf.ItemData(0)
    Me.accountchoose = Me!accountchoose.ItemData(0)
End If
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top