473,796 Members | 2,595 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can't get Allen Browne's Carry Over records code to work in a form and subform

1 New Member
I have a form that also contains a subform. I have tried to input Allen Browne's code to have the last records entered on the form to carry over when a new record is added. However, nothing is carrying over. Here is my code.

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Public Function CarryOver(frm As Form, strErrMsg As String, ParamArray avarExceptionList()) As Long
  4. On Error GoTo Err_Handler
  5.     'Purpose: Carry over the same fields to a new record, based on the last record in the form.
  6.     'Arguments: frm               = the form to copy the values on.
  7.     '           strErrMsg         = string to append error messages to.
  8.     '           avarExceptionList = list of control names NOT to copy values over to.
  9.     'Return:    Count of controls that had a value assigned.
  10.     'Usage:     In a form's BeforeInsert event, excluding Surname and City controls:
  11.     '               Call CarryOver(Me, strMsg, "Surname", City")
  12.     Dim rs As DAO.Recordset         'Clone of form.
  13.     Dim ctl As Control              'Each control on form.
  14.     Dim strForm As String           'Name of form (for error handler.)
  15.     Dim strControl As String        'Each control in the loop
  16.     Dim strActiveControl As String  'Name of the active control. Don't assign this as user is typing in it.
  17.     Dim strControlSource As String  'ControlSource property.
  18.     Dim lngI As Long                'Loop counter.
  19.     Dim lngLBound As Long           'Lower bound of exception list array.
  20.     Dim lngUBound As Long           'Upper bound of exception list array.
  21.     Dim bCancel As Boolean          'Flag to cancel this operation.
  22.     Dim bSkip As Boolean            'Flag to skip one control.
  23.     Dim lngKt As Long               'Count of controls assigned.
  24.  
  25.     'Initialize.
  26.     strForm = frm.Name
  27.     strActiveControl = frm.ActiveControl.Name
  28.     lngLBound = LBound(avarExceptionList)
  29.     lngUBound = UBound(avarExceptionList)
  30.  
  31.     'Must not assign values to the form's controls if it is not at a new record.
  32.     If Not frm.NewRecord Then
  33.         bCancel = True
  34.         strErrMsg = strErrMsg & "Cannot carry values over. Form '" & strForm & "' is not at a new record." & vbCrLf
  35.     End If
  36.     'Find the record to copy, checking there is one.
  37.     If Not bCancel Then
  38.         Set rs = frm.RecordsetClone
  39.         If rs.RecordCount <= 0& Then
  40.             bCancel = True
  41.             strErrMsg = strErrMsg & "Cannot carry values over. Form '" & strForm & "' has no recrods." & vbCrLf
  42.         End If
  43.     End If
  44.  
  45.     If Not bCancel Then
  46.         'The last record in the form is the one to copy.
  47.         rs.MoveLast
  48.         'Loop the controls.
  49.         For Each ctl In frm.Controls
  50.             bSkip = False
  51.             strControl = ctl.Name
  52.             'Ignore the active control, those without a ControlSource, and those in the exception list.
  53.             If (strControl <> strActiveControl) And HasProperty(ctl, "ControlSource") Then
  54.                 For lngI = lngLBound To lngUBound
  55.                     If avarExceptionList(lngI) = strControl Then
  56.                         bSkip = True
  57.                         Exit For
  58.                     End If
  59.                 Next
  60.                 If Not bSkip Then
  61.                     'Examine what this control is bound to. Ignore unbound, or bound to an expression.
  62.                     strControlSource = ctl.ControlSource
  63.                     If (strControlSource <> vbNullString) And Not (strControlSource Like "=*") Then
  64.                         'Ignore calculated fields (no SourceTable), autonumber fields, and null values.
  65.                         With rs(strControlSource)
  66.                             If (.SourceTable <> vbNullString) And ((.Attributes And dbAutoIncrField) = 0&) _
  67.                                 And Not (IsCalcTableField(rs(strControlSource)) Or IsNull(.Value)) Then
  68.                                 If ctl.Value = .Value Then
  69.                                     'do nothing. (Skipping this can cause Error 3331.)
  70.                                 Else
  71.                                     ctl.Value = .Value
  72.                                     lngKt = lngKt + 1&
  73.                                 End If
  74.                             End If
  75.                         End With
  76.                     End If
  77.                 End If
  78.             End If
  79.         Next
  80.     End If
  81.  
  82.     CarryOver = lngKt
  83.  
  84. Exit_Handler:
  85.     Set rs = Nothing
  86.     Exit Function
  87.  
  88. Err_Handler:
  89.     strErrMsg = strErrMsg & Err.Description & vbCrLf
  90.     Resume Exit_Handler
  91. End Function
  92.  
  93. Private Function IsCalcTableField(fld As DAO.Field) As Boolean
  94.     'Purpose: Returns True if fld is a calculated field (Access 2010 and later only.)
  95. On Error GoTo ExitHandler
  96.     Dim strExpr As String
  97.  
  98.     strExpr = fld.Properties("Expression")
  99.     If strExpr <> vbNullString Then
  100.         IsCalcTableField = True
  101.     End If
  102.  
  103. ExitHandler:
  104. End Function
  105.  
  106. Public Function HasProperty(obj As Object, strPropName As String) As Boolean
  107.     'Purpose: Return true if the object has the property.
  108.     Dim varDummy As Variant
  109.  
  110.     On Error Resume Next
  111.     varDummy = obj.Properties(strPropName)
  112.     HasProperty = (Err.Number = 0)
  113. End Function
