UPDATE ACCT# FIELD AUTOMATICALLY

D

Debbie

I HAVE A TABLE FOR APPLICANT AND ONE FOR A CHILD. THE PRIMARY KEY IS
APPLICANT'S SS#. HOW CAN I HAVE THE CHILD ACCOUNT# AUTOMATICALLY ENTER
THE APPLICANTS SS# PLUS 01 FOR FIRST CHILD, 02 FOR SECOND CHILD, ETC.
I HAVE THE FORMS SETUP, APPLICANT IS MAIN FORM, CHILD IS SUBFORM. I
WANT THE CHILDACCT# TO BE ENTERED AUTOMATICALLY ONCE I ENTER THE CHILDS
NAME. CAN ANY ONE GIVE SOME ADVISE?
 
J

John Vinson

I HAVE A TABLE FOR APPLICANT AND ONE FOR A CHILD. THE PRIMARY KEY IS
APPLICANT'S SS#. HOW CAN I HAVE THE CHILD ACCOUNT# AUTOMATICALLY ENTER
THE APPLICANTS SS# PLUS 01 FOR FIRST CHILD, 02 FOR SECOND CHILD, ETC.
I HAVE THE FORMS SETUP, APPLICANT IS MAIN FORM, CHILD IS SUBFORM. I
WANT THE CHILDACCT# TO BE ENTERED AUTOMATICALLY ONCE I ENTER THE CHILDS
NAME. CAN ANY ONE GIVE SOME ADVISE?

I'd first recommend that the Applicant's SS# is a risky choice of a
primary key. Can you be SURE that every applicant has a Social
Security number - a valid one? Can you (legally) get that number?

Secondly, if there are more records than one for an applicant, then
the SS# *cannot* be the primary key. The PK must be unique within the
table.

I'm *guessing* that have a table of Applicants (PK SS#) related
one-to-many to a table of Children. The PK of this table should
consist of *TWO* fields - SS#, as a foreign key to Applicants; and a
Long Integer field ChildNo. You can concatenate them for display but
don't try to store them in one field.

On your Subform, you can automatically generate the child number by
putting the following VBA code in the Form's BeforeInsert event. Click
the ... icon by the event property, and choose Code Builder:

Private Sub Form_BeforeInsert(Cancel as Integer)
Me!ChildNo = NZ(DMax("[ChildNo]", "[Children]", _
"[SS#] = '" & Me![SS#] & "'")) + 1
End Sub

John W. Vinson[MVP]
 
D

Debbie

This is what I entered in the forms BeforeInsert event. It is all red.
What did I do wrong
ChildNo = ChildIDExt
Children = ChildsFirstName and ChildsLastName
SS# = ApplicantID

Me!ChildIDExt = Nz(DMax("[ChildIDExt]",
"[ChildsFirstName.ChildsLastName]",_
"[ApplicantID}=""
&Me![ApplicantID]&""))+1

The Child Table
ChildIDExt is Long Integer
Default is -100
Format is General

What is "' or ""' Is it a quote and exclamation together

Sorry, I am just a beginner with VBA, but am excited to get help to
learn.
Thank you in advance
 
J

John Vinson

This is what I entered in the forms BeforeInsert event. It is all red.
What did I do wrong

For one thing, the continuation character is a SPACE followed by an
underscore, and you need to put it on every line that you're carrying
onto a new line. It can be tricky going back and forth from Usenet to
VBA - here's my take on it with all lines fitting both, I hope:

Me!ChildIDExt = Nz(DMax("[ChildIDExt]", _
"[Child]", _
"[ApplicantID]=" & Me![ApplicantID]))+1
The Child Table
ChildIDExt is Long Integer
Default is -100
Remove the Default - leave it blank. You will NEVER have a child
number -100; it's pointless (and gets in the way of the NZ() function)
to provide a default.
Format is General
And the format is irrelevant in any case, since you're just treating
it as a number.
What is "' or ""' Is it a quote and exclamation together

I made the (mistaken) assumption that you had an applicant name (a
Text field which requires a quote delimiter) rather than a numeric ID
(which must have NO delimiter).

Another problem was that the second argument of the DMax() function
must be a string containing the *name of the table* from which you
want to look up the maximum value. You had what appeared to be two
fieldnames with a period between them - in brackets - which Access
would not know how to handle.

The way the code works is that it uses DMax() to search the table
Child for records with the ApplicantID found on the (currently open)
form. If it finds such records, it returns the MAXimum value of
ChildIDExt - say if you have children 1 and 2 in the table already, it
will return 2. If there IS no record for this applicantID (you're
entering the first child of this applicant), DMax() will return NULL.

The function NZ() returns the value of its argument - the maximum
child number in this case - if that value is not NULL; if it is null,
the NZ() function returns zero.

Finally, you add 1 to the value returned by NZ to get the next
sequential child number. That value is stored in the ChildIDExt
control on the Form.

John W. Vinson[MVP]
 
J

John Vinson

What is "' or ""' Is it a quote and exclamation together

oops! Went back and reread the thread. If the ApplicantID is a Text
field, containing the Social Security number, you do indeed need to
delimit it by quotemarks. You can use either the singlequote character
' or the doublequote " - but since you're building up the string using
doublequote delimiters, it's hard to put a doublequote inside the
doublequote delimited string; you have to use a double doublequote
(now how's THAT for doubletalk!) So it's simpler just to include a '
mark within the " string, before and after the SS#. Try

Me!ChildIDExt = Nz(DMax("[ChildIDExt]", _
"[Child]", _
"[ApplicantID]='" & Me![ApplicantID] & "'")) + 1


John W. Vinson[MVP]
 

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