PC Review


Reply
Thread Tools Rate Thread

Autonumber without a autonumber field

 
 
PennyB
Guest
Posts: n/a
 
      3rd Jul 2009

I have two tables one is

tblProjectsMain - primary key is an autonumber field with no duplicates

table 2

tblPermitMain - is has job number and an indexed key for PermitTaskNumber

I would love to have the permit task number be an autonumber, but the way
the company is handling their permits I can't have it that way.

So for every job number in the main table, there can be many permits.

What I would like to do is write code or something that will auto fill the
next available permit number in for the end user every time they want to
enter a new permit. In other words autonumber for the next permit as follows:

Job Number 1 and Permit task number 1
Job Number 1 and Permit task number 2
Job Number 2 and Permit task number 1
and so on

Hope this makes sense. Any help would be great.

Thanks,

PennyB
 
Reply With Quote
 
 
 
 
Allen Browne
Guest
Posts: n/a
 
      3rd Jul 2009

Use DMax() to get the highest permit task number for the job.

I suggest you do this in the BeforeUpdate event of the form (i.e. the
subform where the jobs are added to the project.) This is the last possible
moment before the record is saved, which reduces the chance that 2 users
will be given the same number at the same time.

Something like this:

Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strWhere As String
Dim varResult As Variant

'do any checking you need to first.
If Me.NewRecord And Not Cancel Then
strWhere = "[JobNumber] = " & Me.[JobNumber]
varResult = DMax("[PermitTaskNumber]", "tblPermitMain", strWhere)
Me.[PermitTaskNumber] = Nz(varResult, 0) + 1
End If
End Sub
--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"PennyB" <(E-Mail Removed)> wrote in message
news:1B5FC132-F890-4740-96C0-(E-Mail Removed)...
>I have two tables one is
>
> tblProjectsMain - primary key is an autonumber field with no duplicates
>
> table 2
>
> tblPermitMain - is has job number and an indexed key for PermitTaskNumber
>
> I would love to have the permit task number be an autonumber, but the way
> the company is handling their permits I can't have it that way.
>
> So for every job number in the main table, there can be many permits.
>
> What I would like to do is write code or something that will auto fill the
> next available permit number in for the end user every time they want to
> enter a new permit. In other words autonumber for the next permit as
> follows:
>
> Job Number 1 and Permit task number 1
> Job Number 1 and Permit task number 2
> Job Number 2 and Permit task number 1
> and so on
>
> Hope this makes sense. Any help would be great.
>
> Thanks,
>
> PennyB


 
Reply With Quote
 
PennyB
Guest
Posts: n/a
 
      3rd Jul 2009

It didn't work on before update, but it works perfect on current.

Thank you for your help!



"PennyB" wrote:

> I have two tables one is
>
> tblProjectsMain - primary key is an autonumber field with no duplicates
>
> table 2
>
> tblPermitMain - is has job number and an indexed key for PermitTaskNumber
>
> I would love to have the permit task number be an autonumber, but the way
> the company is handling their permits I can't have it that way.
>
> So for every job number in the main table, there can be many permits.
>
> What I would like to do is write code or something that will auto fill the
> next available permit number in for the end user every time they want to
> enter a new permit. In other words autonumber for the next permit as follows:
>
> Job Number 1 and Permit task number 1
> Job Number 1 and Permit task number 2
> Job Number 2 and Permit task number 1
> and so on
>
> Hope this makes sense. Any help would be great.
>
> Thanks,
>
> PennyB

 
Reply With Quote
 
Allen Browne
Guest
Posts: n/a
 
      3rd Jul 2009
It would be better to use Form_BeforeInsert rather than Current.

But Form_BeforeUpdate could prevent the multiuser problem (duplicates
assigned.)

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.
"PennyB" <(E-Mail Removed)> wrote in message
news:ED7096D6-CE96-4511-AC1E-(E-Mail Removed)...
> It didn't work on before update, but it works perfect on current.
>
> Thank you for your help!
>
>
>
> "PennyB" wrote:
>
>> I have two tables one is
>>
>> tblProjectsMain - primary key is an autonumber field with no duplicates
>>
>> table 2
>>
>> tblPermitMain - is has job number and an indexed key for PermitTaskNumber
>>
>> I would love to have the permit task number be an autonumber, but the way
>> the company is handling their permits I can't have it that way.
>>
>> So for every job number in the main table, there can be many permits.
>>
>> What I would like to do is write code or something that will auto fill
>> the
>> next available permit number in for the end user every time they want to
>> enter a new permit. In other words autonumber for the next permit as
>> follows:
>>
>> Job Number 1 and Permit task number 1
>> Job Number 1 and Permit task number 2
>> Job Number 2 and Permit task number 1
>> and so on
>>
>> Hope this makes sense. Any help would be great.
>>
>> Thanks,
>>
>> PennyB


 
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
"autonumber" based on subset of "autonumber" field SteveP. Microsoft Access Form Coding 5 2nd Mar 2010 03:31 PM
Create a Sub Autonumber that re-assigns autonumber to each data groupin a sorted field. Junwenren Microsoft Access 1 4th Oct 2008 05:30 AM
Adding autonumber to a non autonumber field don Microsoft Access 2 22nd Apr 2008 05:00 PM
code to autonumber form ID without using autonumber datatype reservedbcreater Microsoft Access Form Coding 4 27th Jan 2005 03:56 PM
AutoNumber field does not contain "(AutoNumber)" when adding a record Keith Microsoft Access Forms 3 12th Nov 2003 04:50 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:50 PM.