PC Review


Reply
Thread Tools Rate Thread

Generate next primary key value

 
 
peterDavey
Guest
Posts: n/a
 
      8th Aug 2005
G'day all,
I'm pretty new to Access (but have a fair amount of Excel VBA experience) so
I suspect this will be the first of many posts.

I'm creating an Access 2000 front end for a SQL Server database using linked
tables. Most of my forms will be used to maintain reference tables. The
problem I'm having is that when I create a new record (e.g. to add a new
department in the department table), I need to run a query to find the
maximum current value in the primary key column so I can use the next value
for the ID of the new record. I can't use the Access AutoNumber data type
because it isn't supported by SQL Server.

I know the SQL I need to run and I assume that it will need to be run by the
Form_BeforeInsert event/sub but I'm just not sure how I run the SQL to get
the value from within this sub.

I'll be very grateful for any assistance.

cheers
peterDavey
Melbourne


 
Reply With Quote
 
 
 
 
=?Utf-8?B?QnJpYW4=?=
Guest
Posts: n/a
 
      8th Aug 2005
In VBA

Where KeyID is the name of a text box or other control bound to the primary
key of the table:

KeyID = DMax("[KeyID]","[TableName]") +1

(Assuming you want to increment by 1).

As you mentioned, be careful where you place this statement. What if one
user opens the form while another user is creating a new record but has not
yet saved it? Whoever saves first will get to keep the ID. The BeforeInsert
event fires when the user types the first character, not when the record is
saved.

If the ID is purely a key and is invisible to the user, you can just create
it in Form_BeforeUpdate for new records.

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Form.NewRecord then KeyID = DMax("[KeyID]","[TableName]") +1
End Sub

If the user needs to see the key, then put a MsgBox in Form_AfterInsert that
shows the number to the user after the save.

"peterDavey" wrote:

> G'day all,
> I'm pretty new to Access (but have a fair amount of Excel VBA experience) so
> I suspect this will be the first of many posts.
>
> I'm creating an Access 2000 front end for a SQL Server database using linked
> tables. Most of my forms will be used to maintain reference tables. The
> problem I'm having is that when I create a new record (e.g. to add a new
> department in the department table), I need to run a query to find the
> maximum current value in the primary key column so I can use the next value
> for the ID of the new record. I can't use the Access AutoNumber data type
> because it isn't supported by SQL Server.
>
> I know the SQL I need to run and I assume that it will need to be run by the
> Form_BeforeInsert event/sub but I'm just not sure how I run the SQL to get
> the value from within this sub.
>
> I'll be very grateful for any assistance.
>
> cheers
> peterDavey
> Melbourne
>
>
>

 
Reply With Quote
 
 
 
 
John Griffiths
Guest
Posts: n/a
 
      8th Aug 2005
You could also look at SQLServer identity columns.

I think you would have to ask further questions to
resolve lots of interesting gotchas.

Regards John

"peterDavey" <(E-Mail Removed)> wrote in message
news:eE8ogR$(E-Mail Removed)...
> G'day all,
> I'm pretty new to Access (but have a fair amount of Excel VBA experience)

so
> I suspect this will be the first of many posts.
>
> I'm creating an Access 2000 front end for a SQL Server database using

linked
> tables. Most of my forms will be used to maintain reference tables. The
> problem I'm having is that when I create a new record (e.g. to add a new
> department in the department table), I need to run a query to find the
> maximum current value in the primary key column so I can use the next

value
> for the ID of the new record. I can't use the Access AutoNumber data type
> because it isn't supported by SQL Server.
>
> I know the SQL I need to run and I assume that it will need to be run by

the
> Form_BeforeInsert event/sub but I'm just not sure how I run the SQL to get
> the value from within this sub.
>
> I'll be very grateful for any assistance.
>
> cheers
> peterDavey
> Melbourne
>
>



 
Reply With Quote
 
peterDavey
Guest
Posts: n/a
 
      8th Aug 2005
Thanks John but I'm not too keen on the SQL server identity columns.
cheers
peterD

