A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Running & Setting Up Queries
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Type mismatch in Function



 
 
Thread Tools Display Modes
  #1  
Old November 18th, 2009, 01:11 AM posted to microsoft.public.access.queries
PatK
external usenet poster
 
Posts: 111
Default Type mismatch in Function

Hoping someone might be able to help. I have the following Function that I
have used in excel 2007 (and it works fine), that I am trying to now use in
MS Access. Basically you pass a date string to the function and it returns a
fiscal year quarter (our fiscal years starts on Nov 1). The code, when runs,
gets a type mismatch whenever it gets to one of the lines coded (entire
function at bottom):

FiscalQtr = "FY" & Right(DatePart("yyyy", inDate + yeardays), 2) & strQtr

When I hover my mouse over inDate, I will see dates like I would expect
("11/14/2009"). All these fields are string, ALTHO when I used the
FiscalQtr() function in a query, the date I am passing is being converted
from a date format, to a string format, and that seems to be working fine.
Ie, in my query I have (design view), the following:

FiscalQ: fiscalqtr(Format$([sharepointlistname].[Prod (Month
Start)],'mm/dd/yyyy'))

Before I inserted the FiscalQtr function code, ie, had on they formatted
text, that output exactly what I expected. Here is the entire code.

Ideas? PatK

Option Compare Database

Function FiscalQtr(inDate As String) As String
Dim strQtr As String
Dim mm As Integer
Dim yeardays As Integer

If (DatePart("yyyy", inDate)) Mod 4 = 0 Then
yeardays = 364
Else
yeardays = 365
End If

strQtr = ""

Select Case DatePart("m", inDate)
Case 11, 12, 1
strQtr = " Q1"
Case 2, 3, 4
strQtr = " Q2"
Case 5, 6, 7
strQtr = " Q3"
Case 8, 9, 10
strQtr = " Q4"
Case Else
strQtr = ""
End Select
If strQtr " Q1" Then
FiscalQtr = "FY" & Right(DatePart("yyyy", inDate), 2) & strQtr
Else
If DatePart("m", inDate) 1 Then
Debug.Print Right(DatePart("yyyy", inDate + yeardays), 2)
FiscalQtr = "FY" & Right(DatePart("yyyy", inDate + yeardays), 2) &
strQtr
Else
FiscalQtr = "FY" & Right(DatePart("yyyy", inDate), 2) & strQtr
End If
End If


End Function
  #2  
Old November 18th, 2009, 01:41 AM posted to microsoft.public.access.queries
Bob Barrows
external usenet poster
 
Posts: 475
Default Type mismatch in Function

PatK wrote:
Hoping someone might be able to help. I have the following Function
that I have used in excel 2007 (and it works fine), that I am trying
to now use in MS Access. Basically you pass a date string to the
function and it returns a fiscal year quarter (our fiscal years
starts on Nov 1). The code, when runs, gets a type mismatch
whenever it gets to one of the lines coded (entire function at
bottom):

FiscalQtr = "FY" & Right(DatePart("yyyy", inDate + yeardays), 2) &
strQtr

When I hover my mouse over inDate, I will see dates like I would
expect ("11/14/2009"). All these fields are string, ALTHO when I
used the FiscalQtr() function in a query, the date I am passing is
being converted from a date format, to a string format, and that
seems to be working fine. Ie, in my query I have (design view), the
following:

FiscalQ: fiscalqtr(Format$([sharepointlistname].[Prod (Month
Start)],'mm/dd/yyyy'))


No. Don't return a string to a function that is expecting a date/time. Use
CDate()

FiscalQtr = "FY" & _
Right(DatePart("yyyy", CDate(inDate) + yeardays), 2) & strQtr

--
Microsoft MVP - ASP/ASP.NET - 2004-2007
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"


  #3  
Old November 18th, 2009, 12:37 PM posted to microsoft.public.access.queries
John Spencer
external usenet poster
 
Posts: 7,815
Default Type mismatch in Function

You might try the following expression:

IIF(IsDate([sharepointlistname].[Prod (Month Start)]),
"FY" & Format(DateAdd("M",2,CDate([sharepointlistname].[Prod (Month
Start)])),"yy \Qq")
,Null)

IF you wanted you could modify your FiscalQtr function

