473,396 Members | 2,002 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,396 software developers and data experts.

Type mismatch within user function

Hi. I'm using a function which I wrote (based on a forum reply to someone else). It did work fine but I've just come back to it after a few days and other coding, and now it is generating a Type Mismatch error.

All it is trying to do is to find an age group value based on a date of birth (eg "U15", or "senior"). The base table has fields: agegroup_id : auto, agegroup : text, agegroup_oldest : date/time.

Expand|Select|Wrap|Line Numbers
  1. Function AgeGroup(aDate As Date) As String
  2.  
  3. 'This function takes an argument aDate as athlete's date of birth and looks up the textual age group
  4.  
  5. Dim tempvar As String
  6. Dim qdf As QueryDef
  7. Dim rst As Recordset
  8. Dim qdf1 As QueryDef
  9. Dim rst1 As Recordset
  10.  
  11. Set qdf = CurrentDb.CreateQueryDef("", "SELECT T1.agegroup FROM t_agegroup AS T1 WHERE T1.agegroup_oldest =(SELECT Min(T2.agegroup_oldest)FROM t_agegroup AS T2 WHERE T2.agegroup_oldest>=[value];);")
  12. qdf.Parameters("Value") = (aDate)
  13.   Set rst = qdf.OpenRecordset
  14.  If rst.RecordCount > 0 Then tempvar = rst!AgeGroup
  15.    rst.Close
  16.  
  17. AgeGroup = tempvar
  18. End Function
A couple of hours of looking at this has not enlightened me!

Any thoughts greatly appreciated.

Thanks
Aug 2 '09 #1
7 2735
ADezii
8,834 Expert 8TB
@carl1957
The code appears to be working just fine:
  1. Are you Declaring a Variable as a String to accept the Return Value of the Function?
  2. Try passing an Explicit Date to the AgeGroup() Function.
  3. Both points are illustrated below:
    Expand|Select|Wrap|Line Numbers
    1. Dim strRetVal As String      'Return Value should be a String
    2.  
    3. 'Try passing Explicit Dates and see what happens
    4. strRetVal = AgeGroup(#12/30/2000#)
    5. strRetVal = AgeGroup(CDate(Me![txtDOB]))
    6.  
    7. MsgBox "The appropriate Age Group is: " & strRetVal
Aug 2 '09 #2
Hi. Thanks for quick reply. Yes, had tried that with same result. Test code is:
Private Sub Command13_Click()
On Error GoTo Err_Command13_Click

Dim xx As String

xx = AgeGroup(#11/1/1989#)
MsgBox xx

Exit_Command13_Click:
Exit Sub

Err_Command13_Click:
MsgBox Err.Description
Resume Exit_Command13_Click
End Sub
I'm sure it must be something obvious since I am sure I didn't change it between when it worked and when it didn't!

Have also tried embedding a date within the function and taking out the final assignment. Same result.

Cheers
Aug 2 '09 #3
ADezii
8,834 Expert 8TB
@carl1957
  1. Try changing the Return Type of the Function to Variant, either:
    Expand|Select|Wrap|Line Numbers
    1. Function AgeGroup(aDate As Date) 
    2.               OR
    3. Function AgeGroup(aDate As Date) As Variant
    4.  
  2. Qualify your Declarations with the appropriate Type Library:
    Expand|Select|Wrap|Line Numbers
    1. Dim tempvar As String
    2. Dim qdf As DAO.QueryDef
    3. Dim rst As DAO.Recordset
    4. Dim qdf1 As DAO.QueryDef
    5. Dim rst1 As DAO.Recordset
Aug 2 '09 #4
Hi. Thanks. Specifying DAO solved it. Why would I need to do that? Would the format have been different if ADO used?

Again thanks - appreciated.
Aug 2 '09 #5
ADezii
8,834 Expert 8TB
@carl1957
You probably have References set to both the DAO and ADO Object Libraries, in which case you should 'always' qualify the Library to which each Object belongs to avoid any potential Errors and ambiguity. If you do not qualify them, then the first Type Library listed in the References Dialog will be used, which you may/may not want.

P.S. - In your specific case, I would venture that the Reference to ADO is set prior to DAO. When the Compiler reaches this line of code...
Expand|Select|Wrap|Line Numbers
  1. Dim qdf As QueryDef
you will get a Type mismatch, since it will use the Higher Priority ADO Library which contains no such QueryDef animal.
Aug 2 '09 #6
Brilliant - many thanks for your time.
Aug 2 '09 #7
ADezii
8,834 Expert 8TB
@carl1957
You are quite welcome.
Aug 2 '09 #8

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

Similar topics

0
by: Sue Adams | last post by:
I actually have two issues/questions: I have an autonumber field in an access db table that I grab and later use to update a record in another table withing the same db. The code I use to get...
1
by: Mark | last post by:
Hi - I tried this in VS.Net, and also in the Web Matrix code below: - but I am getting a type mismatch error. The sql statement runs perfectly from within the Access Query Designer. Can anyone...
3
by: Laurel | last post by:
this is driving me crazy. i need to use a form control as a criteria in a select query, and the control's value is set depending upon what a user selects in an option group on the form. the query...
3
by: martlaco1 | last post by:
Trying to fix a query that (I thought) had worked once upon a time, and I keep getting a Data Type Mismatch error whenever I enter any criteria for an expression using a Mid function. Without the...
1
by: Brett | last post by:
I have a form that calls a method within a DLL. By clicking a button on the form, the DLL is instantiated and the SaveOutlookMessage() method invoked. The DLL code copies messages from Outlook to...
2
by: igor.barbaric | last post by:
Hello! I have created a very simple query like this: SELECT Tasks.Name, DurationHrs(,) AS Duration FROM Tasks INNER JOIN Log ON Tasks.TaskID=Log.TaskID; The above query works fine....
17
by: The Frog | last post by:
Hello everyone, I am working on an application that can build database objects in MS Access from text files. I suppose you could call it a backup and restore type routine. Accessing the...
3
by: cyberdwarf | last post by:
Hi Any idea why the following (cut down) function gives a "Type Mismatch" error? I've tried almost everything, in terms of arrays, etc, but I need to pass back 3 values (int, int, double)...
10
by: lasmith329 | last post by:
I've never posted a question on any site before, but after racking my head over this hurdle for days, I've caved. I'm working on a program that creates a kml file and exports it to Google Earth. In...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.