473,395 Members | 1,616 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,395 software developers and data experts.

Generic function for control BeforeUpdate

1,287 Expert 1GB
I have a generic AfterUpdate function applied to my 14 text boxes in a continuous subform, but I can't figure out the syntax to do BeforeUpdate with Cancel. Is this even possible?
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.   Dim ctl As Control
  3.   For Each ctl In Me.Controls
  4.     If Left(ctl.Name, 3) = "Day" Then
  5. '???  ctl.BeforeUpdate = "=CheckDay(""" & ctl.Name & """)"
  6.       ctl.AfterUpdate = "=FixDay(""" & ctl.Name & """)"
  7.       End If
  8.   Next ctl
  9. End Sub
  10.  
  11. Public Function FixDay(Name As String)
  12.   If Me.Controls(Name) = "" Or IsNull(Me.Controls(Name)) Then
  13.     Me.Controls(Name) = 0
  14.   End If
  15. End Function
  16.  
  17. Public Function CheckDay(Name As String, Cancel As Integer)
  18. '???
  19.   Dim hours As Currency
  20.   hours = Me.Controls(Name)
  21.   If (hours < 0) Or (hours > 24) Or (Not (hours * 4 = Int(hours * 4))) Then
  22.     MsgBox "Daily hours should be positive, multiples of .25, and less than 24.", , "Timesheet Error"
  23.     Cancel = True
  24.   End If
  25. End Sub
Each day in the pay period has a text box to enter a number of hours.
I know I can copy BeforeUpdate code for each control, but I would be very grateful if someone could give me an example of how to set the .BeforeUpdate to a custom function. I'm sure the capability to set these dynamicaly will be beneficial down the road.
Sep 3 '09 #1

✓ answered by ChipR

NeoPa - My experience with user-defined functions for events is limited to this attempt. I just decided it was time to learn.

On the positive side, some more poking around VB Help led me to the answer I was looking for originally!
Expand|Select|Wrap|Line Numbers
  1. Public Function CheckDay(Name As String)
  2.     If Me.Controls(Name) < 0 Then
  3.         MsgBox "must be > 0!"
  4.         DoCmd.CancelEvent
  5.     End If
  6. End Function

11 3429
FishVal
2,653 Expert 2GB
Hi, Chip.

I think it is not possible utilizing expression in BeforeUpdate property.
However it is possible using this method.
Sep 3 '09 #2
ajalwaysus
266 Expert 100+
Try This... I tweaked it a bit

Expand|Select|Wrap|Line Numbers
  1.  
  2. Public Function CheckDay(Name As String) as Integer
  3. '???
  4.   Dim hours As Currency
  5.   hours = Me.Controls(Name)
  6.   If (hours < 0) Or (hours > 24) Or (Not (hours * 4 = Int(hours * 4))) Then
  7.     MsgBox "Daily hours should be positive, multiples of .25, and less than 24.", , "Timesheet Error"
  8.     CheckDay= True
  9.   End If
  10. End Sub
  11.  
I haven't tested it, but in theory it could work. =)

Let me know if this works,
-AJ
Sep 3 '09 #3
ChipR
1,287 Expert 1GB
AJ, I thought that and tried the exact same code, though I added:
line 3. CheckDay=False
I couldn't get it to Cancel. I can't seem to find any documentation on how Cancel works. I mean, I know how to use it, but not why.

I've decided to go with DonRayner's method in Post #9 of the thread that FIshVal linked, at least until I can sort through the more advanced material there.

Thanks for responding so quickly,
Chip
Sep 3 '09 #4
NeoPa
32,556 Expert Mod 16PB
I'm not very experienced with this Chip, but from my reading of the Help System info, no parameters can be passed to User-defined functions.

Have you any experience of such parameters working (It's an interesting concept to be sure)?
Sep 3 '09 #5
ChipR
1,287 Expert 1GB
NeoPa - My experience with user-defined functions for events is limited to this attempt. I just decided it was time to learn.

On the positive side, some more poking around VB Help led me to the answer I was looking for originally!
Expand|Select|Wrap|Line Numbers
  1. Public Function CheckDay(Name As String)
  2.     If Me.Controls(Name) < 0 Then
  3.         MsgBox "must be > 0!"
  4.         DoCmd.CancelEvent
  5.     End If
  6. End Function
Sep 3 '09 #6
FishVal
2,653 Expert 2GB
BTW, Chip, looking at function FixDay() I would expect it will wipe out the control which has fired AfterUpdate event since it returns Null in all cases.
Am I right?
............
............
Well. Looks like it works this way.
Sep 3 '09 #7
FishVal
2,653 Expert 2GB
@ChipR
That is nice, really nice.
Thanks for sharing.
Sep 3 '09 #8
ChipR
1,287 Expert 1GB
The AfterUpdate event doesn't seems to require a value to be returned, it just calls a function or a macro. FixDay() works as inteded, replacing blanks with zeros, and leaving the value alone otherwise. The BeforeUpdate event also doesn't seems to expect (or do anything with) a returned value, but I'm not sure what other events work this way.
Sep 3 '09 #9
NeoPa
32,556 Expert Mod 16PB
@FishVal
Absolutely. I'm with Fish on this Chip. Well done.
Sep 3 '09 #10
NeoPa
32,556 Expert Mod 16PB
@ChipR
As all Event Procedures are subroutines rather than functions, and the syntax for specifying a User-Defined procedure is simply "=UDF()", I would be very surprised if these were not all treated as subroutines. There seems nowhere to put a return value even if passed. I'm confident any values, if supplied, are simply dropped in the bit-bucket.
Sep 3 '09 #11
ajalwaysus
266 Expert 100+
Thanks ChipR, I can use this code. =)

-AJ
Sep 3 '09 #12

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

Similar topics

4
by: William Wisnieski | last post by:
Hello Everyone, Access 2000 I have a form with multiple pages on it. There is one text field on the third page of the form that I need the user to complete before leaving the form or moving...
2
by: Lyn | last post by:
Hi, I have a text control on a form which is bound to table field StartDate which is in Date format. When updating the table record via the form, any data entered into the StartDate control is...
6
by: David Sullivan | last post by:
Hi I have a problem with a database where certain users are closing access (by clicking on the X in top right-hand corner) instead of using a command button before they fill in all relevant...
5
by: Lyn | last post by:
Hi, In the BeforeUpdate event of a textbox control I have the following simplified code:- Private Sub StartDate_BeforeUpdate(Cancel As Integer) MsgBox "StartDate Error -- Please Check and...
9
by: simonmarkjones | last post by:
I want to call a function which does this when the next record button is pressed (calling it from before update) if textboxes are empty then Message box you must fill text box
3
by: .Net Newbie | last post by:
I'm new to .Net and need to create a generic (free) way to update lookup tables in SQL Server (using C#) in ASP.Net pages. I found an article at:...
5
by: Richard Brown | last post by:
Ok, I've been looking through the .NET SDK docs and stuff. I'm wondering if you can provide a control extender that does generic validation or functionality just by dropping it on the form. For...
3
by: Seth Gecko | last post by:
Hi I am working with generic lists of various objects and a control dealing with these lists. For instance: A parent form holds: dim Walls as List(Of wall) dim Segments as List(Of segment) ...
2
by: jock1up | last post by:
I am working with editing within a DataGrid control cell and am confused about which events occur after the editing is completed. I see that 5 events can occur total, including the ones below and...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
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
tracyyun
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...
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,...

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.