"John Griffiths" <(E-Mail Removed)> wrote in message
news:dd8cm2$ulb$(E-Mail Removed)...
> You could also look at SQLServer identity columns.
>
> I think you would have to ask further questions to
> resolve lots of interesting gotchas.
>
> Regards John
>
> "peterDavey" <(E-Mail Removed)> wrote in message
> news:eE8ogR$(E-Mail Removed)...
> > G'day all,
> > I'm pretty new to Access (but have a fair amount of Excel VBA

experience)
> so
> > I suspect this will be the first of many posts.
> >
> > I'm creating an Access 2000 front end for a SQL Server database using

> linked
> > tables. Most of my forms will be used to maintain reference tables.

The
> > problem I'm having is that when I create a new record (e.g. to add a new
> > department in the department table), I need to run a query to find the
> > maximum current value in the primary key column so I can use the next

> value
> > for the ID of the new record. I can't use the Access AutoNumber data

type
> > because it isn't supported by SQL Server.
> >
> > I know the SQL I need to run and I assume that it will need to be run by

> the
> > Form_BeforeInsert event/sub but I'm just not sure how I run the SQL to

get
> > the value from within this sub.
> >
> > I'll be very grateful for any assistance.
> >
> > cheers
> > peterDavey
> > Melbourne
> >
> >

>
>



 
Reply With Quote
 
peterDavey
Guest
Posts: n/a
 
      8th Aug 2005
Thanks Brian, this looks like just the ticket. The multi-user issues won't
be a problem because the forms will used by only a couple of people.
cheers
peterD


"Brian" <(E-Mail Removed)> wrote in message
news:10835F4F-7981-4B2F-B767-(E-Mail Removed)...
> In VBA
>
> Where KeyID is the name of a text box or other control bound to the

primary
> key of the table:
>
> KeyID = DMax("[KeyID]","[TableName]") +1
>
> (Assuming you want to increment by 1).
>
> As you mentioned, be careful where you place this statement. What if one
> user opens the form while another user is creating a new record but has

not
> yet saved it? Whoever saves first will get to keep the ID. The

BeforeInsert
> event fires when the user types the first character, not when the record

is
> saved.
>
> If the ID is purely a key and is invisible to the user, you can just

create
> it in Form_BeforeUpdate for new records.
>
> Private Sub Form_BeforeUpdate(Cancel As Integer)
> If Form.NewRecord then KeyID = DMax("[KeyID]","[TableName]") +1
> End Sub
>
> If the user needs to see the key, then put a MsgBox in Form_AfterInsert

that
> shows the number to the user after the save.
>
> "peterDavey" wrote:
>
> > G'day all,
> > I'm pretty new to Access (but have a fair amount of Excel VBA

experience) so
> > I suspect this will be the first of many posts.
> >
> > I'm creating an Access 2000 front end for a SQL Server database using

linked
> > tables. Most of my forms will be used to maintain reference tables.

The
> > problem I'm having is that when I create a new record (e.g. to add a new
> > department in the department table), I need to run a query to find the
> > maximum current value in the primary key column so I can use the next

value
> > for the ID of the new record. I can't use the Access AutoNumber data

type
> > because it isn't supported by SQL Server.
> >
> > I know the SQL I need to run and I assume that it will need to be run by

the
> > Form_BeforeInsert event/sub but I'm just not sure how I run the SQL to

get
> > the value from within this sub.
> >
> > I'll be very grateful for any assistance.
> >
> > cheers
> > peterDavey
> > Melbourne
> >
> >
> >



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Do I have to set the primary key to include primary key fields? boooney Microsoft Access Database Table Design 3 15th Apr 2010 12:17 AM
Data Primary key vs. Artificial (Autonumber) primary key M. Microsoft Access Database Table Design 78 14th Dec 2007 10:10 AM
Re: How to build a funciton to return primary key,alternate key and foreign key fields Allen Browne Microsoft Access VBA Modules 0 18th Jul 2005 01:51 PM
How to build a funciton to return primary key,alternate key and foreign key fields tonyck Microsoft Access VBA Modules 0 18th Jul 2005 01:07 PM
Cannot add primary key constraint since primary key is already set for the table Microsoft VB .NET 1 3rd Dec 2003 02:14 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:23 AM.