Results 1 to 10 of 10
  1. #1
    jamin14 is offline Novice
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Posts
    17

    running a function after update

    i have created a public function called validdaterequired does anyone know how i am able to run this function after an update on my form .

    below is my function code

    Public Function ValidDateRequired(OrderDate As Date, _
    RequiredDate As Date) As Boolean

    ' Default value
    ValidDateRequired = False

    ' Order date must be filled in
    If IsNull(textOrderDate) = False Then
    ' Required date must be filled in
    If IsNull(textRequiredDate) = False Then
    ' If the Required date is later than the Order date,
    ' it's valid.
    If textRequiredDate > textOrderDate Then
    ValidDateRequired = True


    End If
    If textRequiredDate < textOrderDate Then
    MsgBox "Wrong"

    End If
    End If

    End Function

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Based on how it's written (returning True/False), one way would be to use it in the before update event of the form:

    Cancel = ValidDateRequired(...)

    I would point out that you pass parameters to the function but don't use them.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    jamin14 is offline Novice
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Posts
    17
    Quote Originally Posted by pbaldy View Post
    Based on how it's written (returning True/False), one way would be to use it in the before update event of the form:

    Cancel = ValidDateRequired(...)

    I would point out that you pass parameters to the function but don't use them.

    it returns with the eroor argument is not optional

    Private Sub textRequiredDate_BeforeUpdate(Cancel As Integer)
    Cancel = ValidDateRequired
    End sub

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Of course; your function requires two parameters, which as I said you don't actually use. Either drop them from the function or provide them when you call it (and presumably use them).
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    jamin14 is offline Novice
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Posts
    17
    Quote Originally Posted by pbaldy View Post
    Of course; your function requires two parameters, which as I said you don't actually use. Either drop them from the function or provide them when you call it (and presumably use them).
    which ones are invalid?

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    I didn't say anything was invalid. The function as written asks for 2 parameters; "OrderDate As Date, RequiredDate As Date".

    In your function you use textOrderDate and textRequiredDate. It isn't clear from the code if those are coming from a form or what.

    So what I'm saying is to either drop the required parameters and get the dates from wherever those are coming from, or include the parameters when you call the function, and change it to use them.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    jamin14 is offline Novice
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Posts
    17
    Quote Originally Posted by pbaldy View Post
    I didn't say anything was invalid. The function as written asks for 2 parameters; "OrderDate As Date, RequiredDate As Date".

    In your function you use textOrderDate and textRequiredDate. It isn't clear from the code if those are coming from a form or what.

    So what I'm saying is to either drop the required parameters and get the dates from wherever those are coming from, or include the parameters when you call the function, and change it to use them.
    how do i specify what form they are from?

    thankss

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    After going through the function that you had posted I would like to point out the following Points:

    Public Function ValidDateRequired(OrderDate As Date, _
    RequiredDate As Date) As Boolean

    ' Default value
    ValidDateRequired = False

    ' Order date must be filled in
    If IsNull(textOrderDate) = False Then
    ' Required date must be filled in
    If IsNull(textRequiredDate) = False Then
    ' If the Required date is later than the Order date,
    ' it's valid.
    If textRequiredDate > textOrderDate Then
    ValidDateRequired = True
    End If
    If textRequiredDate < textOrderDate Then
    MsgBox "Wrong"

    End If
    End If

    End Function


    1) The Red highlighted part is absolutely unnecessary because you have already set the value of ValidDateRequired Boolean to False at the start of the function. Thus the value of the Boolean will become true only when RequiredDate>OrderDate.
    2) The part highlighted in green are obviously some control of a form. A function is applicable to all forms in a database Orderdate and RequiredDate are the two parameters that we have so we have to specify what are these parameters.

    What I have done:

    Modified the Function as Follows:

    Option Compare Database
    Dim OrderDate As Date
    Dim RequiredDate As Date

    Public Function ValidDateRequired(OrderDate, RequiredDate) As Boolean

    ValidDateRequired = False

    If RequiredDate > OrderDate Then
    ValidDateRequired = True
    ElseIf RequiredDate < OrderDate Then
    MsgBox "Required Date < OrderDate"
    ValidDateRequired = False
    End If
    End Function


    Now how to use it. I fully agree with pbaldy that using the function in the Before UpdateEvent is a Good Idea:

    I have a table
    Order:{OrderID Auto Number PK, OrderDate, RequiredDate, Product, Price}

    I have a Form Order in the BeforeUpdate Event I have put the following code:

    Private Sub Form_BeforeUpdate(Cancel As Integer)
    Dim blValidate As Boolean
    blValidate = ValidDateRequired(Me.OrderDate, Me.RequiredDate)
    Select Case blValidate
    Case Is = True
    MsgBox "Validation Successful"
    Case Is = False
    Cancel = True
    MsgBox "Validation Failed"
    Me.RequiredDate.SetFocus
    End Select
    End Sub
    This code prevents the user to save by closing, go to the next record, enter new record, and go to previous record when validation fails.

    How to use sample mdb:

    Order Form will as startup Type Required Date less than Orderdate see what happens.

    Mark this thread solved if it solves your problem. Find attached sample mdb

  10. #10
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    is this problem of yours is solved if yes mark the thread solved. If you have solved it by some other way please post your work. This will help somebody with a similar problem to find assured answers.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Query with a running sum
    By is49460 in forum Queries
    Replies: 3
    Last Post: 09-07-2013, 11:11 PM
  2. Running parameter queries from VBA
    By John Southern in forum Programming
    Replies: 6
    Last Post: 03-25-2010, 10:24 AM
  3. Running a VB function in a Macro
    By JuuJuu in forum Access
    Replies: 1
    Last Post: 10-27-2009, 02:50 AM
  4. Want function to get current function name
    By Davis DeBard in forum Programming
    Replies: 2
    Last Post: 08-13-2009, 05:02 AM
  5. Replies: 0
    Last Post: 06-21-2009, 01:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums