Is there a way to call the BeforeUpdate event procedure of any control in VBA (MS Access 2002 or 2003)?

In VBA, I am changing the value of several controls on an Access form. I like to fire BeforeUpdate events of these controls after this, as it checks for consistency between the fields:

Private Sub ExampleProc1()
  Dim intCancel as Integer

  intCancel = False

  Me.Controls("Date1").Value=Null
  Me.Controls("Textfield1").Value=Null

  Call Date1_BeforeUpdate(intCancel)
  Call Textfield1_BeforeUpdate(intCancel)
End Sub

I would like to make it general, but I cannot find a way to trigger the event procedure. I would like something like this:

Private Sub ExampleProc2(ParamArray Fields())
  Dim intCancel as Integer, varV as Variant

  For Each varV in Fields
    Me.Controls(varV).value=Null
    intCancel = False
    Call Me.Controls(varV).BeforeUpdate(intCancel)
  Next
End Sub

Is it possible? Maybe something with DoCmd.RunMacro is the way to go?

+3
source share
4 answers

You can actually do this using CallByName

CallByName Me, Me.Controls(varV).Name & "_BeforeUpdate", VbMethod, intCancel
+3
source

Wisg Run (http://msdn.microsoft.com/en-us/library/aa199108(office.10).aspx) Eval Function:

Debug.Print Eval("ShowNames()")    
Debug.Print Eval("StrComp(""Joe"",""joe"", 1)")
Debug.Print Eval("Date()")
+1

, , . . , :

  Dim mcolDateFields As New Collection

OnLoad :

  mcolDateFields.Add Me!txtDateEntered, "txtDateEntered"
  mcolDateFields.Add Me!txtDatePrinted, "txtDatePrinted"
  mcolDateFields.Add Me!txtDateArchived, "txtDateArchived"

, - :

  Dim varItem As Variant
  Dim ctl As Control

  For Each varItem In mcolDateFields
    Set ctl = varItem
    Debug.Print ctl.Name & ": " & ctl.Value
  Next varItem
  Set ctl = Nothing

... Debug.Print , .

, - :

  Public Sub (mcolCollection As Collection)
    Dim varItem As Variant
    Dim ctl As Control

    For Each varItem In mcolCollection 
      Set ctl = varItem
      Debug.Print ctl.Name & ": " & ctl.Value
    Next varItem
    Set ctl = Nothing
  End Sub

, . , , . , :

  Dim varItem As Variant
  Dim ctl As Control

  For Each varItem In mcolCollection 
    Set ctl = varItem
    Debug.Print ctl.Parent.Name & "!" & ctl.Name & ": " & ctl.Value
  Next varItem
  Set ctl = Nothing

, , Me , ctl.Parent, , ( , , ).

- :

  Private Sub ExampleProc1()
    Dim intCancel as Integer

    intCancel = False

    Me.Controls("Date1").Value=Null
    Me.Controls("Textfield1").Value=Null

    Call Date1_BeforeUpdate(intCancel)
    Call Textfield1_BeforeUpdate(intCancel)
  End Sub

, , - , True (-1). , ​​Access VBA VBA, Access Basic Boolean. , Booleans , Cancel.

  Me.Controls("Date1").Value=Null

, . :

  Me!Date1 = Null

, : . , Controls Fields, , , , , . , , , , , , , .

,.Value , . , , , , - , ByRef, ByVal. , , .

, , . , .

+1

It seems possible to make CallByName in Access, but you can also put your BeforeUpdate logic for all your controls in a centralized unit, changing its behavior based on the parameters you pass. Then all that you would have in BeforeUpdate event handlers would be a call to your centralized routine.

0
source

Source: https://habr.com/ru/post/1709315/


All Articles