473,394 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Data Mismatch for UDF called from Where clause

Hello,

I started working on VBA,access from yesterday, and here I am with my first problem :). Please help.

I get the above mentioned problem when my UDF is returning an integer.(Following is the UDF):
**************************************************
Expand|Select|Wrap|Line Numbers
  1. Function Missing(a As String) As Integer
  2. Static count_mis As Integer
  3. On Error GoTo Err_Trap
  4.  
  5.  If a = "M" Then
  6.   count_mis = 1
  7.  ElseIf a = "I" Then
  8.   count_mis = 2
  9.  ElseIf IsNull(a) = True Then
  10.   count_mis = 3
  11.  ElseIf IsEmpty(a) = True Then
  12.   count_mis = 4
  13.  ElseIf IsError(a) = True Then
  14.   count_mis = 5
  15.  Else
  16.   count_mis = 6
  17.   End If
  18.  
  19.   Missing = count_mis
  20.  
  21. Exit_Trap:
  22.    Exit Function
  23. Err_Trap:
  24.    MsgBox Err.Description
  25.    Missing = 2
  26.    Resume Exit_Trap
  27. End Function
  28. ************************************************
The argument passed to the UDF is a field which is either I or M or blank. The call to the UDF in the Where clause is:
(Missing([Missing data summary].Field40) = 1)

But I get data mismatch here. Is it beacause of the blank values in Field40,( which show an error in the retrieved table,if I run the query withouth this criteria in where clause). I get a 1 and 2 respectively corresponding to "M" or "I" in the Field40

Thank you!
Jun 4 '10 #1
10 1890
Sorry Forgot to mention,I need only those records to be selected which have a "M" in Field40 of Missing data summary table. I have written this complex logic as I need to further modify it such that the return value from the UDF will be derived from a logic on values of 30 fields in the record including field40. Thanks!!
Jun 4 '10 #2
OldBirdman
675 512MB
Try:
Expand|Select|Wrap|Line Numbers
  1. (Missing([Missing data summary].Field40)) = 1
The outer parens () are redundant, but might be needed when you expand to 30 fields.
Jun 4 '10 #3
@OldBirdman
@OldBirdman

Thank you so much for the prompt reply. The mismatch is gone but I get an Unknown Jet error now. I do not know what that is,so will get back once i know. Thanks
Jun 4 '10 #4
well, not sure what happened, but without changing much (or anything) I am back to the data mismatch error now. This is after i included the redundant parenthesis :) does this give us any clue?
Jun 4 '10 #5
OldBirdman
675 512MB
Not really!
The function should be "Public" and in a Standard Module, not a Class Module.
Can you post the SQL statement?
Jun 4 '10 #6
Yepp it is in standard module and have made it public now. same result though. Here is the SQL query(smaller version,all that is required now and goes down with same error):

SELECT [Missing data summary].Field40
FROM [Missing data summary]
WHERE((Missing([Missing data summary].Field40)) Like 1);
Jun 4 '10 #7
OldBirdman
675 512MB
What immediately stands out is that there is no blank after the keyword WHERE
If that is not the problem, open a new query in Design View. Close the "Show Table" window without using it. Click SQL on the query toolbar (leftmost icon). Paste your SQL statement into the textbox
Switch to DataSheet View (leftmost icon).
If it works, the problem is elsewhere in your code.
If it doesn't work, put a BreakPoint in your function "Missing" on the line [If a = "M" Then]. Step through the code to be sure it is working as you expect.
Switch to Design View for the query. This should have your function on the "Criteria:" row of the design grid.

I must leave now for a couple of hours. Good luck!
Jun 4 '10 #8
Hi, Thank you for you reply again. I tried out what you told me,the query does not work either way. I am unable to figure out what is wrong using breakpoints. However I have broken down the problem to the following:

There are 3 possible values of the field to be passed to
the function they are "m", "i" (both as String) and a NUll. The null causes the function to error, and even the trap i have set does not catch the error.

I tried to get around this by using a subquery in the FROM statement to filter out non-Null values only to which the where clause will be applied. If I do not use the Where clause the query works fine, but witht the where clause i again get data mismatch error.

Not sure why, when the values passed is an integer and I have an integer in the criteria condition.Following are the simplified query and code.

code:
Public Function Missing(Optional a As String) As Integer
On Error GoTo Err_Trap
Dim count_mis As Integer

If a = "M" Then
count_mis = 1
ElseIf a = "I" Then
count_mis = 2
Else: IsNull (a)
count_mis = 6
End If

Missing = count_mis

Exit_Trap:
Exit Function

Err_Trap:
MsgBox Err.Description
Missing = 2
Resume Exit_Trap

End Function

Query:
SELECT [Missing data summary].Field40, Missing([Missing data summary].Field40)
FROM (SELECT [Missing data summary].Field40 FROM [Missing data summary] WHERE [Missing data summary].Field40 IS Not Null)
WHERE (Missing([Missing data summary].Field40) = 1);

Thanks!!
Jun 7 '10 #9
Thank you very much. I got this. The Data Mismatch wasnt in the return value passed back to the query but the parameter passed by the query. Just had to accept the parameter in the UDF as a variant data type.
Jun 7 '10 #10
OldBirdman
675 512MB
I'm glad you figured it out yourself. Your complete statement of what you tried, and what happened was quite clear. Sometimes a carefully composed question can help solve a problem better than any help a forum can give.
Jun 7 '10 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Christian | last post by:
HI, I have a function that is used to constrain a query: Select COl1, Col2 From MyTable WHERE col1 = ... AND col2 = ... And MyFunction(col1) = ... My problem is that MyFunction is executed...
14
by: joshsackett | last post by:
I have a WHERE clause that could be an "=" or a "LIKE" depending upon if the passed variable is populated or not. I would like to know the best way to write the WHERE clause to make it dynamically...
1
by: msnews.microsoft.com | last post by:
Hi All, When I use a SQL Query to Fill the Dataset if I use the following Where Clause ( > '100' ) Then I am getting a 'Data Type Mismatch Error' because the Field Type is double.
7
by: Britney | last post by:
Original code: this.oleDbSelectCommand1.CommandText = "SELECT TOP 100 user_id, password, nick_name, sex, age, has_picture, city, state, " + "country FROM dbo.users WHERE (has_picture = ?) AND (sex...
2
by: Zygo Blaxell | last post by:
I have a table with a few million rows of temperature data keyed by timestamp. I want to group these rows by timestamp intervals (e.g. every 32 seconds), compute aggregate functions on the...
2
by: The Other Mike | last post by:
VS 2005 Is their a way to dynamically change the where clause in a datagridview or dataset? I have a search window where the user can enter a part number or description or customer etc... and...
6
by: Altman | last post by:
I am playing around with making a dataset with the designer in vb.net 2005. I realize you can add parameters to select statement and then filter off of those parameters. What I would like to do...
2
by: Jim.Mueksch | last post by:
I am having a problem with using calculated values in a WHERE clause. My query is below. DB2 gives me this error message: Error: SQL0206N "APPRAISAL_LESS_PRICE" is not valid in the context where...
4
by: Gregg K | last post by:
I'm using Office 2003 on Xp sp2. Please notice that this VBA code works and produces a record set which contains the desired data if the WHERE clause is omitted. When, however, the code is run...
9
by: Yitzak | last post by:
Hi spent a few hours on this one wrote a query that joined on results of 2 other queries. Qry3 using Qry1 and Qry2 When I used Qry1.FasText <cstr(Qry2.FasInteger) in the where clause - got...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.