CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 1999
    Location
    Colorado, USA
    Posts
    1,002

    Function return value to Access query

    I'm returning an Integer from a VBA function in a global module to a query in Access 2000. That's fine. But as soon as I try to give the returning Integer a criteria like >0 or <>0, the resulting DataSheet produces the results of the query (judging from the first 20 or so rows, successfully) then an error message box saying that there is a data type mismatch. As soon as I OK the message box, Access redraws the datasheet with all errors. I've tried returning a Variant from the function but still had the same problem.

    I've tried a search on this forum and a few Access and VB books but can't find an answer. Any help? Thanks.

    The relevant portion of the SQL WHERE clause is
    ((CalcMaterial([TicketDetail].[Description]))<>0)

    CalcMaterial is the VB function
    Public Function CalcMaterial(strDescription As String) As Integer

    As I said, the query has no problem with the returned value as long as there is no criteria against it.

    Steve

  2. #2
    Join Date
    Aug 2000
    Location
    Essex, Uk
    Posts
    1,214
    This sounds as though the field you are testing against has some illegal entries in some of the records (at least 1 record) probably a NULL value.
    If you find my answers helpful, dont forget to rate me

  3. #3
    Join Date
    Sep 1999
    Location
    Colorado, USA
    Posts
    1,002
    That was it Bill. Without criteria, there is 1 entry far down in the datasheet. Thanks.

    I still don't understand how the function returns a NULL. The default return value of 0 for the function is set as soon as the function starts. Maybe I need a few tips on how to better write a function. Here's the beginning...
    Code:
    Public Function CalcMaterial(strDescription As String) As Integer
    On Error GoTo Err_CalcMaterial
        CalcMaterial = 0
        If IsNull(strDescription) Then
            Exit Function
        End If
        Dim strLen As Integer
        strLen = Len(strDescription)
        If strLen < 1 Then
            CalcMaterial = 0
            Exit Function
        End If
    And the error handler at the end
    Code:
    Err_CalcMaterial:
        MsgBox "Err_CalcMaterial: " & Err.Description
        CalcMaterial = 0
    End Function
    Steve

  4. #4
    Join Date
    Apr 2002
    Location
    Melbourne, Victoria, Australia
    Posts
    1,792
    A string is initialised to "", therefore when you pass it into the function, you should test for both conditions.
    Code:
    If (IsNull(strDescription) OR strDescription = "") Then
    Be nice to Harley riders...

  5. #5
    Join Date
    Sep 1999
    Location
    Colorado, USA
    Posts
    1,002
    I thought the Len() fxn would check for that the same way strlen/_tcslen does in C. I needed the length for other parsing/string extraction in the function so I thought I might as well get it at the beginning and kill 2 birds with one stone.

    Steve

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

Featured