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

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])

Related

MS Access Query date range when last date not found

I have the following update query in MS Access 2013
UPDATE WXObs SET WXObs.SnowFlag = 1
WHERE (((WXObs.StationID) ="451409") And(
(WxObs.ObsDate) Between #1/3/2003# AND #3/29/2003# OR
(WxObs.ObsDate) Between #11/16/2003# AND #5/7/2004# OR
(WxObs.ObsDate) Between #10/30/2004# AND #4/30/2005#));
This works until the end date in the range is not found. For instance, if 5/7/2004 is not in the data set, then the update continues to the next end date, in this case 4/30/2005.
I would prefer it ended on the last date in the range. For instance, if the data ended on 4/21/2004, that would be last field updated between 11/16/ and 5/7/2004. The query would then continue to update again beginning on 10/30/2004.
I have tried < and <=
Thanks
You're missing some parentheses that are affecting the evaluation order, causing the behavior that you're reporting.
What you want is for each of the BETWEEN portions to be evaluated completely before the OR option is evaluated, and you need to make sure that evaluation is done by surrounding the BETWEEN expressions in parentheses to guarantee the evaluation order.
This should correct it (untested, as you've not provided the test data necessary to create a test case).
UPDATE WXObs SET WXObs.SnowFlag = 1
WHERE
(WXObs.StationID ="451409")
And
(
(WxObs.ObsDate Between #1/3/2003# AND #3/29/2003#) OR
(WxObs.ObsDate Between #11/16/2003# AND #5/7/2004#) OR
(WxObs.ObsDate Between #10/30/2004# AND #4/30/2005#)
);

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

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#

UPDATE QUERY - Sum up a value from form with value from table

I've just started using microsoft access so I don't really know how to solve this. I would like to use an update query to add a value from a form to a value on a table.
I originally used the SUM expression which gave me an error saying it was an aggregate function.
I also tried to add the two values together (e.g [field1] + [field2]) which as a result gave me a value with both numbers together instead of adding them together.
The following is the SQL I'm using:
UPDATE Votes
SET Votes.NumVotes = [Votes]![NumVotes]+[Forms]![frmVote]![txtnumvotes]
WHERE (((Votes.ActID) = [Forms]![frmVote]![combacts])
AND ((Votes.RoundNum) = [Forms]![frmVote]![combrndnum]))
I want to add a value [txtnumvotes] a form to a field [NumVotes] from the table [Votes].
Could someone please help me?
You can specify the expected data type with parameters:
PARAMETERS
[Forms]![frmVote]![txtnumvotes] Short,
[Forms]![frmVote]![combacts] Long,
[Forms]![frmVote]![combrndnum] Long;
UPDATE
Votes
SET
Votes.NumVotes = [Votes]![NumVotes]+[Forms]![frmVote]![txtnumvotes]
WHERE
(((Votes.ActID) = [Forms]![frmVote]![combacts])
AND
((Votes.RoundNum) = [Forms]![frmVote]![combrndnum]))
Without the specification, Access has to guess, and that sometimes fails.

Firebird " Column does not belong to referenced table "

I am trying to create my first procedure on firebird 2.5 by using ibexpert gui.
The procedure will return 'PROCESS_DATE' which belongs to a specific 'PROCESS_ID'. I prepared following code:
begin
OUTPUT_DATE = (select PROCESS_DATE from PROCESSES
where PROCESS_ID = INPUT_ID);
suspend;
end
input parameter : 'INPUT_ID' --> type 'INTEGER'
output parameter : 'OUTPUT_DATE' --> type 'DATE'
But when I tried to compile it returns this error:
Column does not belong to referenced table.
Dynamic SQL Error.
SQL error code = -206.
Column unknown.
INPUT_ID.
At line 9, column 48.
I do not know how to deal with this error.
I tried to find solutions on other questions also the internet but i couldn't find a basic, understandable answer for beginners. thanks for helps.
Try this:
CREATE PROCEDURE MyP (INPUT_ID INTEGER)
RETURNS (OUTPUT_DATE DATE)
AS
BEGIN
FOR
SELECT PROCESS_DATE FROM PROCESSES
WHERE PROCESS_ID = :INPUT_ID
INTO :OUTPUT_DATE
DO
SUSPEND;
END
Always prepend parameter names with ":". The only place where ":" is not allowed is at the left side of "=" operator.

DISTINCT is not working

SQL query in Ms-Access
INSERT INTO tblTmpEventLog( TrackingNumber, PartNumber, PartNumberChgLvl,
EnteredBy, EventTypeSelected, EventDate )
SELECT DISTINCT tblRevRelLog_Detail.RevRelTrackingNumber,
tblRevRelLog_Detail.PartNumber, tblRevRelLog_Detail.ChangeLevel,
[Forms]![frmEventLog_Input]![EnteredBy] AS EnteredBy,
[Forms]![frmEventLog_Input]![EventTypeSelected] AS EventTypeSelected,
CDate([Forms]![frmEventLog_Input]![EventDate]) AS EventDate
FROM tblRevRelLog_Detail LEFT JOIN tblEventLog
ON (tblEventLog.PartNumber = tblRevRelLog_Detail.PartNumber)
AND (tblEventLog.PartNumberChgLvl = tblRevRelLog_Detail.ChangeLevel)
WHERE ((([tblRevRelLog_Detail]![RevRelTrackingNumber]) =
[Forms]![frmEventLog_Input]![TrackingNumber]))
AND ((tblEventLog.PartNumber) NOT IN
(SELECT tblEventLog.PartNumber FROM tblEventLog
WHERE tblEventLog.EventTypeSelected = 'pn REMOVED From Wrapper'
AND tblEventLog.TrackingNumber =
tblRevRelLog_Detail.RevRelTrackingNumber
AND tblEventLog.PartNumber = tblRevRelLog_Detail.PartNumber
AND tblEventLog.PartNumberChgLvl =
tblRevRelLog_Detail.ChangeLevel
));
DISTINCT keyword for EnteredBy, EventTypeSelected is not working..I mean, data for these columns is not displaying when I use DISTINCT keyword.
EVENTDATE is working fine, but I do not understand why is it not displaying for EneteredBy and EventTypeSelected columns.
Can anyone tell me how to handle this?
It may be that the query can't interpret properly from the form directly as the final data type. However in your date field, you are wrapping it in a function CDATE( ... ). So, the SQL engine knows the result type. I would suggest doing the same for the other fields. Ex: doing a CAST ( ...your form control... as DateTime ) as OtherColumn, etc... I THINK Access allows casting, but not positive. Otherwise, pre-pull the form value into a declared data type variable and use THAT variable in the query AS OtherColumn as you are doing.
Additionally to what #Jack mentioned, you can always go back to your account, look at your historical question, and click on whatever answers actually helped / solve your problems. Some questions never do get answers and that's ok, just give credit to those that DO help.
I have found in the past (I don't remember which old version of Access this was) that if you set the value of a form control in VBA, and then use that control in a query, the query will not see the value you set in VBA. If the user edits the control normally, the query sees the expected value. Perhaps that's what happened here.
To work around that, you can declare a VBA function that returns the desired value. For example, instead of this:
SELECT ..., Forms!MainForm!TextEntry AS TextEntry, ... FROM ...
use this:
SELECT ..., GetTextEntry() AS TextEntry, ... FROM ...
along with this:
Public Function TextEntry() As Variant
TextEntry = Forms!MainForm!TextEntry
End Function