473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Exclude Control Fields in CarryOver Procedure

mjoachim
I am building an Access form and want most of my fields to carry forward automatically when advancing to the next record. I found code on AllenBrowne.com that works well, but am unable to successfully get the call command to exclude fields that I DO NOT want carried forward to the new record.

I have tried to mimic the code that he provided for this purpose:
Call CarryOver(Me, strMsg, "Notes", "EmployeeID")

...and receive the following error message:
"Compile error: Wrong number of arguments or invalid property assignment"

My code that currently works is simply:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeInsert(Cancel As Integer)
  2.     Call CarryOver(Me)
  3. End Sub
I would like this call command to exclude a few fields, including: "Hours", "OtherHours"

Thank you for any assistance!

<< Edit (NeoPa) : This code added later for clarity >>
Expand|Select|Wrap|Line Numbers
  1. Sub CarryOver(frm As Form)
  2. On Error GoTo Err_CarryOver
  3. ' Purpose:  Carry the values over from the last record to a new one.
  4. ' Usage:    In a form's BeforeInsert event procedure, enter:
  5. '               Call CarryOver(Me)
  6. ' Notes:    This example limited to text boxes and combo boxes.
  7. '           Text/combo boxes must have same Name as the fields they represent.
  8. '*****    Dim rst As DAO.Recordset
  9.     Dim ctl As Control
  10.     Dim i As Integer
  11.  
  12.     Set rst = frm.RecordsetClone
  13.     If rst.RecordCount > 0 Then
  14.         rst.MoveLast
  15.         For i = 0 To frm.Count - 1
  16.             Set ctl = frm(i)
  17.             If TypeOf ctl Is TextBox Then
  18.                 If Not IsNull(rst(ctl.Name)) Then
  19.                     ctl = rst(ctl.Name)
  20.                 End If
  21.             ElseIf TypeOf ctl Is ComboBox Then
  22.                 If Not IsNull(rst(ctl.Name)) Then
  23.                     ctl = rst(ctl.Name)
  24.                 End If
  25.             End If
  26.         Next
  27.     End If
  28.  
  29. Exit_CarryOver:
  30.     Set rst = Nothing
  31.     Exit Sub
  32.  
  33. Err_CarryOver:
  34.     Select Case Err
  35.     Case 2448         'Cannot assign a value
  36.         Debug.Print "Value cannot be assigned to " & ctl.Name
  37.         Resume Next
  38.     Case 3265         'Name not found in this collection.
  39.         Debug.Print "No matching field name found for " & ctl.Name
  40.         Resume Next
  41.     Case Else
  42.         MsgBox "Carry-over values were not assigned, from " & ctl.Name & _
  43.             ". Error #" & Err.Number & ": " & Err.Description, vbExclamation, "CarryOver()"
  44.         Resume Exit_CarryOver
  45.     End Select
  46. End Sub
Jul 13 '15 #1

✓ answered by Seth Schrock

You would replace lines 17 through 25 with
Expand|Select|Wrap|Line Numbers
  1. If ctl.Tag = "Transfer" Then
  2.     If Not IsNull(rst(ctl.Name)) Then
  3.         ctl = rst(ctl.Name)
  4.     End If
  5. End If
You would then put Transfer in the Tag property of each control that you want to be transferred.

11 1300
Seth Schrock
2,965 Expert 2GB
On lines 18 and 22 you would need to test to make sure that the control isn't one that you don't want to move over. As an example, say you don't want the controls named txtFirstName and txtLastName moved over:
Expand|Select|Wrap|Line Numbers
  1. If Not IsNull(rst(ctl.Name)) And Not (ctl.Name = txtFirstName Or ctl.Name = txtLastName) Then
Jul 13 '15 #2
Don't the lines "For i = 0 To frm.Count-1" and "Set ctl = frm(i)" automatically cycle through every control on the form?

Should I be using an if statement in the procedure itself to exclude controls that I want skipped? Is there a better way?
Jul 13 '15 #3
Seth Schrock
2,965 Expert 2GB
Yes, line 15 does loop through every control on your form, including labels, buttons, subforms, tab controls, etc. That is why lines 17 and 21 are included so that something happens only if the control is either a textbox or a combobox. Then lines 18 and 22 make sure that there is something in those controls. My added code makes sure that the control name isn't one that you want excluded.
Jul 13 '15 #4
NeoPa
32,556 Expert Mod 16PB
How about setting a value in the .Tag property of each Control that indicates whether or not it needs to be carried over? That way the check is simpler and cleaner.
Jul 13 '15 #5
Seth Schrock
2,965 Expert 2GB
Why didn't I think of that? That would be a much easier method. I have actually done that too. Thanks NeoPa.
Jul 13 '15 #6
Seth: I initially missed the code you added. Once I fixed what I had broken in testing, I was able to confirm that your approach does work. Thanks!

