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? -
Private Sub Form_Load()
-
Dim ctl As Control
-
For Each ctl In Me.Controls
-
If Left(ctl.Name, 3) = "Day" Then
-
'??? ctl.BeforeUpdate = "=CheckDay(""" & ctl.Name & """)"
-
ctl.AfterUpdate = "=FixDay(""" & ctl.Name & """)"
-
End If
-
Next ctl
-
End Sub
-
-
Public Function FixDay(Name As String)
-
If Me.Controls(Name) = "" Or IsNull(Me.Controls(Name)) Then
-
Me.Controls(Name) = 0
-
End If
-
End Function
-
-
Public Function CheckDay(Name As String, Cancel As Integer)
-
'???
-
Dim hours As Currency
-
hours = Me.Controls(Name)
-
If (hours < 0) Or (hours > 24) Or (Not (hours * 4 = Int(hours * 4))) Then
-
MsgBox "Daily hours should be positive, multiples of .25, and less than 24.", , "Timesheet Error"
-
Cancel = True
-
End If
-
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.
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! - Public Function CheckDay(Name As String)
-
If Me.Controls(Name) < 0 Then
-
MsgBox "must be > 0!"
-
DoCmd.CancelEvent
-
End If
-
End Function
11 3429
Hi, Chip.
I think it is not possible utilizing expression in BeforeUpdate property.
However it is possible using this method.
Try This... I tweaked it a bit -
-
Public Function CheckDay(Name As String) as Integer
-
'???
-
Dim hours As Currency
-
hours = Me.Controls(Name)
-
If (hours < 0) Or (hours > 24) Or (Not (hours * 4 = Int(hours * 4))) Then
-
MsgBox "Daily hours should be positive, multiples of .25, and less than 24.", , "Timesheet Error"
-
CheckDay= True
-
End If
-
End Sub
-
I haven't tested it, but in theory it could work. =)
Let me know if this works,
-AJ
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
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)?
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! - Public Function CheckDay(Name As String)
-
If Me.Controls(Name) < 0 Then
-
MsgBox "must be > 0!"
-
DoCmd.CancelEvent
-
End If
-
End Function
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.
@ChipR
That is nice, really nice.
Thanks for sharing.
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.
NeoPa 32,556
Expert Mod 16PB @FishVal
Absolutely. I'm with Fish on this Chip. Well done.
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.
Thanks ChipR, I can use this code. =)
-AJ
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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
|
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:...
|
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...
|
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)
...
|
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...
|
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...
|
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: 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: 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: 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...
|
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,...
| |