How to Increment Primary key with Dmax before rest of VBA code runs

  • Thread starter adurrschmidt via AccessMonster.com
  • Start date
A

adurrschmidt via AccessMonster.com

Monsters,

I have a simple form with a command button containing code to automatically
create "x" amount of records with the same data for all fields except the
primary key (unique ID). I know that sounds weird but It's something that I
need it to do.

My problem is that I cannot figure out how to get the Dmax() + 1 function to
increment my primary key before the rest of the code runs. As the code
creates new records with the same data, it also seems to be creating the same
primary key over which will not allow the code to finish running and the
"cannot have duplicate values in primary key" error pops up...Is there any
one that can help???

Is there some way to get the primary key to update before each new record is
added?

Option Compare Database

Private Sub Add_Records_Click()
On Error GoTo Err_Add_Records_Click

Dim SampleCounter As Integer
SampleCounter = [Forms]![Frm1]![SampleCounter]

Dim Check, Counter
Check = True: Counter = 0

Do
Do While Counter < SampleCounter
Counter = Counter + 1

'Here is where I thought I could use the Dmax() function to update the
primary key, but so far I've had no luck

DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 2, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 5, , acMenuVer70 'Paste Append

If Counter = SampleCounter Then
Check = False
Exit Do

End If

Loop
Loop Until Check = False

Exit_Add_Records_Click:
Exit Sub

Err_Add_Records_Click:
MsgBox Err.Description
Resume Exit_Add_Records_Click

End Sub
 
A

adurrschmidt via AccessMonster.com

I think I figured it out...I added the following line of code in the loop
after the three "DoCmd" Lines...

[Forms]![frm1]![ID] = [Forms]![frm1]![ID] + 1

Now I am able to specify how many records I want to add, each containing the
same info specified in the desired fields, while maintaining a primary key
that increments...I'm still open for suggestions if anyone knows a smarter
way to do this..

Thank you,
Andrew

Monsters,

I have a simple form with a command button containing code to automatically
create "x" amount of records with the same data for all fields except the
primary key (unique ID). I know that sounds weird but It's something that I
need it to do.

My problem is that I cannot figure out how to get the Dmax() + 1 function to
increment my primary key before the rest of the code runs. As the code
creates new records with the same data, it also seems to be creating the same
primary key over which will not allow the code to finish running and the
"cannot have duplicate values in primary key" error pops up...Is there any
one that can help???

Is there some way to get the primary key to update before each new record is
added?

Option Compare Database

Private Sub Add_Records_Click()
On Error GoTo Err_Add_Records_Click

Dim SampleCounter As Integer
SampleCounter = [Forms]![Frm1]![SampleCounter]

Dim Check, Counter
Check = True: Counter = 0

Do
Do While Counter < SampleCounter
Counter = Counter + 1

'Here is where I thought I could use the Dmax() function to update the
primary key, but so far I've had no luck

DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 2, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 5, , acMenuVer70 'Paste Append

If Counter = SampleCounter Then
Check = False
Exit Do

End If

Loop
Loop Until Check = False

Exit_Add_Records_Click:
Exit Sub

Err_Add_Records_Click:
MsgBox Err.Description
Resume Exit_Add_Records_Click

End Sub
 

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