Public Function FiscalQtr(vDateIn)
If IsDate(vDateIn) Then
FiscalQtr ="FY" & Format(DateAdd("M",2,CDate(vDateIn)),"yy \Qq")
Else
FiscalQtr = Null
End IF
End Function


John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County

PatK wrote:
Hoping someone might be able to help. I have the following Function that I
have used in excel 2007 (and it works fine), that I am trying to now use in
MS Access. Basically you pass a date string to the function and it returns a
fiscal year quarter (our fiscal years starts on Nov 1). The code, when runs,
gets a type mismatch whenever it gets to one of the lines coded (entire
function at bottom):

FiscalQtr = "FY" & Right(DatePart("yyyy", inDate + yeardays), 2) & strQtr

When I hover my mouse over inDate, I will see dates like I would expect
("11/14/2009"). All these fields are string, ALTHO when I used the
FiscalQtr() function in a query, the date I am passing is being converted
from a date format, to a string format, and that seems to be working fine.
Ie, in my query I have (design view), the following:

FiscalQ: fiscalqtr(Format$([sharepointlistname].[Prod (Month
Start)],'mm/dd/yyyy'))

Before I inserted the FiscalQtr function code, ie, had on they formatted
text, that output exactly what I expected. Here is the entire code.

Ideas? PatK

Option Compare Database

Function FiscalQtr(inDate As String) As String
Dim strQtr As String
Dim mm As Integer
Dim yeardays As Integer

If (DatePart("yyyy", inDate)) Mod 4 = 0 Then
yeardays = 364
Else
yeardays = 365
End If

strQtr = ""

Select Case DatePart("m", inDate)
Case 11, 12, 1
strQtr = " Q1"
Case 2, 3, 4
strQtr = " Q2"
Case 5, 6, 7
strQtr = " Q3"
Case 8, 9, 10
strQtr = " Q4"
Case Else
strQtr = ""
End Select
If strQtr " Q1" Then
FiscalQtr = "FY" & Right(DatePart("yyyy", inDate), 2) & strQtr
Else
If DatePart("m", inDate) 1 Then
Debug.Print Right(DatePart("yyyy", inDate + yeardays), 2)
FiscalQtr = "FY" & Right(DatePart("yyyy", inDate + yeardays), 2) &
strQtr
Else
FiscalQtr = "FY" & Right(DatePart("yyyy", inDate), 2) & strQtr
End If
End If


End Function

  #4  
Old November 19th, 2009, 06:55 PM posted to microsoft.public.access.queries
PatK
external usenet poster
 
Posts: 111
Default Type mismatch in Function

Thanks to both of you on your replies. Question, John, for you: The
fiscalQtr funtion you provided below: That would have no notion of the start
of my fiscal year, right? That would only return standard "calendar" fiscal
quarters, right? Assume so, given I see no reference to that. But the code
intrigues me....THANKS!

patk

"John Spencer" wrote:

You might try the following expression:

IIF(IsDate([sharepointlistname].[Prod (Month Start)]),
"FY" & Format(DateAdd("M",2,CDate([sharepointlistname].[Prod (Month
Start)])),"yy \Qq")
,Null)

IF you wanted you could modify your FiscalQtr function

Public Function FiscalQtr(vDateIn)
If IsDate(vDateIn) Then
FiscalQtr ="FY" & Format(DateAdd("M",2,CDate(vDateIn)),"yy \Qq")
Else
FiscalQtr = Null
End IF
End Function


John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County

PatK wrote:
Hoping someone might be able to help. I have the following Function that I
have used in excel 2007 (and it works fine), that I am trying to now use in
MS Access. Basically you pass a date string to the function and it returns a
fiscal year quarter (our fiscal years starts on Nov 1). The code, when runs,
gets a type mismatch whenever it gets to one of the lines coded (entire
function at bottom):

FiscalQtr = "FY" & Right(DatePart("yyyy", inDate + yeardays), 2) & strQtr

When I hover my mouse over inDate, I will see dates like I would expect
("11/14/2009"). All these fields are string, ALTHO when I used the
FiscalQtr() function in a query, the date I am passing is being converted
from a date format, to a string format, and that seems to be working fine.
Ie, in my query I have (design view), the following:

FiscalQ: fiscalqtr(Format$([sharepointlistname].[Prod (Month
Start)],'mm/dd/yyyy'))

