Results 1 to 3 of 3

Thread: Completely New to VBA, Issue regarding Custom Functions

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    2

    Question Completely New to VBA, Issue regarding Custom Functions

    I have reviewed the forums and I am fairly sure that my issue is that I do not know how to properly reference the access database and table table in my code.

    My quest:

    I want to be able to place a custom function in query that looks at a certain date field within the table and returns values based on what it sees. If I can get the below code to work, I will extrapolate to return about 15 different values based on what it finds in the "Duedate" field in "Tbl1." I am working within a VB module contained in the same access database that the table "tbl1" is stored.

    Again, I believe where I am failing is the proper syntax for referencing the table and the field, and I believe making proper declarations. I know it must be simple but I am learning on my own and I cant find the answer.


    VB Code:
    1. Public Function AgingCat()
    2. If Month(TBL1.DUEDATE) >= Month(Date) And Year(TBL1.DUEDATE) >= Year(Date) Then
    3. AgingCat = "CURRENT"
    4. End If
    5. End Function


    Please help!

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Completely New to VBA, Issue regarding Custom Functions

    You may want to look at some of the threads in the FAQ about opening recordsets. You cannot access the table the way you have written in your code through VB nor through VBA/Access unless tbl1 is a variable/class and has already been set.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    2

    Thumbs up Re: Completely New to VBA, Issue regarding Custom Functions

    I found the solution...

    I simply needed to add the argument DueDate into the function, AgingCat([DueDate])

    VB Code:
    1. Public Function AgingCat(DueDate as Date)
    2. If Month(DueDate) >= Month(Date) And Year(DueDate) >= Year(Date) Then
    3. AgingCat = "CURRENT"
    4. End If
    5. End Function

    AgingCat([DueDate]) is placed in the query that is going against a table that has a Date field DueDate. I like it also because it is flexible. If the field it is going against is called "NextDue" I can simply change the field name to AgingCat([NextDue])

    It's a small victory. Hopefully the first of many.

    Thanks Ecniv!

Posting Permissions

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



Click Here to Expand Forum to Full Width