Results 1 to 4 of 4

Thread: [RESOLVED] Dealing with Null values in VBA Access Functions

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2008
    Posts
    44

    Resolved [RESOLVED] Dealing with Null values in VBA Access Functions

    Hi all,

    I'm trying to write a simple function in Access VBA that I can use in a Jet SQL statement to return Null if a time value is greater than 20 hours (as this would have to be a data entry error).

    I've got this far:
    Code:
    Function Cln(MyTime As Date)
    
    If IsNull(MyTime) Then
    Cln = Null
    Else
    If MyTime > 0.833333333333333 Then Cln = Null Else Cln = MyTime
    End If
    
    End Function
    This works perfectly when passed a time but returns "#Error" if there is no time in the row (and therefore no MyTime). Clearly the IsNull element is not the right one to use.

    Could anyone tell me how to handle instances where the value is missing in a custom access function as I've tried the standard Excel options (="", isNull, IsEmpty, is Nothing) and nothing seems to work.

  2. #2
    Addicted Member
    Join Date
    Mar 2009
    Posts
    157

    Re: Dealing with Null values in VBA Access Functions

    Check out this article:
    http://articles.techrepublic.com.com...1-5034252.html

    It may help answer your question.

  3. #3
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Dealing with Null values in VBA Access Functions

    MyTime As Date cannot accept value of a blank cell because Date data type cannot be Null. That is why you get #Error on cell.
    Try this function:
    Code:
    Public Function ValidTime(varTime As Variant) As Variant
        Dim t As Date
        
        ValidTime = ""
        'ValidTime = Null '-- returns 0 on cell
        If varTime <> "" Then
            On Error GoTo GetOut
            t = CDate(varTime)
            If t >= 0 And t <= TimeSerial(20, 0, 0) Then ValidTime = t
        End If
    GetOut:
    End Function
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2008
    Posts
    44

    Re: Dealing with Null values in VBA Access Functions

    Thank you for your help

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