Avatar billede solopolo Nybegynder
10. juni 2002 - 19:47 Der er 13 kommentarer og
1 løsning

Autofill last value from the same field

Does anyone know how can I set a field's default value in table properties or in form properties so that while entering new records it retrieves the value from the last record? But it should be editable, that is, even though I gets the value from the last record but can enter a new value, if required.
Please help me!!!! Thanks.
SoloPOLO!
Avatar billede pato Nybegynder
10. juni 2002 - 19:53 #1
do you have a recordcounter/autonumber on your records?
Avatar billede solopolo Nybegynder
10. juni 2002 - 19:57 #2
Nope. No autonumber fields.
10. juni 2002 - 20:04 #3
Hi Solopolo,

On the forms OnCurrent-event, you place this code:

Private Sub Form_Current()
  Dim rs As DAO.Recordset
  If Me.NewRecord Then
      Set rs = Me.RecordsetClone
      rs.MoveLast
      Me![YourField].DefaultValue = "'" & rs![YourField] & "'"
      rs.Close
  End If
End Sub
10. juni 2002 - 20:04 #4
The code requires a reference to Microsoft DAO 3.x Object Library, if you don't use Access 97.
Avatar billede pato Nybegynder
10. juni 2002 - 20:20 #5
If you do then put this code on the forms oncurrent event

Private Sub Form_Current()
Dim tempvalue As String

If Me.NewRecord = True Then
    ' get last value
    tempvalue = DLookup("[yourtextfieldinthetable]", "yourtabelname", "[theidfield] =" & DMax("[theidfield]", "yourtabelname"))
    Me.txtfieldname.Value = tempvalue
Else
End If

End Sub
Avatar billede pato Nybegynder
10. juni 2002 - 20:25 #6
or just place this code in the defaultvalue parameter on field in the form.

=DLookUp("[name]";"test";"[id] =" & DMax("[ID]";"test"))

thats all

pato
Avatar billede fdata Forsker
10. juni 2002 - 20:25 #7
The easiest way must be:

Private Sub Felt1_AfterUpdate()
  Me.Felt1.DefaultValue = Me.Felt1
End Sub
Avatar billede solopolo Nybegynder
10. juni 2002 - 20:27 #8
I'm using Access 2000 & not familier with refering to Microsoft DAO 3.x Object Library. How do I do that?
Avatar billede pato Nybegynder
10. juni 2002 - 20:27 #9
[name] is the fieldname from witch you want the last value
"test" is the tablename
[id] is the autonumerfield in the table
Avatar billede fdata Forsker
10. juni 2002 - 20:30 #10
If "the last value" means the one you just entered in the previous record you don't need to look it up in any table. All you have to do is save it as a new default value.
Avatar billede solopolo Nybegynder
10. juni 2002 - 20:36 #11
Thanks a lot everyone ... I really appreciate your help ... I just tried pato's solution & it is simple & working fine. So thanks to you pato.
Avatar billede pato Nybegynder
10. juni 2002 - 20:40 #12
anytime :-)
Avatar billede terry Ekspert
10. juni 2002 - 21:25 #13
solopolo>You said there was NO autonumber field, how can you get the DMAX from an autonumber if there IS NO autonumber?
Avatar billede solopolo Nybegynder
10. juni 2002 - 22:11 #14
terry> I just created one
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Dyk ned i databasernes verden på et af vores praksisnære Access-kurser

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester