I'm not so smart

H

Humble David

I pulled this code from Allen Browne's web site. It
copies the values of text boxes and combo boxes in a
form. I am in "data entry" mode where users will enter
multiple records at one time (from a form). I want to
copy the value of user_id from one record to another as
they enter.
Here is the code... Can someone PLEASE explain this to
me in ENGLISH?

Do I just change the i to user_id?

http://members.iinet.net.au/~allenbrowne/ser-24.html

----- partial copy of code written by MVP Allen Browne
Sub CarryOver(frm As Form)
On Error GoTo Err_CarryOver
' Purpose: Carry the values over from the last record to
a new one.
' Usage: In a form's BeforeInsert event procedure,
enter:
' Call CarryOver(Me)
' Notes: This example limited to text boxes and combo
boxes.
' Text/combo boxes must have same Name as the
fields they represent.
Dim rst As DAO.Recordset
Dim ctl As Control
Dim i As Integer

Set rst = frm.RecordsetClone
If rst.RecordCount > 0 Then
rst.MoveLast
For i = 0 To frm.count - 1
Set ctl = frm(i)
If TypeOf ctl Is TextBox Then
If Not IsNull(rst(ctl.Name)) Then
ctl = rst(ctl.Name)
End If
ElseIf TypeOf ctl Is ComboBox Then
If Not IsNull(rst(ctl.Name)) Then
ctl = rst(ctl.Name)
End If
End If
Next
End If
 
J

John Vinson

I pulled this code from Allen Browne's web site. It
copies the values of text boxes and combo boxes in a
form. I am in "data entry" mode where users will enter
multiple records at one time (from a form). I want to
copy the value of user_id from one record to another as
they enter.
Here is the code... Can someone PLEASE explain this to
me in ENGLISH?

Do I just change the i to user_id?

No. i is just a loop variable.

The code as written goes through ALL of the textboxes and combo boxes
in the entire Form (not just one of them). I'll comment the code on
the following line...
http://members.iinet.net.au/~allenbrowne/ser-24.html

----- partial copy of code written by MVP Allen Browne
Sub CarryOver(frm As Form)
On Error GoTo Err_CarryOver
If the code encounters an error, execution will jump to the line
labeled Err_CarryOver.
' Purpose: Carry the values over from the last record to
a new one.
' Usage: In a form's BeforeInsert event procedure,
enter:
' Call CarryOver(Me)
' Notes: This example limited to text boxes and combo
boxes.
' Text/combo boxes must have same Name as the
fields they represent.
Dim rst As DAO.Recordset
Reserve a variable named rst for a Recordset object
Dim ctl As Control
reserve a variable named ctl for a Control object (e.g. a textbox)
Dim i As Integer
reserve a variable named i as an Integer
Set rst = frm.RecordsetClone
Set the variable rst to the current form's RecordsetClone property,
i.e. the set of records available for display on the Form
If rst.RecordCount > 0 Then
Check to see if the form actually HAS at least one record
rst.MoveLast
If so, move to the last (most recently entered) record
For i = 0 To frm.count - 1
Loop through all the Controls on the form, starting from the last and
looping backwards. Me.Controls(i) will be a pointer to the i'th
control.
Set ctl = frm(i)
Set the variable ctl to the Control object in the i'th position
If TypeOf ctl Is TextBox Then Is this control a Textbox?
If Not IsNull(rst(ctl.Name)) Then
And does it have a Name property? (a bit odd, all controls do)
ctl = rst(ctl.Name)
Assuming (as in the comment above) that the name of the control is
identical to the name of the field displayed in that control, set the
default Value property of the control to the value of that field in
the Form's RecordsetClone
End If
ElseIf TypeOf ctl Is ComboBox Then
Similar logic for Combo Box controls
If Not IsNull(rst(ctl.Name)) Then
ctl = rst(ctl.Name)
End If
End If
If the control is neither a textbox nor a combo box, don't do anything
- just loop to the next control
Next
End If

Hope this helps!

It may be simpler to JUST carry over the User_ID: you could use the
following code in the User_ID control's AfterUpdate event:

Private Sub User_ID_AfterUpdate()
' this code will run when the user enters a value into the UserID
' control. If that control is named, say, txtUser_ID, change the
' name as appropriate.
Me!User_ID.DefaultValue = Chr(34) & Me!User_ID & Chr(34)
' Set the DefaultValue property of the control to a text string
' delimited by " - Chr(34) - containing whatever the user entered.
' When the user moves to the new record, it will now default to
' whatever they last typed.
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top