user5831311
user5831311

Reputation: 273

auto-increment a field in MS Access 2016 form

I'm building a basic form and would like an id field in the form to auto-increment when the user opens the form (or could be a different event as well, just figured this is simplest).

I've written the following vb code, but there seems to be an issue:

Private Sub Form_Load()    

    lngNextID = DMax("[portfolio_id]", "table1") + 1
    Form 1.portfolio_id = lngNextID

End Sub

table1 is the table I want the vb to look up for the next increment. The name of my form is 'Form 1' and the field in that form that I'm looking to autoincrement is portfolio_id.

Any advice/modifications would be helpful, thank you.

Upvotes: 0

Views: 1062

Answers (1)

Gustav
Gustav

Reputation: 55816

Try with:

Forms("Form 1").portfolio_id = lngNextID

or the simpler:

Me!portfolio_id = lngNextID

However, that will update the opening record, so try setting the DefaultValue (a string):

Me!portfolio_id.DefaultValue = "'" & lngNextID "'"

Upvotes: 1

Related Questions