Before I inserted the FiscalQtr function code, ie, had on they formatted
text, that output exactly what I expected. Here is the entire code.

Ideas? PatK

Option Compare Database

Function FiscalQtr(inDate As String) As String
Dim strQtr As String
Dim mm As Integer
Dim yeardays As Integer

If (DatePart("yyyy", inDate)) Mod 4 = 0 Then
yeardays = 364
Else
yeardays = 365
End If

strQtr = ""

Select Case DatePart("m", inDate)
Case 11, 12, 1
strQtr = " Q1"
Case 2, 3, 4
strQtr = " Q2"
Case 5, 6, 7
strQtr = " Q3"
Case 8, 9, 10
strQtr = " Q4"
Case Else
strQtr = ""
End Select
If strQtr " Q1" Then
FiscalQtr = "FY" & Right(DatePart("yyyy", inDate), 2) & strQtr
Else
If DatePart("m", inDate) 1 Then
Debug.Print Right(DatePart("yyyy", inDate + yeardays), 2)
FiscalQtr = "FY" & Right(DatePart("yyyy", inDate + yeardays), 2) &
strQtr
Else
FiscalQtr = "FY" & Right(DatePart("yyyy", inDate), 2) & strQtr
End If
End If


End Function

.

  #5  
Old November 19th, 2009, 08:53 PM posted to microsoft.public.access.queries
John Spencer
external usenet poster
 
Posts: 7,815
Default Type mismatch in Function

When you add 2 months to the date you get a calculated date in the two months
in the future

So November 2009 dates yield a calculated date in January 2010.

If you use that calculated date you will get quarter 1 of year 2010 to show
for the date in November 2009

August 2010 will return 2010 Q3

So you should see your Fiscal Year and your Fiscal Quarter for any date.


John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County

PatK wrote:
Thanks to both of you on your replies. Question, John, for you: The
fiscalQtr funtion you provided below: That would have no notion of the start
of my fiscal year, right? That would only return standard "calendar" fiscal
quarters, right? Assume so, given I see no reference to that. But the code
intrigues me....THANKS!

patk

"John Spencer" wrote:

You might try the following expression:

IIF(IsDate([sharepointlistname].[Prod (Month Start)]),
"FY" & Format(DateAdd("M",2,CDate([sharepointlistname].[Prod (Month
Start)])),"yy \Qq")
,Null)

IF you wanted you could modify your FiscalQtr function

Public Function FiscalQtr(vDateIn)
If IsDate(vDateIn) Then
FiscalQtr ="FY" & Format(DateAdd("M",2,CDate(vDateIn)),"yy \Qq")
Else
FiscalQtr = Null
End IF
End Function


John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County

PatK wrote:
Hoping someone might be able to help. I have the following Function that I
have used in excel 2007 (and it works fine), that I am trying to now use in
MS Access. Basically you pass a date string to the function and it returns a
fiscal year quarter (our fiscal years starts on Nov 1). The code, when runs,
gets a type mismatch whenever it gets to one of the lines coded (entire
function at bottom):

FiscalQtr = "FY" & Right(DatePart("yyyy", inDate + yeardays), 2) & strQtr

When I hover my mouse over inDate, I will see dates like I would expect
("11/14/2009"). All these fields are string, ALTHO when I used the
FiscalQtr() function in a query, the date I am passing is being converted
from a date format, to a string format, and that seems to be working fine.
Ie, in my query I have (design view), the following:

FiscalQ: fiscalqtr(Format$([sharepointlistname].[Prod (Month
Start)],'mm/dd/yyyy'))

Before I inserted the FiscalQtr function code, ie, had on they formatted
text, that output exactly what I expected. Here is the entire code.

Ideas? PatK

Option Compare Database

Function FiscalQtr(inDate As String) As String
Dim strQtr As String
Dim mm As Integer
Dim yeardays As Integer

If (DatePart("yyyy", inDate)) Mod 4 = 0 Then
yeardays = 364
Else
yeardays = 365
End If

strQtr = ""