NeoPa: Can you elaborate on your comment? I see that there is a property "Tag" under the Other tab of each control. Just entering a value there does not prevent the control from copying forward. Would use of that property require additional code to have that examined?
Jul 13 '15 #7
Seth Schrock
2,965 Expert 2GB
You would replace lines 17 through 25 with
Expand|Select|Wrap|Line Numbers
  1. If ctl.Tag = "Transfer" Then
  2.     If Not IsNull(rst(ctl.Name)) Then
  3.         ctl = rst(ctl.Name)
  4.     End If
  5. End If
You would then put Transfer in the Tag property of each control that you want to be transferred.
Jul 13 '15 #8
Great! Both excellent solutions. I will swap out my code with the Tag approach.

Thanks!
Jul 13 '15 #9
NeoPa
32,556 Expert Mod 16PB
Seth has done a fine job of illustrating the concept. It could also work as :
Expand|Select|Wrap|Line Numbers
  1. If (ctl.Tag = "Transfer") And (Not IsNull(rst(ctl.Name))) Then _
  2.     ctl = rst(ctl.Name)
I agree with the selection of his post as Best Answer, but I merged the code into the OP in order that it will make immediate sense to anyone reviewing the thread quickly (It refers to code lines from your code).
Jul 13 '15 #10
zmbd
5,501 Expert Mod 4TB
I often use the tag property for other activities, i.e. group security; thus, the tag option doesn't work for me. However, there is an old school method illustrated here using a hidden control and a value list:
How to fill a record with data from a previous record automatically in Access 2000 and 2002 and Office Access 2003
I've been using this method since Acc97 days.

BOL
-z
Jul 15 '15 #11
NeoPa
32,556 Expert Mod 16PB
It's true. The .Tag property is so flexible it can be used for various things. Clearly, only use it in such a way that its needs never overlap, or find a way of ensuring all required needs are met somehow. I sometimes find myself using it for multiple needs by using multiple values separated by commas.

Nevertheless, it's really the concept that we're pushing here. If you understand why to use the .Tag property then you can more easily find alternative ways of handling it when it's not available.
Jul 15 '15 #12

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

Similar topics

3
by: Lynn | last post by:
Hello, I have some user controls on a page...one for the user to fill in name, address, etc., and the other for the user to fill in credit card information. I have built the user controls in...
8
by: tonsam | last post by:
I have a report (monthly inventory) whose record source is based on a query named monthly_inventory_qry. On same query (monthly_inventory_qry), I add two fields (month_1 and month_2)and its sum was...
1
by: bmdlfc | last post by:
Morning all, long time reader, first time poster. I need to manipulate an existing CSV file and create another one from it but excluding some fields. For example, the fields I have in the...
2
by: db2learner | last post by:
Hi, I am new to DB2 and i just started worked on it a couple of days back. I have created basic EMPLOYEE table from control centre which has 2 fields: EmpNo, EmpName. I am trying to write...
1
by: Jeremy | last post by:
I've got a GridView control in an updatepanel. Each row has two controls. one control only affects the display of things inside the update panel, so I want it to be asynchronous, but the other...
10
kcdoell
by: kcdoell | last post by:
Good Morning: I have 12 and 3 . I created the below query: SELECT , , Sum() AS , Sum() AS FROM UqryProductTypeStatic WHERE ((()=..-1) AND (()=..)) GROUP BY , ;
8
by: banderson | last post by:
Hello, I have a combo box in which I want to display multiple fields by concatenating the fields together. If one of those concatenated fields is Null, then the combo box does not show anything. To...
3
by: Sensas | last post by:
Sorry for this noob Huh question, but I am calling images from a database and I don't want to show the empty images. Example webpage here As you can see, there are 7 empty image fields left....
3
by: Shane van Heerden | last post by:
Table Fields: ID, Route, Flight1, NewDate1, Flight2, NewDate2, Surname, TagNumber etc. Form Filter Code: If Not IsNull(Me.txtStartDate) Then strWhere = strWhere & "( >= " &...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.