473,320 Members | 2,112 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,320 software developers and data experts.

Variables as Control names

52
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.

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Dim CurrrentVal As String
  5.  
  6. Private Sub Control_BeforeUpdate(Cancel As Integer)
  7.     CurrentVal = Me.Control.Value
  8. End Sub
  9.  
  10. Private Sub Control_AfterUpdate()
  11.  
  12.     If Me.ControlA.Value = -1 And (Not (IsNull(Me.Control.Value))) Then
  13.         MsgBox ("You're not allowed to do this because it will screw up the info in the DB")
  14.         Me.Control.Value = CurrentVal
  15.         Exit Sub
  16.     End If
  17. End Sub
  18.  
  19.  
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.
Apr 2 '09 #1
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.
Apr 2 '09 #2
Chinde
52
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.
Apr 2 '09 #3
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.
Apr 2 '09 #4
FishVal
2,653 Expert 2GB
Actually, it could be done.
See attachment in this thread or maybe better this thread.
Apr 2 '09 #5
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.
Apr 2 '09 #6
Chinde
52
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.
Apr 3 '09 #7
FishVal
2,653 Expert 2GB
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.
Attached Files
File Type: zip FEM.zip (35.0 KB, 124 views)
Apr 3 '09 #8
DonRayner
489 Expert 256MB
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.

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Dim CurrrentVal As String
  5.  
  6. Public sub CheckVal(ctl as string, src as boolean)
  7. if src = True then   'Being called from before update event
  8.     CurrentVal = Me(ctl).Value
  9. Else ' Being called from after update event
  10.         If Me((ctl) & "A").Value = -1 And (Not (IsNull(Me(ctl).Value))) Then
  11.         MsgBox ("You're not allowed to do this because it will screw up the info in the DB")
  12.         Me(ctl).Value = CurrentVal
  13.         Exit Sub
  14.     End If
  15. End If
  16.  
  17. Private Sub MyControl_BeforeUpdate(Cancel As Integer)
  18.     Call CheckVal("MyControl", True)
  19. End Sub
  20.  
  21. Private Sub MyControl_AfterUpdate()
  22.     Call CheckVal("MyControl", False)
  23. End Sub
  24.  
  25. Private Sub My2ndControl_BeforeUpdate(Cancel As Integer)
  26.     Call CheckVal("My2ndControl", True)
  27. End Sub
  28.  
  29. Private Sub My2ndControl_AfterUpdate()
  30.     Call CheckVal("My2ndControl", False)
  31. End Sub
Apr 3 '09 #9
RuralGuy
375 Expert 256MB
@FishVal
I get Runtime error '2462' opening either form.
Apr 3 '09 #10
FishVal
2,653 Expert 2GB
@RuralGuy
Ok.

Could you please trace down to the line of code where it actually fails?
Apr 3 '09 #11
RuralGuy
375 Expert 256MB
In:
Public Property Set Form(ByRef frmNewValue As Access.Form)

...of the FEM Module.

Expand|Select|Wrap|Line Numbers
  1.    For i = 0 To 4
  2.       Set obj = New FEMMember
  3.       With obj
  4.          Set .Control = frmForm.Section(i) '<--- ERROR ON THIS LINE
  5.          If Not Err Then
  6.             Set .Parent = Me
  7.             Me.FormControls.Add obj
  8.          End If
  9.          Set obj = Nothing
  10.       End With
  11.    Next
Apr 3 '09 #12
FishVal
2,653 Expert 2GB
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"?
Apr 3 '09 #13
RuralGuy
375 Expert 256MB
By golly you were correct. Switched to unhandled errors and viola, we're off and running. Thanks.
Apr 4 '09 #14
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 :->.
Apr 4 '09 #15
Chinde
52
Thanks FishVal

The DB is an excellent example.
Apr 6 '09 #16

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

Similar topics

21
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
13
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...
2
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...
6
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...
6
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...
17
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>
2
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...
114
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...
1
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...
18
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?
0
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...
0
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...
0
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...
0
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...
0
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...
0
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...
0
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....
0
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
0
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...

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.