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

Auto-fill code, need HELP

Status
Not open for further replies.

nicolea

Instructor
Jan 21, 2004
9
US
I have a form that will need to auto fill certain fields when the next record is entered. I got some code from another user, but it is kind of confusing the way the code is written. I understand everything except, how do you determine what fields will auto fill. This code is in a module. If there is an easier way pleas help. Thank you in advance.

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 = &quot;;&quot; & F![AutoFillNewRecordFields] & &quot;;&quot;

'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 cn be found inthe FillFields list.
If FillAllFields Or InStr(FillFields, &quot;;&quot; & (C.Name) & &quot;;&quot;) > 0 Then
C = RS(C.ControlSource)
End If
Next

F.Painting = True

End Function
 
If you cerate a New record button withthe wizard and then add this code.
Add the new stuff in [red]RED[/color] where shown

Private Sub Command0_Click()
On Error GoTo Err_Command0_Click


DoCmd.GoToRecord , , acNewRec[red]
Me![field1ToAutoFill] = Somevalue
Me![field2ToAutoFill] = Some2ndvalue
Me![field3ToAutoFill] = Some3rdvalue
etc
me![fieldToStartKeyingIntoFirst].setfocus
[/color]
Exit_Command0_Click:
Exit Sub

Err_Command0_Click:
MsgBox Err.Description
Resume Exit_Command0_Click

End Sub



DougP, MCP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top