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: - Private Sub Form_BeforeInsert(Cancel As Integer)
-
Call CarryOver(Me)
-
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 >> - 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
-
-
Exit_CarryOver:
-
Set rst = Nothing
-
Exit Sub
-
-
Err_CarryOver:
-
Select Case Err
-
Case 2448 'Cannot assign a value
-
Debug.Print "Value cannot be assigned to " & ctl.Name
-
Resume Next
-
Case 3265 'Name not found in this collection.
-
Debug.Print "No matching field name found for " & ctl.Name
-
Resume Next
-
Case Else
-
MsgBox "Carry-over values were not assigned, from " & ctl.Name & _
-
". Error #" & Err.Number & ": " & Err.Description, vbExclamation, "CarryOver()"
-
Resume Exit_CarryOver
-
End Select
-
End Sub
You would replace lines 17 through 25 with - If ctl.Tag = "Transfer" Then
-
If Not IsNull(rst(ctl.Name)) Then
-
ctl = rst(ctl.Name)
-
End If
-
End If
You would then put Transfer in the Tag property of each control that you want to be transferred.
11 1300
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: - If Not IsNull(rst(ctl.Name)) And Not (ctl.Name = txtFirstName Or ctl.Name = txtLastName) Then
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?
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.
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.
Why didn't I think of that? That would be a much easier method. I have actually done that too. Thanks NeoPa.
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?
You would replace lines 17 through 25 with - If ctl.Tag = "Transfer" Then
-
If Not IsNull(rst(ctl.Name)) Then
-
ctl = rst(ctl.Name)
-
End If
-
End If
You would then put Transfer in the Tag property of each control that you want to be transferred.
Great! Both excellent solutions. I will swap out my code with the Tag approach.
Thanks!
NeoPa 32,556
Expert Mod 16PB
Seth has done a fine job of illustrating the concept. It could also work as : - If (ctl.Tag = "Transfer") And (Not IsNull(rst(ctl.Name))) Then _
-
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).
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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 , ;
|
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...
|
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....
|
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 & "( >= " &...
|
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
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |