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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Default value from the previous entry

Status
Not open for further replies.

Fabioedl

Programmer
Feb 18, 2004
6
US
Hi all,

Is there a way to set a default value in a field from the same field from the previous entry?

Thanks,

Fabioedl
 
Not as you have stated the intent.

Relational databases do not support the notion of "Previous" or "Next" for records except in the context of an ORDER BY clause. You can of course, write some code that looks up the "previous" record using some criteria like an autonumber or a date stamp and then drop the value of the field in question into the corresponding value in a new record.
 
Ok Golom,

So, how do I retrieve a data from the table using something like FieldData[autonumber] = FieldData[autonumber-1]?
It might be a dumb question but I don't have any experience in Access.

Thanks anyway,

Fabioedl
 
Data entry should be done in a form. You can set the default value of a text box to the value most recently entered (since the form was opened) by using code in the After Update event of the text box. For instance if you have a text box "txtCity" and want it to default to the value just entered, set the after update event code of the text box to:
Me.txtCity.DefaultValue = """" & Me.txtCity & """"
If the field is numeric, you can remove all the quotes.
Once the form is closed, the default value dissappears.

When entering data, you can also use Ctrl+' to automatically enter the previous value.

Duane
MS Access MVP
Find out how to get great answers faq219-2884.
 
Here's some pseudo-code for one way to do it using DAO (ADO has a bit different syntax for opening recordsets.)
[tt]
Dim SaveField As Variant
Dim ro As DAO.Recordset
Dim rs As DAO.Recordset

' Get the value of the field from the last record.
Set ro = Currentdb.OpenRecordset ( _
"Select * From tbl Where AutoNumFld = " & _
"(Select MAX(AutonumFld) From tbl)" )
SaveField = ro.Fields("DefaultFld").Value
Set ro = Nothing

' Now Add a record to the Table
Set rs = Currentdb.OpenRecordset ("tbl")
rs.AddNew
rs.Fields("DefaultFld").Value = SaveField
rs.Update
[/tt]
Your desire to implement

FieldData[autonumber] = FieldData[autonumber-1]

has some problems. First, it's structured in much the same way that an array subscript is structured and that's not supported in SQL (i.e. it implies that FieldData[autonumber-1] will access a different record than the current one and that doesn't happen.)

Second ... there's no assurance that autonumber-1 exists where autonumber is the number being generated for the new record. If, for example, a record was added and then deleted, then the next record added will skip a number in the autonumber sequence. Autonumbers are (in the absence of any manipulation) the number of records that have been added to the table since it was created ... NOT adjusted for any that may have been deleted.

All that said, it is possible (I believe) to fabricate an INSERT INTO statement that does what you want.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top