Solved

Default Value Using DMax

Posted on 2010-11-10
13
844 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

762 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