Aug 3 '10 #1
0 1135

Sign in to post your reply or Sign up for a free account.

Similar topics

3
11106
by: Jason | last post by:
I am trying to filter records in a primary form based on records in related tables. The data in the related tables is being displayed in the primary form through subforms. To be more specific, I have a primary form named TestResults, which is connected to data in a table named TestResults. There are basically two other tables that are related to the TestResults table (and the primary form) named Names-Normalized and SiteAddresses. The...
1
2485
by: Mary Ann | last post by:
I have a main form with 4 subforms. One of the subforms reflects other costs/miscellaneous charges that an account might be assessed. The main form includes a field with a total pulled from that other cost subform. The total field reflects the correct amount as long as there is at least one record for the account in the other cost subform. However, many accounts won't have other costs assessed so those accounts are not in the query for...
1
2393
by: Steven Britton via AccessMonster.com | last post by:
Follow Up question to the below posted by Allen Browne on 02/06/2005 - Could something like this work for a form that has a subform and the subform is a datasheet? _______________________________________________________________________ Re: Multiple Sort in a Continuous Form - Similiar to AutoFilter 06 Feb 2005 06:21 Allen Browne
15
3077
by: Jerry Alexander | last post by:
The Northwind Order Entry Application database is great! ----------------------------------------- But one thing is lacking: Real-time Stock Qty calculation! ----------------------------------------- I know that this topic has been discussed over & over in forums, but I have yet to find anyone that has shown the following: ----------------- 1) Linking Allen Browne's code to Northwind? then an 2) Inventory report to show Products &...
3
11971
by: dhowell | last post by:
In reading some of the posts on this group, it appears as though it is not strait forward at all to filter a form, which has subforms, by criteria which are either on subforms or span more than one subform..... This MSDN article however makes it sound as simple as clicking "Filter by Form", entering the search criterial in the form and/or it's subforms, then just clicking "Apply Filter"...... ...
10
3626
by: Thelma Lubkin | last post by:
My form/subform combination that allows user to display 'ColorSet' records, move to other records via a selector, to add,delete, and edit them, and to manage the related 'Color' records from the subform is close to completion, but I still need help... The parent form's recordset has fields string integer COLORNAME CLASSSIZE and a few more that aren't relevant here.
7
3390
by: Swinky | last post by:
Mr. Browne's copy code on his web site has saved me. I have been struggling to copy a record with several related sub-form tables. I found code on his web site that copies a sub-form table, implemented it and was successful to make it work. Thank you Mr. Browne! However, the copy code on his web site only updates one sub-form table. My database has three sub-forms that need to be updated. Although, I am not a programmer (I am...
2
1665
by: sara | last post by:
I use Allen Browne's Audit Trail code in everything I do and I love it. Recently, I've run into a problem I can't figure out. I have a database with about 35 lookup tables. I am creating an "Admin" screen to allow the head social worker to maintain items in the list. (This is a volunteer project for a non-profit that is trying to help elders "age successfully" in their own homes, and I'm trying to help them do some tracking for...
6
9431
by: Dave | last post by:
On my form I have combo boxes. These combo boxes, after updating them, populate respective listboxes that are located below the combo boxes on the same form. I am trying to use a "generate report" button located on my form to print all of the list box values (that have been updated via selection from combo boxes) from the form to the report. I've tried using a macro with the code: Macro Name: cmdGenerateReport : On Click Action:...
6
2743
by: babamc4 | last post by:
I have a main form (mainformlung) with 5 subforms (followupacute, followuplate, biochemresults, haemresults and pftresults). I have copied Allen Browne's Audit Trail code (thanks to Allen Browne) and this is working great, edit, insert etc is working bar when I try to delete a record in one of my subforms (I'm in test stage at the mo) I get a run time error 3022 'The changes you requested to the table where not successful because they would...
0
9679
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10453
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10172
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.