I would like to run the same set of checks on various controls on a form, including a before update event and an after update event. - Option Compare Database
-
Option Explicit
-
-
Dim CurrrentVal As String
-
-
Private Sub Control_BeforeUpdate(Cancel As Integer)
-
CurrentVal = Me.Control.Value
-
End Sub
-
-
Private Sub Control_AfterUpdate()
-
-
If Me.ControlA.Value = -1 And (Not (IsNull(Me.Control.Value))) Then
-
MsgBox ("You're not allowed to do this because it will screw up the info in the DB")
-
Me.Control.Value = CurrentVal
-
Exit Sub
-
End If
-
End Sub
-
-
Is there a way of producing this as a function with arguments, where the argument would be the control name??
I have chosen this method as I got various errors using just the BeforeUpdate, Cancel, Undo method.
15 8637 NeoPa 32,556
Expert Mod 16PB
You can make generic code (in a procedure) that can be called from any of the event procedures.
It is not possible though, to set up generic events procedures, as there are no generic events. Each event you want handled must have an event procedure, even if it simply calls another procedure then exits.
Bear in mind this is not VBA code logic, this is the design within Access you're talking about.
Ok I think I see your point. Like to capture control name for the event btnX_click I would need the btnX_click event to find which btn has been clicked?
Not sure my brain is in straight this afternoon.
Thank you.
NeoPa 32,556
Expert Mod 16PB
No worries. It is actually a very good idea in such circumstances to have the main code in a single place, then just call that procedure from the various control event procedures.
NeoPa 32,556
Expert Mod 16PB
Ah.
It may be possible with use of a class as Fish says. Have a look if it's something you want to go to that depth for. If you often have similar requirements it may well be a good idea. Also if the number of controls is quite large.
FishVal - Thanks for the 2 very long thread links. Just skimmed over them but both look like they would have a solution to my question.
Thanks again all.
You are welcome, Chinde.
P.S. In the attachment you will find sample database where the approach used in the threads I've pointed you to is used in class merging MouseMove event of form components. The code there is more clean, commented and comprehensive.
You could do it like this. Based on your code, I assumed that for each MyControl you have a MyControlA. You would need to have a seperate before and after updated event for each control but they would just be the one line call to the public sub. - Option Compare Database
-
Option Explicit
-
-
Dim CurrrentVal As String
-
-
Public sub CheckVal(ctl as string, src as boolean)
-
if src = True then 'Being called from before update event
-
CurrentVal = Me(ctl).Value
-
Else ' Being called from after update event
-
If Me((ctl) & "A").Value = -1 And (Not (IsNull(Me(ctl).Value))) Then
-
MsgBox ("You're not allowed to do this because it will screw up the info in the DB")
-
Me(ctl).Value = CurrentVal
-
Exit Sub
-
End If
-
End If
-
-
Private Sub MyControl_BeforeUpdate(Cancel As Integer)
-
Call CheckVal("MyControl", True)
-
End Sub
-
-
Private Sub MyControl_AfterUpdate()
-
Call CheckVal("MyControl", False)
-
End Sub
-
-
Private Sub My2ndControl_BeforeUpdate(Cancel As Integer)
-
Call CheckVal("My2ndControl", True)
-
End Sub
-
-
Private Sub My2ndControl_AfterUpdate()
-
Call CheckVal("My2ndControl", False)
-
End Sub
@FishVal
I get Runtime error '2462' opening either form.
@RuralGuy
Ok.
Could you please trace down to the line of code where it actually fails?
In:
Public Property Set Form(ByRef frmNewValue As Access.Form)
...of the FEM Module. - For i = 0 To 4
-
Set obj = New FEMMember
-
With obj
-
Set .Control = frmForm.Section(i) '<--- ERROR ON THIS LINE
-
If Not Err Then
-
Set .Parent = Me
-
Me.FormControls.Add obj
-
End If
-
Set obj = Nothing
-
End With
-
Next
This portion of code is sandwiched in
On Error Resume Next
On Error Goto 0
I have no idea how it could fail.
The reason for this is that form could have up to 5 sections. If some is missed, then code proceeds to the next.
Are you sure you don't have "Error Trapping" set to "Break on All Errors"?
By golly you were correct. Switched to unhandled errors and viola, we're off and running. Thanks.
NeoPa 32,556
Expert Mod 16PB
On a similar theme, I've discovered that, if you ever develop class module code, it's a good idea to set it to Break in Class Module. This is like a superset of Break on Unhandled Errors.
It is only not appropriate (AFAICS) when using third party class modules that can error (and you don't want to be left debugging their code).
If you never develop Class Modules though, it's entirely unimportant :->.
Thanks FishVal
The DB is an excellent example.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Thomas Mlynarczyk |
last post by:
Hello,
My provider has set register_globals = On and I can't change the php.ini
file. Is there a way to unset all the imported get/post etc. variables at
the beginning of my script?
Thomas
|
by: Larry L |
last post by:
I have a Module that declares several arrays as public, some string, some
integers. I then have 2 forms. Form A is the main form, that loads on
start-up, and has a command button to open Form B. On...
|
by: Wayne J |
last post by:
I have 3 pages, each with 2 tables (tblNew and tblConfirm), because these 2
tables are affected by the same code, I thought if I used an inherited class
defining these variables along with the...
|
by: RFS666 |
last post by:
Hello,
After I posted yesterday "using C# class in jscript", I have a new problem:
I have a C# class - DBResult - that contains (and other variables) a string
array (and other variables), that...
|
by: rlrcstr |
last post by:
The DBA team at the office controls the naming conventions for the database
structure, but their naming convention are rather tedious. So typically I
create a global module that I use as a mapping...
|
by: yb |
last post by:
Hi,
Looking for clarification of undefined variables vs. error in
JavaScript code.
e.g.
<script>
alert( z ); // this will be an error, i.e. an exception
</script>
|
by: John Salerno |
last post by:
After reading the PEP, I'm still not quite sure if there is a
recommended (or widely preferred) method of naming variables. Here are
the relevant bits:
> Global Variable Names
>
> (Let's...
|
by: Jonathan Wood |
last post by:
I was just wondering what naming convention most of you use for class
variables.
Underscore, "m_" prefix, camel case, capitalized, etc?
Has one style emerged as the most popular?
Thanks for...
|
by: mk |
last post by:
http://www.python.org/dev/peps/pep-0008/
"Function Names
Function names should be lowercase, with words separated by underscores
as necessary to improve readability."
However, this PEP does...
|
by: Bruce |
last post by:
When I do this:
<input id="someName" type="text">
<script type="text/javascript">
alert(someName.nodeName);
</script>
Both IE6 and Firefox alert("INPUT"). Why isn't "someName" undefined?
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |