PC Review


Reply
 
 
New TO Access
Guest
Posts: n/a
 
      25th Sep 2008
I found this great code for carrying over records from one form to another
(from Allen Browne). This has worked really well until I applied it to a form
with a sub-form. When I run it the sub-form does not appear in the new
record. Would appreciate some assistance with this.

The code is:

Public Function CarryOver(frm As Form, strErrMsg As String, ParamArray
avarExceptionList()) As Long
On Error GoTo Err_Handler
'Purpose: Carry over the same fields to a new record, based on the last
record in the form.
'Arguments: frm = the form to copy the values on.
' strErrMsg = string to append error messages to.
' avarExceptionList = list of control names NOT to copy values
over to.
'Return: Count of controls that had a value assigned.
'Usage: In a form's BeforeInsert event, excluding Surname and City
controls:
' Call CarryOver(Me, strMsg, "Surname", City")
Dim rs As DAO.Recordset 'Clone of form.
Dim ctl As Control 'Each control on form.
Dim strForm As String 'Name of form (for error handler.)
Dim strControl As String 'Each control in the loop
Dim strActiveControl As String 'Name of the active control. Don't
assign this as user is typing in it.
Dim strControlSource As String 'ControlSource property.
Dim lngI As Long 'Loop counter.
Dim lngLBound As Long 'Lower bound of exception list array.
Dim lngUBound As Long 'Upper bound of exception list array.
Dim bCancel As Boolean 'Flag to cancel this operation.
Dim bSkip As Boolean 'Flag to skip one control.
Dim lngKt As Long 'Count of controls assigned.

'Initialize.
strForm = frm.Name
strActiveControl = frm.ActiveControl.Name
lngLBound = LBound(avarExceptionList)
lngUBound = UBound(avarExceptionList)

'Must not assign values to the form's controls if it is not at a new
record.
If Not frm.NewRecord Then
bCancel = True
strErrMsg = strErrMsg & "Cannot carry values over. Form '" & strForm
& "' is not at a new record." & vbCrLf
End If
'Find the record to copy, checking there is one.
If Not bCancel Then
Set rs = frm.RecordsetClone
If rs.RecordCount <= 0& Then
bCancel = True
strErrMsg = strErrMsg & "Cannot carry values over. Form '" &
strForm & "' has no recrods." & vbCrLf
End If
End If

If Not bCancel Then
'The last record in the form is the one to copy.
rs.MoveLast
'Loop the controls.
For Each ctl In frm.Controls
bSkip = False
strControl = ctl.Name
'Ignore the active control, those without a ControlSource, and
those in the exception list.
If (strControl <> strActiveControl) And HasProperty(ctl,
"ControlSource") Then
For lngI = lngLBound To lngUBound
If avarExceptionList(lngI) = strControl Then
bSkip = True
Exit For
End If
Next
If Not bSkip Then
'Examine what this control is bound to. Ignore unbound,
or bound to an expression.
strControlSource = ctl.ControlSource
If (strControlSource <> vbNullString) And Not
(strControlSource Like "=*") Then
'Ignore calculated fields (no SourceTable),
autonumber fields, and null values.
With rs(strControlSource)
If (.SourceTable <> vbNullString) And
((.Attributes And dbAutoIncrField) = 0&) _
And Not IsNull(.Value) Then
If ctl.Value = .Value Then
'do nothing. (Skipping this can cause
Error 3331.)
Else
ctl.Value = .Value
lngKt = lngKt + 1&
End If
End If
End With
End If
End If
End If
Next
End If

CarryOver = lngKt

Exit_Handler:
Set rs = Nothing
Exit Function

Err_Handler:
strErrMsg = strErrMsg & Err.Description & vbCrLf
Resume Exit_Handler
End Function

Public Function HasProperty(obj As Object, strPropName As String) As Boolean
'Purpose: Return true if the object has the property.
Dim varDummy As Variant

On Error Resume Next
varDummy = obj.Properties(strPropName)
HasProperty = (Err.Number = 0)
End Function
 
Reply With Quote
 
 
 
 
Allen Browne
Guest
Posts: n/a
 
      25th Sep 2008
If we are discussing this code:
http://allenbrowne.com/ser-24.html
you can use it in the subform also.

The subform has its own BeforeInsert event.
You will need use that as well.

That is, follow setps 5 to 10 for the subform itself.

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"New TO Access" <(E-Mail Removed)> wrote in message
news:390C1936-9BD5-4EF8-8EA0-(E-Mail Removed)...
>I found this great code for carrying over records from one form to another
> (from Allen Browne). This has worked really well until I applied it to a
> form
> with a sub-form. When I run it the sub-form does not appear in the new
> record. Would appreciate some assistance with this.


 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with carry over a value =?Utf-8?B?QWw=?= Microsoft Access Form Coding 7 5th Oct 2006 09:18 PM
Carry-Over =?Utf-8?B?YmxhZGVsb2Nr?= Microsoft Access Forms 2 30th Mar 2005 02:26 PM
auto-fill, set carry to, set carry on etc vw Microsoft Access Forms 1 4th Sep 2004 07:29 AM
carry a value Microsoft Access VBA Modules 2 20th May 2004 08:31 PM
carry over s Microsoft Excel Discussion 4 8th Mar 2004 08:44 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:22 AM.