I am trying to get my form to autofill the values from the last record to the new record. In the form properites on the BeforeInsert, I used the following module
I have been away from VB for several years and am finding it challenging to understand. Can you all help? The other part of the code debugs. See below
I keep getting the same error when I debug this
Compile Error: Invalid use of Me Keyword
Here is the code. What comes back highlighted is the 3rd line, the word Me Module7 - Option Compare Database
-
-
Private Sub Form_BeforeInsert(Cancel As Integer)
-
Dim strMsg As String
-
Call CarryOver(Me, strMsg)
-
If strMsg <> vbNullString Then
-
MsgBox strMsg, vbInformation
-
End If
-
-
End Sub
Module9 - Option Compare Database
-
-
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
5 5713 - In the future, kindly use Code Tags for readability purposes.
- I just browsed at your code for a minute, but the syntax for passing the Current Form appears to be OK.
- Try two things before we proceed any further:
- Pass the Absolute Reference to the Form as the Function Argument, namely:
- Call Carry_Over(Forms("<Form Name>"), strMsg)
- Explicitly Declare the Parameter in the Function Declaration as Access.Form, namely:
- Public Function CarryOver(frm As Access.Form, strErrMsg As String, ParamArray avarExceptionList()) As Long
- I do not see the ParamArray() Values being passed to the Function.
- Let us know how you make out.
Thank you for your help. Again I am just getting back to Access and am finding the terms confusing. When I add code above and run, it still stops at Me This seems to be a sticking point I can't get around.
@Abest - Call CarryOver(Me, strMsg)
- What is the Value of strMsg being passed?
- Where are the ParamArray() Values being passed?
NeoPa 32,556
Expert Mod 16PB
I would guess your problem is that module7 is not a form module. The Me keyword, referring as it does to the associated form - IE the form the module is a part of, is only available in form modules. Standard modules could not sensibly have any meaning for Me, so they cannot use it.
Welcome to Bytes!
Is Paramarray a optional argument? I don't see that statement anywhere. Either way, nothing wrong with passing reference of any object from a form class to a function in a code module as long as your scopes are correct which seems to be the case.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Frank Jones |
last post by:
When manually running resgen.exe from the command prompt (Visual Studio .NET
2003 command prompt) I get the following error:
error: Invalid ResX input.
error: Specific exception:...
|
by: bazley |
last post by:
I've been tearing my hair out over this:
#ifndef MATRIX2_H
#define MATRIX2_H
#include <QVector>
template<class T>
class Matrix2
{
public:
|
by: Ravikanth[MVP] |
last post by:
Hi
Check out this KB Article.
http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%
3B306172
Ravikanth
>-----Original Message-----
|
by: Stephen |
last post by:
Hi,
I found code snippet for running an app (.exe, .bat) from a web app(using
Process class). its pretty interesting but along with the other posts (that
i cant run a windows app) i found...
|
by: snicks |
last post by:
I'm trying to exec a program external to my ASP.NET app using the following
code. The external app is a VB.NET application.
Dim sPPTOut As String
sPPTOut = MDEPDirStr + sID + ".ppt"
Dim p As...
|
by: Eric |
last post by:
I run a program which read emails from a text file. There is some thing
invalid in one or more text file. When program is busy doing parsing it
shows that error and process stop. I dont know which...
|
by: The|Godfather |
last post by:
Hi everybody,
I read Scotte Meyer's "Effective C++" book twice and I know that
he mentioned something specific about constructors and destructors that
was related to the
following...
|
by: ravisc |
last post by:
Hi,
I had .Net 2005 framework installed in my system. I had uninstalled the same. Now I have been working with .Net 2003 develpment environment. when I try to debug any .Net 2003 project, it gives...
|
by: mahmoodn |
last post by:
Hi, I have a problem with a compiler error "invalid use of incomplete type". Here is the description:
in "first.h", I have:
class cache_t {
public:
virtual void printStats( pseq_t *pseq );...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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: 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: 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...
| |