[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Default Value Using DMax

Posted on 2010-11-10
13
Medium Priority
?
959 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
13 Comments
 
LVL 58

Assisted Solution

by:cyberkiwi
cyberkiwi earned 668 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 664 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 668 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

656 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