Solved

Default Value Using DMax

Posted on 2010-11-10
13
864 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 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
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 - Microsoft MVP, Access and Data Platform) 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Familiarize people with the process of utilizing SQL Server stored procedures 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 Micr…
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…

790 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