Generate Autonumber on subform

K

kolsby

I have a subfrom (Mac_subform) that has as its only field an
autonumber.
I would like to click a button on the main form and have the subform
add a record (autonumber).

The code i am trying is:
DoCmd.GoToControl "mac subform"
DoCmd.GoToRecord , , acNewRec

This is not working though.

Any ideas?

thanks,
KO
 
K

kolsby

Do a Save to save the blank record then do a goto new record.

Steve










- Show quoted text -


I tried the followinw with no luck.
DoCmd.GoToControl "mac subform"
DoCmd.Save
DoCmd.GoToRecord , , acNewRec

Is my code right?
thanks,
KO
 
J

John W. Vinson

I tried the followinw with no luck.
DoCmd.GoToControl "mac subform"
DoCmd.Save
DoCmd.GoToRecord , , acNewRec

Is my code right?

Nope. It's confusing - the Save operator doesn't save the record, it saves
*design changes to the Form*.

Replace DoCmd.Save with

If Me.Dirty Then Me.Dirty = False

or with

RunCommand acCmdSaveRecord
 
J

John W. Vinson

Try:
Me![mac subform].SetFocus
Me![mac subform].Form![NameOfAControlOnYourSubform.SetFocus
DoCmd.Save
DoCmd.GoToRecord , , acNewRec

You're mistaken, Steve. DoCmd.Save saves *design changes to the form*, not the
current record. You need eitehr

RunCommand acCmdSaveRecord

or

If Me.Dirty Then Me.Dirty = False

to save the record.
 
K

kolsby

Try:
Me![mac subform].SetFocus
Me![mac subform].Form![NameOfAControlOnYourSubform.SetFocus
DoCmd.Save
DoCmd.GoToRecord , , acNewRec

You're mistaken, Steve. DoCmd.Save saves *design changes to the form*, not the
current record. You need eitehr

RunCommand acCmdSaveRecord

or

If Me.Dirty Then Me.Dirty = False

to save the record.

thanks for the reply John. Unfortunately that still isn't working.
It is not creating the new record.
any other suggestions?
 
J

John W. Vinson

I have a subfrom (Mac_subform) that has as its only field an
autonumber.
I would like to click a button on the main form and have the subform
add a record (autonumber).

The code i am trying is:
DoCmd.GoToControl "mac subform"
DoCmd.GoToRecord , , acNewRec

The autonumber will not be populated unless you dirty the record. And I see NO
point to creating a new record containing just an autonumber value and nothing
else! Let's back up a step: what is the recordsource of "mac subform"? What
information needs to be stored in that table? If the only information that you
want in that table is a meaningless autonumber... *WHY*? What purpose does the
record serve?
 
K

kolsby

Theautonumberwill not be populated unless you dirty the record. And I seeNO
point to creating a new record containing just anautonumbervalue and nothing
else! Let's back up a step: what is the recordsource of "mac subform"? What
information needs to be stored in that table? If the only information that you
want in that table is a meaninglessautonumber... *WHY*? What purpose doesthe
record serve?

The subform is going to create a MAC address that can not be reused.
It is really the only information i need on the subform but can add a
date field if that helps. Each P/N might have several MAC addresses.
hope that helps
 
J

John W. Vinson

Try:
Me![mac subform].SetFocus
Me![mac subform].Form![NameOfAControlOnYourSubform.SetFocus
DoCmd.Save
DoCmd.GoToRecord , , acNewRec

You're mistaken, Steve. DoCmd.Save saves *design changes to the form*, not the
current record. You need eitehr

RunCommand acCmdSaveRecord

or

If Me.Dirty Then Me.Dirty = False

to save the record.

thanks for the reply John. Unfortunately that still isn't working.
It is not creating the new record.
any other suggestions?

I suspect you'll need to actually open a recordset and add a new record.
Perhaps you shouldn't be using an Autonumber at all; you may want to instead
use code to increment a Long Integer ID.
 

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