views:

78

answers:

2

i have a front end in access and back end is sql server 2008

one of the fields is the account number and here are the rules

it is a zipcode like 92111 plus a dash plus a number.

so the first one would be 92111-1, the second 92111-2

this has to do with how many clients we have in the zip code

i would like this zip code to be automatically generated. here is what i need:

  1. the user enters the zip code
  2. i have a stored procedure that checks if this zip code exists already, to increment it: if 92111-4 exists already, then make it 92111-5.

what is the proper way of doing this?

+2  A: 

If you're storing both the zip and the client sequence number in a single account number field, you would have to split them apart to figure out the next sequence number in a given zip code.

It should be simpler to store them in 2 fields:

zipcode   sequence_num
92111     4
92111     5

Then derive your account number field with a query whenever you need it.

SELECT zipcode & "-" & sequence_num AS acct_num
FROM YourTable;

Then when you need to determine the next sequence_num, lngNextSequenceNum, within a given zipcode, pZip:

lngNextSequenceNum = DMax("sequence_num", "YourTable", "zipcode = " & pZip) +1

That approach can work fine for a single user application. If your application is multi-user, you need something more refined. However, that requirement exists whether you store "account number" as a single field or split it between two fields.

See Create and Use Flexible AutoNumber Fields for a multi-user approach.

HansUp
@hansup: thank you very much. why do i need a diff approach for multiuser?
i am a girl
You don't want a user to enter a zip code, get an account number, adn then go to lunch before saving the account record. The odds the next person creates a new account with the same number is likely.
Jeff O
@jenny What Jeff said! I was gonna say so the users don't stomp on each other's changes, but Jeff said it better.
HansUp
@hansup: thank you very much. cant i just have access automatically save every minute or so?
i am a girl
Maybe, but I wouldn't do it that way. If two users are adding accounts in the same zip code, creating a conflict could happen instantly. BTW, I just realized you said the table is in SQL Server. In that case, you might consider a stored procedure, or insert trigger, or something. I was thinking strictly in Access terms.
HansUp
@hansup: how would i do this with a stored procedure or insert trigger?
i am a girl
Unfortunately I can't help you there. My guess is it can be done with T-SQL ... but not by me.
HansUp
@hansup: you seem to be the #2 expert on SOF on access DBs, surely a trigger isnt anything too difficult for you to come up with?
i am a girl
Are you joking, @jenny? I don't claim #2, but thanks. I do OK with Access. But an SQL Server trigger would be T-SQL, and I don't use that often.
HansUp
@hansup: ive seen your responses. we both know you are at least #2 after fenton
i am a girl
@jenny Why do you keep changing your name?
HansUp
@hansup to keep you on your toes
i am a girl
I don't know how to tell you to write a SQL Server trigger or SPROC, either. I'm no SQL Server expert at all, and have never claimed to be. My apps that use it are all upsized from Jet, and I keep it as simple as possible, and in none of my apps have I been required to move functions into SPROCs. I could probably marginally improve performance in a few cases, but I haven't found it to have enough bang for the buck in the cases I've been working with personally.
David-W-Fenton
+1  A: 

I agree with HansUp that you should keep the ZIP and sequence separated. You can create an indexed computed field called AccountNumber which joins the zip and sequence.

The only way I know of doing this is locking the table, selecting max(sequence) ... where ZIP = 12345, inserting the new record, then unlocking the table. However, locking the table means everyone else has to wait, greatly affecting scalability. I can't recommend this, but I don't have another solution.

Nelson