Select Case DatePart("m", inDate)
Case 11, 12, 1
strQtr = " Q1"
Case 2, 3, 4
strQtr = " Q2"
Case 5, 6, 7
strQtr = " Q3"
Case 8, 9, 10
strQtr = " Q4"
Case Else
strQtr = ""
End Select
If strQtr " Q1" Then
FiscalQtr = "FY" & Right(DatePart("yyyy", inDate), 2) & strQtr
Else
If DatePart("m", inDate) 1 Then
Debug.Print Right(DatePart("yyyy", inDate + yeardays), 2)
FiscalQtr = "FY" & Right(DatePart("yyyy", inDate + yeardays), 2) &
strQtr
Else
FiscalQtr = "FY" & Right(DatePart("yyyy", inDate), 2) & strQtr
End If
End If


End Function

.

  #6  
Old November 20th, 2009, 03:03 PM posted to microsoft.public.access.queries
PatK
external usenet poster
 
Posts: 111
Default Type mismatch in Function

This is "SO" cool. Makes so much sense. Neat way to use DateAdd. Works
great in my Excel code, as well. It's a beautiful thing. Thanks for opening
my eyes!

"John Spencer" wrote:

When you add 2 months to the date you get a calculated date in the two months
in the future

So November 2009 dates yield a calculated date in January 2010.

If you use that calculated date you will get quarter 1 of year 2010 to show
for the date in November 2009

August 2010 will return 2010 Q3

So you should see your Fiscal Year and your Fiscal Quarter for any date.


John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County

PatK wrote:
Thanks to both of you on your replies. Question, John, for you: The
fiscalQtr funtion you provided below: That would have no notion of the start
of my fiscal year, right? That would only return standard "calendar" fiscal
quarters, right? Assume so, given I see no reference to that. But the code
intrigues me....THANKS!

patk

"John Spencer" wrote:

You might try the following expression:

IIF(IsDate([sharepointlistname].[Prod (Month Start)]),
"FY" & Format(DateAdd("M",2,CDate([sharepointlistname].[Prod (Month
Start)])),"yy \Qq")
,Null)

IF you wanted you could modify your FiscalQtr function

Public Function FiscalQtr(vDateIn)
If IsDate(vDateIn) Then
FiscalQtr ="FY" & Format(DateAdd("M",2,CDate(vDateIn)),"yy \Qq")
Else
FiscalQtr = Null
End IF
End Function


John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County

PatK wrote:
Hoping someone might be able to help. I have the following Function that I
have used in excel 2007 (and it works fine), that I am trying to now use in
MS Access. Basically you pass a date string to the function and it returns a
fiscal year quarter (our fiscal years starts on Nov 1). The code, when runs,
gets a type mismatch whenever it gets to one of the lines coded (entire
function at bottom):

FiscalQtr = "FY" & Right(DatePart("yyyy", inDate + yeardays), 2) & strQtr

When I hover my mouse over inDate, I will see dates like I would expect
("11/14/2009"). All these fields are string, ALTHO when I used the
FiscalQtr() function in a query, the date I am passing is being converted
from a date format, to a string format, and that seems to be working fine.
Ie, in my query I have (design view), the following:

FiscalQ: fiscalqtr(Format$([sharepointlistname].[Prod (Month
Start)],'mm/dd/yyyy'))

Before I inserted the FiscalQtr function code, ie, had on they formatted
text, that output exactly what I expected. Here is the entire code.

Ideas? PatK

Option Compare Database

Function FiscalQtr(inDate As String) As String
Dim strQtr As String
Dim mm As Integer
Dim yeardays As Integer

If (DatePart("yyyy", inDate)) Mod 4 = 0 Then
yeardays = 364
Else
yeardays = 365
End If

strQtr = ""

Select Case DatePart("m", inDate)
Case 11, 12, 1
strQtr = " Q1"
Case 2, 3, 4
strQtr = " Q2"
Case 5, 6, 7
strQtr = " Q3"
Case 8, 9, 10
strQtr = " Q4"
Case Else
strQtr = ""
End Select
If strQtr " Q1" Then
FiscalQtr = "FY" & Right(DatePart("yyyy", inDate), 2) & strQtr
Else
If DatePart("m", inDate) 1 Then
Debug.Print Right(DatePart("yyyy", inDate + yeardays), 2)
FiscalQtr = "FY" & Right(DatePart("yyyy", inDate + yeardays), 2) &
strQtr
Else
FiscalQtr = "FY" & Right(DatePart("yyyy", inDate), 2) & strQtr
End If
End If


End Function
.

.

 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 01:19 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.