Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Default Values 1

Status
Not open for further replies.

pianoben

Programmer
May 8, 2006
13
US
On a form (bound to the table 'Passenger')in Data Entry mode, I would like to have a text box bound to the primary key, automatically generate a default value. The primary key is an ID number, which is simply one greater than the previous entry.

I've attempted several things.

I've placed this query

Count(SELECT * FROM Passenger)

into the 'Default Value' field of the box's Properties, but in Form View the string '#Name?' is all that appears. I've futzed around with some VBA code (of which I know next to nothing) and various permutations of the query.

Can someone help me get this Default Value thing going? Thanks!
 
I also tried using a macro that executes when the form opens that attempts to set the value of the field to "(SELECT Count(*) FROM Passenger) + 1". When the form opens, a message informs me that the macro has failed.

I'm suspecting that maybe forms and SQL Aggregate functions don't mix, but I don't know. Any thoughts? Thanks!
 
How about DMax?
=Dmax("IDFieldName","Passengers")+1
 
Never mind, I was able to generate the proper values using a VBA module attached to a button's OnClick event. Code below:

[tt]'Container variables
Dim SQL, temp, p1, p2, p3, p4, p5, p6, p7, p8, c, q As String

Dim
rec As Recordset

Set rec = CurrentDb.OpenRecordset("Passenger", dbOpenDynaset)

Set rec = CurrentDb.OpenRecordset("Select Max(PassID) + 1 AS newID FROM Passenger")

p1 = CStr(rec!newID)[/tt]
 
And what about DMax, which will do the same thing in one line?
 
Maybe I'm a dunce with Access, but Dmax didn't work for me, either. I still get the same erroneous output. Thanks for your fast response, though!
 
How are ya pianoben . . .

Really! . . .

Perhaps if you post the DMax syntax you tried?

Calvin.gif
See Ya! . . . . . .
 
I placed

[tt]DMax([PassID],[Passenger])[/tt]

into the Default Value field of the textbox, which when run displayed the "#Name?" value.
 
You seem to have missed the quotes. Try:
DMax([Red]"[/Red][PassID][Red]"[/Red],[Red]"[/Red][Passenger][Red]"[/Red]) + 1
 
Remou, you're right; I did forget the quotes. It now works just as you said it would. Thanks a ton!
 
And to handle the first addition in an empty table:
Nz(DMax("[PassID]","[Passenger]"),0) + 1

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
so I don't have to put in the first value in by hand!

Pampers [afro]
Just let it go...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top