Link to home
Start Free TrialLog in
Avatar of shacho
shacho

asked on

Default Value Using DMax

I feel like this should be easy but I can't figure it out.

I have a table with these fields:

ID, VALUE, SEQUENCE

I want the Default Value for SEQUENCE to be the max value of SEQUENCE for the same ID, plus 1.
I tried DMax("SEQUENCE", "tblEntries", "ID = " & [ID]) - But Access doesn't like [ID].  Is there a way
to do this?

Mike
SOLUTION
Avatar of cyberkiwi
cyberkiwi
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Since you have it in Access Coding, I suspect it is in straight VBA.
The [ID] references a field in a table, but since there is no table involved, drop the brackets, but give it a variable instead.

ID = 12
nextID = DMax("SEQUENCE", "tblEntries", "ID = " & ID)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
For the default, you can use

("0" & DMax("SEQUENCE", "tblEntries", "ID = " & ID)) + 1
Avatar of shacho
shacho

ASKER

Oops - forgot to clarify.  This is in a table, not a form or in code.  So the point is that the default value is a function of another value of the same record.
You mean query right?
If you multiple tables in the query, you may need to specify where the ID comes from.

Select Id,
("0" & DMax("SEQUENCE", "tblEntries", "ID = " & tbl.ID)) + 1 as NewId
From tbl
<This is in a table, not a form or in code. >

you will get many records returned, each for every ID that you have in the table

SELECT ID, Nz(DMax("SEQUENCE","tblEntries","ID = " & [ID]),0) +1 AS Expr1
FROM tblEntries
GROUP BY ID, Nz(DMax("SEQUENCE","tblEntries","ID = " & [ID]),0) +1;


like

ID    next Seq
1  5
2  7

3  4

so you need to specify what ID number you want to use







Looking at it again, the default value won't work because you will never see the ID in the result set if it is not there, so there is nothing to add 1 to, no records at all.

Really need to see your query
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shacho

ASKER

Seems I still haven't communicated the problem correctly.  And I should not have put this in coding, in retrospect.  Sorry for the confusion.

To be clear...

- ID is a field name in a table, tblEntries, that has three fields: ID, VALUE, and SEQUENCE.
- There is no primary key.
- I am looking at the table definition interface and trying to set a default value for SEQUENCE.
- I want the default value to be the highest SEQUENCE number in the table +1, WHERE ID = the ID of the record in question.

This would work just fine on a form, because the value for ID could be referenced from the control that is bound to it.
But the formula I am using cannot 'see' the value for ID.  Maybe that's just not possible in a table definition.

Now that I think of it, it probably isn't because a new record has a null value for ID until it is entered.


Avatar of shacho

ASKER

Ah - mx's comment answers the question.
"it probably isn't because a new record has a null value for ID until it is entered."
That and other fields as well ... values aren't really committed or 'available' yet ... as in 'see'.  Chicken before the egg.

mx
Avatar of shacho

ASKER

well put.