AutoFillNewRecord

hyousef

Registered User.
Local time
Today, 10:37
Joined
Jan 15, 2002
Messages
41
I want to fill in one field automatically from previous record.

i have done the following but i didn't get what i want, it filled in all fields but i want only one.


Function AutoFillNewRecord(F As Form)
Dim RS As Recordset, C As Control
Dim FillFields As String, FillAllFields As Integer

On Error Resume Next

' Exit if not on the new record.
If Not F.NewRecord Then Exit Function

' Goto the last record of the form recordset (to autofill form).
Set RS = F.RecordsetClone
RS.MoveLast

' Exit if you cannot move to the last record (no records).
If Err <> 0 Then Exit Function

' Get the list of fields to autofill.
FillFields = ";" & F![AutoFillNewRecordFields] & ";"

' If there is no criteria field, then set flag indicating ALL
' fields should be autofilled.
FillAllFields = Err <> 0

F.Painting = False

' Visit each field on the form.
For Each C In F
' Fill the field if ALL fields are to be filled OR if the
' ...ControlSource field can be found in the FillFields list.
If FillALLFields Or _
InStr(FillFields, ";" & (C.Name) & ";") > 0 Then
C = RS(C.ControlSource)
End If
Next

F.Painting = True

End Function

then on form's OnCurrent property i typed:
=AutoFillNewRecord([Forms]![Project])


then i created a text box and typed the following:
Name: AutoFillNewRecordFields
Visible: No
DefaultValue: "LocationID"


LocationID is the field that i need to fill in automatically from a previous record.


Any help.
Thanks in advance.
 
I have used this useful function but not with only one field. On examining the code, I think the value you should enter in the fill fields box should be the name of the control and not the field. Try this and let me know if that is the case.
 
Thanks Fizzio, it works.
 

Users who are viewing this thread

Back
Top Bottom