convert text field to a date/time field in access query not working - sql

I'm working with a access database and vb6. My table has a field named "InvoiceDate" which is a text field. I'm not allowed to make database modifications. So I guess my only option is to change the text field into a date/time field in my query. I found several methods to do so. They are as follows.
Format(InvoiceDate, "yyyy/mm/dd")
(DateSerial(Left(InvoiceDate,4),Mid(InvoiceDate,5,2),Right(InvoiceDate,2))
Between #2015/01/01# And #2016/01/01#))
DateValue(InvoiceDate, "yyyy/mm/dd")
CDate(InvoiceDate, "yyyy/mm/dd")
But those 4 methods didn't work. I can't figure out this.
The query I'm using as follows
SELECT Invoice.InvoiceDate, InvoicedProduct.InvoiceType, Invoice.InvoiceStatus,
Invoice.RetailerID, Invoice.DailySalesID, Invoice.RepID,
InvoicedProduct.Quantity, InvoicedProduct.UnitRate,
InvoicedProduct.TotalItemValue
FROM Invoice
INNER JOIN InvoicedProduct
ON (Invoice.DailySalesID = InvoicedProduct.DailySalesID)
AND (Invoice.RepID = InvoicedProduct.RepID)
AND (Invoice.InvoiceID = InvoicedProduct.InvoiceID)
WHERE (((InvoicedProduct.ProductID)='9010001174130.4')
AND (DateValue(Invoice.InvoiceDate) Between #2015/01/01# And #2016/01/01#))
GROUP BY Invoice.InvoiceDate, InvoicedProduct.InvoiceType, Invoice.InvoiceStatus,
Invoice.RetailerID, Invoice.DailySalesID, Invoice.RepID,
InvoicedProduct.Quantity, InvoicedProduct.UnitRate,
InvoicedProduct.TotalItemValue
HAVING (((InvoicedProduct.InvoiceType)='Invoice' OR (InvoicedProduct.InvoiceType)='Sound')
AND ((Invoice.InvoiceStatus)='VALID'))
ORDER BY Invoice.InvoiceDate;
This gives me the error "Data Type mismatch in criteria expression"
Following two types are include in my InvoiceDate Field
2016/01/04 10:00: AM and 2016/01/20 08:25 PM
The only difference is the colon after the time
Please help.
Thank you.

Your criteria:
DateValue(Invoice.InvoiceDate) Between #2015/01/01# And #2016/01/01#
is correct, so the error message indicates, that one or more of your text dates in InvoiceDate don't represent a valid date, like 2015-06-31 or Null.
Run a query to check this:
Select *, IsDate(InvoiceDate) As ValidDate From Invoice
and see if any of the values of ValidDate are False.
To ignore the extra colon:
DateValue(Replace(Invoice.InvoiceDate, ": ", " ")) Between #2015/01/01# And #2016/01/01#

Related

How can I refer to 2 differents dataset into a gsheet query formula?

I'm trying to get a formula to match 2 different data set in google sheets.
I did the following but I keep getting "parse error"
iferror(if(len($D10)> 0,
query({'Sheet1'!$A$2:$T;'Sheet2'!$A$2:$BK}
"select {'Sheet2'!$A$2:$BK}.B where {'Sheet2'!$A$2:$BK}.A contains 'condition2'
and {'Sheet2'!$A$2:$BK}.D contains'"&$D$4&"
and {'Sheet1'!$A$2:$T}.N >= date '"&text(($A$4), "YYYY-MM-DD")&"'
and {'Sheet1'!$A$2:$T}.N <= date '"&text(($B$4), "YYYY-MM-DD")&"'",0),""),"")
With D10 is the cell that I'll look up the match for.
Let me know if you have any tips and thanks!
The syntax for QUERY is QUERY(data, query, [headers])
In your case, you forgot the , between the data and the query.
It should be ...query({'Sheet1'!$A$2:$T;'Sheet2'!$A$2:$BK}, "select {'Sheet2'!$A$2:$BK}.B where...

How to calculate Employee Service in MS Access and save in Table Fields. (not via query)

I am facing regarding the EmployeeServiceWithUs (Field in table, datatype is Number) in "years".
In TxtServiceWithUs I use the following and Result is OK:
Control Source=DateDiff("yyyy",[DateOfJoing],Date())
But in following:
Private Sub EmployeeServiceWithUs_LostFocus()
[EmployeeServiceWithUs] = [DateOfJoing] & (DateDiff("yyyy", [DateOfJoing], Date))
End Sub
I get:
error "2113 The value you enter is not valid for this field.
Is the able syntax is wrong?
DateDiff calculates the difference in calendar years, not _years of service, or age or similar. You will need a custom function like AgeSimple found here.
Then your ControlSource becomes:
=AgeSimple([DateOfJoing])
To update a (temporary) table:
Update
YourTable
Set
[EmployeeServiceWithUs] = AgeSimple([DateOfJoing])

How to concatenate date variables to create between in where condition

While I am trying to set my value to between the user given start date and end date for a query, I am running into a run-time error 3071 (The expression is typed incorrectly or it is too complex)
This is being used to pass a user given variable from a form to a query
Please see below
WHERE (
IIf([Forms]![Find]![Entity]<>"",DB.Entity=[Forms]![Find]![Entity],"*")
AND IIf([Forms]![Find]![AEPS]<>"",DB.AEPSProgram=[Forms]![Find]![AEPS],"*")
AND IIf([Forms]![Find]![DeliveryType]<>"",DB.DeliveryType=[Forms]![Find]![DeliveryType],"*")
AND IIf([Forms]![Find]![ReportingYear] is not Null,DB.ReportingYear=[Forms]![Find]![ReportingYear],"*")
AND IIf([Forms]![Find]![Price] is not Null,DB.Price=[Forms]![Find]![Price],"*")
AND IIf([Forms]![Find]![Volume]is not Null,DB.Volume=[Forms]![Find]![Volume],"*")
AND IIf([Forms]![Find]![sDate] is not Null AND [Forms]![Find]![eDate] is not Null,DB.TransactionDate= ">" & [Forms]![Find]![sdate] & " and <" & [Forms]![Find]![edate],"*")
);
If I set it equal to one of the dates it works as expected. I assume I am missing something with how I am joining the dates
Thank you
This (for the date field only) works:
WHERE (((DB.TransactionDate)>[Forms]![Find]![sdate] And (DB.TransactionDate)<[Forms]![Find]![edate])) OR ((([Forms]![Find]![sDate]+[Forms]![Find]![eDate]) Is Null));
Don't use *, it is for use with Like.
Consider assigning NULL condition to corresponding column via NZ() which sets column equal to itself and so avoids filtering any rows by that respective condition. Asterisks alone does not evaluate to a boolean condition unless using LIKE operator.
WHERE DB.Entity = NZ([Forms]![Find]![Entity], DBEntity)
AND DB.AEPSProgram = NZ([Forms]![Find]![AEPS], DB.AEPSProgram)
AND DB.DeliveryType = NZ([Forms]![Find]![DeliveryType], DB.DeliveryType)
AND DB.ReportingYear = NZ([Forms]![Find]![ReportingYear], DB.ReportingYear)
AND DB.Price = NZ([Forms]![Find]![Price], DB.Price)
AND DB.Volume = NZ([Forms]![Find]![Volume], DB.Volume)
AND DB.TransactionDate >= NZ([Forms]![Find]![sdate], DB.TransactionDate)
AND DB.TransactionDate <= NZ([Forms]![Find]![edate], DB.TransactionDate)
;

How do I use a Parameter form to select whether or not I want null values?

I am working on a database that my school uses for student recruitment. I want to include an option on the parameter form used to generate a students contact information that allows the user to select whether or not to include "applied" students in that contact list. I am currently trying to do that by using a select query that pulls data based on whether or not the "IdNumber" field is null. When I run the attached SQL, it doesn't pull any data at all. Any help would be appreciated!
SELECT InterestCards.NotStudentID, InterestCards.Email, InterestCards.YearOfGraduation,
InterestCards.SourceTwoLocation, InterestCards.FirstName, InterestCards.LastName,
InterestCards.Program, InterestCards.Inactive, InterestCards.IDNumber
FROM InterestCards
WHERE (((InterestCards.Email)<>"no email")
AND ((InterestCards.YearOfGraduation) Like [Forms]![MassCommunicationTool]![GradYear] & "*")
AND ((InterestCards.SourceTwoLocation) Like [Forms]![MassCommunicationTool]![FairName] & "*")
AND ((InterestCards.Inactive)=No)
AND ((InterestCards.IDNumber)=Switch([Forms]![MassCommunicationTool]![IncludeApplied]=0,Null,[forms]![MassCommunicationTool]![IncludeApplied]=(-1),([InterestCards].[IDNumber]) Is Not Null Or ([InterestCards].[IDNumber]) Is Null))
AND ((InterestCards.HighSchool) Like [Forms]![MassCommunicationTool]![HighSchool] & "*")
AND ((InterestCards.SourceThreeType) Like [Forms]![MassCommunicationTool]![SelectTerm] & "*")
AND ((InterestCards.DateEntered) Between Nz([Forms]![MassCommunicationTool]![StartDate],#1/1/1900#) And Nz([Forms]![MassCommunicationTool]![EndDate],#1/1/2199#)));
Operators cannot be dynamic in a query object. Cannot be selected via a conditional expression. However, parameters can be dynamic. Suggest you construct a field with an expression that handles the nulls to return either of 2 values:
IIf([IDNumber] Is Null, "XXXX", "AAAA") AS Grp
Then to select either the Null or not Null records, apply conditional parameter to that field:
Grp = IIf([Forms]![MassCommunicationTool]![IncludeApplied]=0, "XXXX", "AAAA")
I've never used dynamic parameterized queries. I prefer VBA to construct filter criteria and apply to form or report.

Comparing Date Values in Access - Data Type Mismatch in Criteria Expression

i'm having an issue comparing a date in an access database. basically i'm parsing out a date from a text field, then trying to compare that date to another to only pull newer/older records.
so far i have everything working, but when i try to add the expression to the where clause, it's acting like it's not a date value.
here's the full SQL:
SELECT
Switch(Isdate(TRIM(LEFT(bc_testingtickets.notes, Instr(bc_testingtickets.notes, ' ')))) = false, 'NOT ASSIGNED!!!') AS [Assigned Status],
TRIM(LEFT(bc_testingtickets.notes, Instr(bc_testingtickets.notes, ' '))) AS [Last Updated Date],
bc_testingtickets.notes AS [Work Diary],
bc_testingtickets.ticket_id,
clients.client_code,
bc_profilemain.SYSTEM,
list_picklists.TEXT,
list_picklists_1.TEXT,
list_picklists_2.TEXT,
list_picklists_3.TEXT,
bc_testingtickets.createdate,
bc_testingtickets.completedate,
Datevalue(TRIM(LEFT([bc_TestingTickets].[notes], Instr([bc_TestingTickets].[notes], ' ')))) AS datetest
FROM list_picklists AS list_picklists_3
RIGHT JOIN (list_picklists AS list_picklists_2
RIGHT JOIN (list_picklists AS list_picklists_1
RIGHT JOIN (bc_profilemain
RIGHT JOIN (((bc_testingtickets
LEFT JOIN clients
ON
bc_testingtickets.broker = clients.client_id)
LEFT JOIN list_picklists
ON
bc_testingtickets.status = list_picklists.id)
LEFT JOIN bc_profile2ticketmapping
ON bc_testingtickets.ticket_id =
bc_profile2ticketmapping.ticket_id)
ON bc_profilemain.id =
bc_profile2ticketmapping.profile_id)
ON list_picklists_1.id = bc_testingtickets.purpose)
ON list_picklists_2.id = bc_profilemain.destination)
ON list_picklists_3.id = bc_profilemain.security_type
WHERE ( ( ( list_picklists.TEXT ) <> 'Passed'
AND ( list_picklists.TEXT ) <> 'Failed'
AND ( list_picklists.TEXT ) <> 'Rejected' )
AND ( ( bc_testingtickets.ticket_id ) <> 4386 ) )
GROUP BY bc_testingtickets.notes,
bc_testingtickets.ticket_id,
clients.client_code,
bc_profilemain.SYSTEM,
list_picklists.TEXT,
list_picklists_1.TEXT,
list_picklists_2.TEXT,
list_picklists_3.TEXT,
bc_testingtickets.createdate,
bc_testingtickets.completedate,
DateValue(TRIM(LEFT([bc_TestingTickets].[notes], Instr([bc_TestingTickets].[notes], ' '))))
ORDER BY Datevalue(TRIM(LEFT([bc_TestingTickets].[notes], Instr([bc_TestingTickets].[notes], ' '))));
the value i'm trying to compare against a various date is this:
DateValue(Trim(Left([bc_TestingTickets].[notes],InStr([bc_TestingTickets].[notes],' '))))
if i add a section to the where clause like below, i get the Data Type Mismatch error:
WHERE DateValue(Trim(Left([bc_TestingTickets].[notes],InStr([bc_TestingTickets].[notes],' ')))) > #4/1/2012#
i've even tried using the DateValue function around the manual date i'm testing with but i still get the mismatch error:
WHERE DateValue(Trim(Left([bc_TestingTickets].[notes],InStr([bc_TestingTickets].[notes],' ')))) > DateValue("4/1/2012")
any tips on how i can compare a date in this method? i can't change any fields in the database, ect, that's why i'm parsing the date in SQL and trying to manipulate it so i can run reports against it.
i've tried googling but nothing specifically talks about parsing a date from text and converting it to a date object. i think it may be a bug or the way the date is being returned from the left/trim functions. you can see i've added a column to the end of the SELECT statement called DateTest and it's obvious access is treating it like a date (when the query is run, it asks to sort by oldest to newest/newest to oldest instead of A-Z or Z-A), unlike the second column in the select.
thanks in advance for any tips/clues on how i can query based on the date.
edit:
i just tried the following statements in my where clause and still getting a mismatch:
CDate(Trim(Left([bc_TestingTickets].[notes],InStr([bc_TestingTickets].[notes],' ')))) > #4/1/2012#
CDate(Trim(Left([bc_TestingTickets].[notes],InStr([bc_TestingTickets].[notes],' ')))) >
CDate("4/1/2012") CDate(DateValue(Trim(Left([bc_TestingTickets].[notes],InStr([bc_TestingTickets].[‌​notes],' '))))) > #4/1/2012#
i tried with all the various combinations i could think of regarding putting CDate inside of DateValue, outside, ect. the CDate function does look like what i should be using though. not sure why it's still throwing the error.
here's a link to a screenshot showing the results of the query http://ramonecung.com/access.jpg. there's two screenshots in one image.
You reported you get Data Type Mismatch error with this WHERE clause.
WHERE DateValue(Trim(Left([bc_TestingTickets].[notes],
InStr([bc_TestingTickets].[notes],' ')))) > #4/1/2012#
That makes me wonder whether [bc_TestingTickets].[notes] can ever be Null, either because the table design allows Null for that field, or Nulls are prohibited by the design but are present in the query's set of candidate rows as the result of a LEFT or RIGHT JOIN.
If Nulls are present, your situation may be similar to this simple query which also triggers the data type mismatch error:
SELECT DateValue(Trim(Left(Null,InStr(Null,' '))));
If that proves to be the cause of your problem, you will have to design around it somehow. I can't offer a suggestion about how you should do that. Trying to analyze your query scared me away. :-(
It seems like you are having a problem with the type conversion. In this case, I believe that you are looking for the CDate function.
A problem might be the order of the date parts. A test in the Immediate window shows this
?cdate(#4/1/2012#)
01.04.2012
?cdate(#2012/1/4#)
04.01.2012
Write the dates backwards in the format yyyy/MM/dd and thus avoiding inadverted swapping of days and months!
DateValue("2012/1/4")
and
CDate(#2012/1/4#)