Filtering on a VB function return value

G

Guest

I've built a VBA function that returns a TRUE/FALSE value. I've defined the
return data type of the function as boolean.

In a query I pass certain values from a table to the VBA function. It works
just fine returning a result that is displayed as 0 or -1. However, if I try
to place a value in the criteria portion of the query, such as TRUE, I get a
message that it has an incompatible data type. If get the same message if I
use either a 0 or -1 for criteria.

In addition, if I run the query without the criteria, and then right-click
on the field and select either Filter by Selection or Filter Excluding
Selection, I get the same error message. In other words, it appears that for
a function return value in a query no possible filter is allowed.

Does anyone have a work around? Thanks.

Regards,
Leif
 
S

strive4peace

Hi Leif,

Is it possible to make an equation in the query instead of
using a function? what is the code for your function?

I had that same problem using a function to generate random
numbers and then specifying that as a sort so I could pick
Top Values

Warm Regards,
Crystal
Microsoft Access MVP 2006

*
Have an awesome day ;)

remote programming and training
strive4peace2006 at yahoo.com

*
 
G

Gary Walter

Leif said:
I've built a VBA function that returns a TRUE/FALSE value. I've defined
the
return data type of the function as boolean.

In a query I pass certain values from a table to the VBA function. It
works
just fine returning a result that is displayed as 0 or -1. However, if I
try
to place a value in the criteria portion of the query, such as TRUE, I get
a
message that it has an incompatible data type. If get the same message if
I
use either a 0 or -1 for criteria.

In addition, if I run the query without the criteria, and then right-click
on the field and select either Filter by Selection or Filter Excluding
Selection, I get the same error message. In other words, it appears that
for
a function return value in a query no possible filter is allowed.
PMFBI

something doesn't sound right

in Access 2000/2002 mdb,
a simple function that returns boolean
can be filtered on for -1 or 0...no problem.

what version of Access?
mdb or adp?

is the data in a mdb?

linked table?

