Results 1 to 2 of 2
  1. #1
    2 Star Lounger
    Join Date
    Jan 2002
    Location
    Brugge, Belgium
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Calling a sub from a event (MS ACCESS 2007)

    In the "On Enter" event of three controls of a subform,the code has to perform the same test : make sure a ClientID is already present.
    After this check the code in each of the three controls has to go on with the rest of the code specific for each of the events.
    To eliminate the number of code lines, i decide to construct a sub that can perform the task common for each of this controls.
    Therefore i declared a global boolean variable: NoClientNr , and the following sub:

    Sub NoNr()
    If IsNull ( Me.Parent![klnt_ID] Or me.Parent![Klnt_Id] = o then
    MsgBox Message, vbExclamation, MD_Titel
    NoClientNr = True
    Else
    NoClientNr = False
    End If
    End Sub

    In the event of each of the controls i wrote the following code

    Call NoNr()

    If NoClientNr = true then
    msgbox ("No clientnr")
    else
    msgbox ("a clientnr")
    end if


    Running the program makes the message from the sub to be displayed when no Clientnr is present,
    but no message is displayed, once we are back from the sub in the event of the control
    I alse tried a function in stead of a sub : Public function NoNr() As boolean
    But the result was she same
    What did i wrong ?

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Calling a sub from a event (MS ACCESS 2007)

    Instead of using a global variable, I'd do it like this. Note that I changed the line that tests klnt_ID, it wasn't correct as posted by you

    Private Function NoNr() As Boolean
    If IsNull(Me.Parent![klnt_ID]) Or Me.Parent![Klnt_Id] = 0 Then
    MsgBox Message, vbExclamation, MD_Titel
    NoNr = True
    Else
    NoNr = False
    End If
    End Function

    In the On Enter events:

    If NoNr = True Then
    MsgBox "No clientnr"
    Else
    MsgBox "A clientnr"
    End If

    I wouldn't use the On Enter event for this myself, but probably the Before Insert event of the subform.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •