Solved

Default Value Using DMax

Posted on 2010-11-10
13
846 Views
Last Modified: 2013-11-29
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
0
Comment
Question by:shacho
  • 5
  • 4
  • 2
  • +1
13 Comments
 
LVL 58

Assisted Solution

by:cyberkiwi
cyberkiwi earned 167 total points
ID: 34108383
Is this in a query? That should work, unless it is in VBA?

what does the whole query/code look like around that snippet?
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34108387
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)
0
 
LVL 119

Assisted Solution

by:Rey Obrero
Rey Obrero earned 166 total points
ID: 34108394
where are you getting the value for & [ID] ?

are doing this in a form?

me.txtSequence=nz(DMax("SEQUENCE", "tblEntries", "ID = " & Me.ID) ,0)+1
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34108432
For the default, you can use

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

Author Comment

by:shacho
ID: 34108477
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.
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34108486
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
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 119

Expert Comment

by:Rey Obrero
ID: 34108662
<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







0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34108719
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
0
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Access MVP) earned 167 total points
ID: 34108795
"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."

Do you mean the Default Value property of a field in a table ?  If so, you cannot do that.  You can't use aggregate functions in the Default Value property of a field.  Nor can you reference the value of another field in the table.

mx
0
 

Author Comment

by:shacho
ID: 34108802
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.


0
 

Author Comment

by:shacho
ID: 34108805
Ah - mx's comment answers the question.
0
 
LVL 75
ID: 34108812
"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
0
 

Author Comment

by:shacho
ID: 34108859
well put.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now