what is the WHERE clause?
is Access wrap -1 or 0 in quotes
(which you don't want obviously)?

something like:

WHERE fSomething([SomeField]) = -1

what happens if you change function
to return string of either "TRUE" or "FALSE"?

is function inordinately complicated
(takes long time to run)?
 
J

John Spencer

I would ***guess*** that the query engine does not know the type of value
that is being returned and therefore is having a problem. You might try
wrapping the function call in one of the conversion functions. That should
ensure that the SQL engine knows what type of value is being returned.

CBool(YourFunction([FieldOne],[FieldTwo]))

The above is speculation on my part and I cannot test it right now. If you
do test it, would you mind posting back with your results? Thanks in
advance.
 
G

Guest

John, the result was no change, I still I'm getting an error message.

John Spencer said:
I would ***guess*** that the query engine does not know the type of value
that is being returned and therefore is having a problem. You might try
wrapping the function call in one of the conversion functions. That should
ensure that the SQL engine knows what type of value is being returned.

CBool(YourFunction([FieldOne],[FieldTwo]))

The above is speculation on my part and I cannot test it right now. If you
do test it, would you mind posting back with your results? Thanks in
advance.

Leif said:
I've built a VBA function that returns a TRUE/FALSE value. I've defined
the
return data type of the function as boolean.

In a query I pass certain values from a table to the VBA function. It
works
just fine returning a result that is displayed as 0 or -1. However, if I
try
to place a value in the criteria portion of the query, such as TRUE, I get
a
message that it has an incompatible data type. If get the same message if
I
use either a 0 or -1 for criteria.

In addition, if I run the query without the criteria, and then right-click
on the field and select either Filter by Selection or Filter Excluding
Selection, I get the same error message. In other words, it appears that
for
a function return value in a query no possible filter is allowed.

Does anyone have a work around? Thanks.

Regards,
Leif
 
G

Guest

Gary,


Access 2003.

mdb

The function is utlitizing a table link to Sybase.


The Query is:
SELECT TransmittalDocument.APC, TransmittalDocument.DrawingNo,
FileFound([Unit],[Type],[SubType],[APC]) AS MFSFileFound,
TransmittalDocument.InSystem
FROM TransmittalDocument
WHERE (((TransmittalDocument.APC) Is Not Null) AND
((FileFound([Unit],[Type],[SubType],[APC]))=True) AND
((TransmittalDocument.InSystem) Is Not Null) AND ((TransmittalDocument.Unit)
Is Not Null) AND ((TransmittalDocument.Type) Is Not Null) AND
((TransmittalDocument.SubType) Is Not Null));


However, instead of TRUE I've also tried 0 and -1 (without quotes).

I tried returning a string (TRUE or FALSE), but I have the same problem.

The function does not take long to run. The FileFound function is defined as:

' Returns true if the file information provided resulted in a file found
' on the MFS (T: drive), otherwise False is returned
Public Function FileFound(Unit As String, docType As Byte, docSubType As
Byte, APC As String) As Boolean
Dim path As String
Dim typeName As String
Dim subTypeName As String

' Build path from the inputs
typeName = DLookup("DirName", "DocType", "TypeID=" & docType)
subTypeName = DLookup("lan_path", "dbo_lan_based_parm",
"apc_doc_type_code=" & _
docType & " And apc_doc_stype_code=" & docSubType)
path = "T:\mfs\U" & Unit & "\" & typeName & "\" & Trim(subTypeName) &
"\CURRENT"
FileFound = FileSrch(path, APC & ".") ' The "." is so that FileSrch
does not wildcard at the end of the APC number
End Function

The FileSrch function used in the above function looks like:

' Returns a True if the file exists in the specified directory, otherwise
' False is returned. File name must end with a ".", or it is equivalent to
' "filename*". File extension is not checked.
Public Function FileSrch(dirName As String, fName As String) As Boolean
Dim i As Integer
Dim path As String

On Error GoTo PROC_ERR

With Application.FileSearch
.NewSearch
.LookIn = dirName
.SearchSubFolders = False
.FileName = fName
' .MatchTextExactly = True
.FileType = msoFileTypeAllFiles
If .Execute() > 0 Then
FileSrch = True
' MsgBox "There were " & .FoundFiles.count & _
' " file(s) found."
' For i = 1 To .FoundFiles.count
' MsgBox .FoundFiles(i)
' Next i
Else
FileSrch = False
' MsgBox "There were no files found."
End If
End With

PROC_EXIT:
Exit Function

PROC_ERR:
MsgBox Err.Description
Resume PROC_EXIT

End Function



Gary Walter said:
Leif said:
I've built a VBA function that returns a TRUE/FALSE value. I've defined
the
return data type of the function as boolean.

In a query I pass certain values from a table to the VBA function. It
works
just fine returning a result that is displayed as 0 or -1. However, if I
try
to place a value in the criteria portion of the query, such as TRUE, I get
a
message that it has an incompatible data type. If get the same message if
I
use either a 0 or -1 for criteria.

In addition, if I run the query without the criteria, and then right-click
on the field and select either Filter by Selection or Filter Excluding
Selection, I get the same error message. In other words, it appears that
for
a function return value in a query no possible filter is allowed.
PMFBI

something doesn't sound right

in Access 2000/2002 mdb,
a simple function that returns boolean
can be filtered on for -1 or 0...no problem.

what version of Access?
mdb or adp?

is the data in a mdb?

linked table?

what is the WHERE clause?
is Access wrap -1 or 0 in quotes
(which you don't want obviously)?

something like:

WHERE fSomething([SomeField]) = -1

what happens if you change function
to return string of either "TRUE" or "FALSE"?

is function inordinately complicated
(takes long time to run)?
 
G

Guest

Hi Crystal,

I don't believe I can make it into an equation. I do database and file
system searches. For the code to the functions see the reply to Gary Walter.


Regards,
Leif
 
G

Guest

FYI, I'm simplified the FileFound function to the following, and I still get
an error when entering a criteria against the field:

Public Function FileFound(Unit As String, docType As Byte, docSubType As
Byte, APC As String) As Boolean
FileFound = True
End Function
 
G

Guest

I found the problem. I guess in one or more of my calls to the function I
was passing a null value as a parameter. The parameters were defined as
either a Byte or String, so that could not handle a null.

I thought I was covered because for each parameter I was passing I had a IS
NOT NULL in the critieria for that parameter. However, I guess Access must
fully execute the function prior to applying the crtieria values. Once I
added the nz function for my function parameters it worked.

Regards,
Leif
 
S

strive4peace

Hi Leif,

Glad you got it! Wish you continued success with your
database :)

Are you from Norway?

Warm Regards,
Crystal
Microsoft Access MVP 2006

*
Have an awesome day ;)

remote programming and training
strive4peace2006 at yahoo.com

*
 
G

Guest

Hi Crystal,

No, So. California. My last name, Eriksen (of course), is from Denmark.

Regards,
Leif
 
S

strive4peace

thanks, just curious :)

Warm Regards,
Crystal
Microsoft Access MVP 2006

*
Have an awesome day ;)

remote programming and training
strive4peace2006 at yahoo.com

*
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top