IF statement in VBA that proceeded incorrectly - vba

I have an Access VBA Function that sends a number of emails containing updated Excel spreadsheets, but only if the underlying data has been updated recently. This piece has been working just fine until last night when if seems that the IF statement incorrectly compared the system date to the data date and sent emails using the older data instead of sending a warning email to the person responsible for correcting the issue of having old data.
Access 2016 on a VM running Windows 10. The current data date is contained in a table and that date is correct. The system date is correct. Everything worked as it should except for comparing the dates. There were no failures or error codes generated.
Boiled down to the trouble spot the code reads ...
' check to see if data is out of date
If ERDBDate < Now() - 2 Then
' send notification of old data
Else
' send updated spreadsheets
End If
Although my experience in coding is not that great, I have never seen an IF statement improperly evaluate like this. All I can assume is either I am using a "shaky" method of comparing the dates or there was some kind of hickup on the server side that caused an incorrect system date to be returned (does this even seem possible?). I would rather it be me then not being able to rely on something as "simple" as the system date.
I have learned a tremendous amount from the answers and help posted on this site. Thanks for all your efforts!

DateDiff is the proven method for comparing date and time. Or use Date() to have no time (with DateDiff however, that doesn't matter):
' check to see if data is out of date
If DateDiff("d", ERDBDate, Date) >= 2 Then
' send notification of old data
Else
' send updated spreadsheets
End If

Try this statement:
If ERDBDate < DateAdd("d", -2, Now()) then

Related

Get date today, if no data - get data from yesterday (dynamic)

I have such an issue. I have a report that is built around today's date. We see data only from today. On Hand units from today.
However, we have many import issues and sometimes import fails and there is no data in this report. I was wondering if there may be a way to make it dynamic like to use MAX function with getdate() or anything similar. I always want to see data from today, however if there is no data available for today I would like to see the data from yesterday. If not from yesterday from a day before yesterday.
I need to change the date logic to just pull the data for the max date.
Does someone know how it can be done?
where sku.ohpost = cast(getdate()as date)
try this If column sku.ohpost type is date
where sku.ohpost = ( select max(sku.ohpost ) from mytable)

Check whether a form date field is equal to 1899-12-30 00:00:00.000

I know this should be simple but after hours of googling I am still failing...
I have a vb.net form application. Basically I have an SQL database with an Employee table with an EmpFinish column in datetime, null format (Employee Finish Date). The data is imported from a linked SQL DB (populated from an external app). Any "blank" dates show as 1899-12-30 00:00:00.000 in SQL.
The Employee table data is shown on a datagridview form, a row is selected, then I want to perform a check to test whether that rows EmpFinish date from the form is earlier or equal to today and is not equal to it's "blank" value i.e. has the employee left employment.
Code excerpt:
Dim currentDateTime = DateTime.Now
Dim selectedEmpFinishDate As DateTime =
EmployeesDataGridView.SelectedRows(0).Cells(5).Value
If selectedEmpFinishDate.CompareTo(currentDateTime) <= 0 Then
' code to do
End If
I didn't start out using the CompareTo() above util I started Googling this issue and it seems this is the best way to compare dates. Yes???
This test works for actual dates e.g. yesterday but also catches all 1899 dates, I have tried lots of tests (using 1 If statement of separate ones) to check for the 1899 blank dates but cannot get anything to work. If I debug the code at the test line, selectedEmpFinishDate supposedly equals #12/30/1899#. If I show the field value onscreen via
MsgBox("selectedFinDate = " & selectedEmpFinishDate) it reports as 00:00:00
I know this must be possible but cannot figure it out.
Could someone please offer a solution as I am running out of hair to pull out.
I have followed djv's advice (I had the same thought last night) and done an override to set these date values to NULL. I was then able to use IsDBNull() successfully.
To clarify where these dates came from... Data is imported into this database from another SQL DB created by a huge business software application. Not sure if they want the dates stored this way or whether the import sets them to be this "blank" default 1899 date.
They actually showed on all forms as 1899 etc so resetting them to NULL is the best option all around.
To anyone else who has these 1899 dates I would advise to update them to NULL.
Thank you to everyone who responded especially djv.

Using #Prompt in sql using SAP BO WEBI 4.2 SP3

I'm running a series of reports where time window called in query is rolling, and individual per report.. Some reports look 400 days back, others look 10 weeks back, while others again look at -40days/+80days... and so on - many options.
All reports are scheduled in daily or weekly runs, meaning setting prompts will require a manual reset of prompt for every instance through the scheduler. Not optimal.
I know the universe designer can design specific filters to drag into the queries using the query designer, but with so many different options, I find it a bit of an issue that the universe designer should create specific filters for these specific purposes, adding a vast number of specific filters intended for specific use to various universes.
I'm after an option where it is possible to assign a calculation to a date field, which stay fixed for the purpose of the report for every scheduled instance.
For instance, looking at invoice date from 400 days before today and onwards would look like Where DIM_TIME_INV.DAY_DAY >= sysdate -400 - This I can hardcode into the SQl of the specific report, and it will stay through the scheduler and roll 1 day for every day the report is run. But if I decide to make a change in the query elements, the SQl is screwed, and I will have to manually add the modification to the SQL again.
I found an article reg. the use of #Prompt and would ask universe designer to try and sandbox this in our version of BO.
While I'm being impatient, I try myself using following code based on example 4 from linked article.
SELECT
#select('DIM_TIME_INV.DAY_DAY') >= sysdate -(#prompt('Invoiced, days before today:','N',[DIM_TIME_INV.DAY_DAY],mono,free))
FROM
DIM_TIME_INV
Testing the SQL gives following error:
ORA-00936
SAP kba 2054721
The whole idea is to have a flexible yet consistent dimension that will calculate every time the report is run, without losing the code whenever new items are added to the report.
Does anyone know of a way to use the #Prompt in SQL for SAP WEBI 4.2? - Or any other way to have 'flexible' time dimensions where it is possible to set a from-date or to-date independently or even a range, without having universe designer creating a s**t-load of filters and dump in various universes.
Thanks // C
With regard to your example code, I think you're on the right track but your syntax has some issues:
SELECT
#select('DIM_TIME_INV.DAY_DAY') >= sysdate -(#prompt('Invoiced, days before today:','N',[DIM_TIME_INV.DAY_DAY],mono,free))
FROM
DIM_TIME_INV
First, both #Select and #Prompt must refer to universe objects, not columns. The syntax for both is: class name\object name. Assuming that the DIM_TIME_INV.DAY_DAY is associated with a universe object named Day Day in a class named Dim Time, the above code should be:
SELECT
#select('Dim Time\Day Day') >= sysdate -(#prompt('Invoiced, days before today:','N','Dim Time\Day Day',mono,free))
FROM
DIM_TIME_INV
Also note that the object reference in the #prompt call is delimited by single quotes, not brackets.
Next, I'm assuming that DAY_DAY is a date field. Its reference in the #prompt call would cause the prompt to display a list of values, sourced from DAY_DAY. But you want a numeric value from the prompt, not a date, so I would just leave that out, which will let the users enter a numeric value:
SELECT
#select('Dim Time\Day Day') >= sysdate -(#prompt('Invoiced, days before today:','N',,mono,free))
FROM
DIM_TIME_INV
Next, even with this corrected syntax, there will be an issue using this code as you have it. A good way to debug #prompt issues is to view the SQL in the WebI report after you get the error -- the SQL will show the rendered result, with all functions (#select and #prompt) expanded. For the above, you might get SQL like:
SELECT
DIM_TIME_INV.DAY_DAY >= sysdate -(400)
FROM
DIM_TIME_INV
This, of course, is invalid - you can't have a condition in the SELECT clause. If this is truly intended to be a condition (which I think it is, based on your objective), then it should be a predefined condition rather than a dimension.
With that said, I think you're on the right track for what you want to do. With the above corrections, you would have a predefined condition that you could drop into reports, which would enable the users to select the starting period (by number of days ago) for the report. You could create additional prompts with different logic, ex:
#select('Dim Time\Day Day') >= sysdate -(#prompt('Invoiced, weeks before today:','N',,mono,free) * 7)
or
#select('Dim Time\Day Day')
BETWEEN sysdate - #prompt('Starting days ago:','N',,mono,free)
AND sysdate - #prompt('Ending days ago:','N',,mono,free)

pulling current date queue

I have a view that lists employee (EmpID), request number (ReqNo), date request was opened (OpenDate) and the date it was moved to the next step in the process (AssignDate). What I am trying to do is get an average of the daily queue size. If EmpID 001 has 20 requests on 1/1/13, then has 24 on 1/2/13, 21 on 1/3/13 the average over 3 days should be 21.66, rounded up to 22. I have the following view:
CREATE VIEW EmpReqs
AS
SELECT [EmpID], [OpenDate], [AssignDate], [ReqID]
FROM [Metrics].[dbo].[Assignments]
WHERE OpenDate BETWEEN '01/01/2013' AND '12/31/2013' AND
[EmpID] IS NOT NULL AND
[ReqNo] NOT LIKE 'M%'
I then wrote a query to pull individual employee's queues per day:
/* First attempt to generate daily queue #s */
SELECT * FROM BLReqs
WHERE [BusLiaison] LIKE 'PN' AND
[OpenDate] <= '11/15/2013' AND
[AssignDate] > '11/15/2013'
Because no one has attempted to pull this information before, I have no way of verifying how accurate the above is. I tried using current dates, since I can see those in our database to compare but the code doesn't work, nothing is returned when I change the dates to 2014 and run my query.
What is the easiest way to verify that my code is correct, short of manually counting a day's queue?
Can anyone see any issues with the above scripts?
Is there a way to get the above code to work with current dates?
This question is really hard to answer because it is kind of broad and has little information at the same time. I'll try anyway:
Because no one has attempted to pull this information before, I have
no way of verifying how accurate the above is.
Try checking the result of this query for a few sampled dates.
I tried using current dates, since I can see those in our database to
compare but the code doesn't work, nothing is returned when I change
the dates to 2014 and run my query.
So clearly, the query is not working. You should probably find out why. Run the query for a date of which you know that it should return results but doesn't. Remove conditions one by one to see which one is incorrectly removing all rows. This should be enough to identify the bug.
Can anyone see any issues with the above scripts?
No, looks fine. A very simple query. That's why I said that we have too little information. There is some key piece of information missing that allows us to find the bug.
Is there a way to get the above code to work with current dates?
Stop staring at the code and hoping for a revelation. Debug it. Experiment.

Access 97: table entry to drop off form when a date/time reached

I like to think i'm not completely useless at creating MS Access databases, but i'm definitely a failure at the SQL code side.. So as a result i'm not sure whether this is a stupid question or not!
At work i'm trying to add a "news feed" type thing to a Form on the front screen of a database used to find useful information stored in various places. At the moment my workplace is using Office 2007, but Access is the 97 version!!!! As they're only recently realizing it can be used to solve a few of their problems... we're expecting to upgrade the whole of office and access to 2010 soon.
On this database (created using access 97..) there is a "refresh" type button which simply closes and re-opens the form and thus shows the latest info entered onto the "news feed", and this also shows the current time and date. What i'd like to happen is have specific entries drop off after a period of time (which probably wont happen unless refreshed), so for example an entry will have been added regarding some server ammendments being made within the workplace so "certain systems will not be working between 8am and 5pm GMT on 9/1/12" and preferably the person who created this entry could enter a date into the form 24 hours, or even a few days, later and when this date is reached the entry would disappear. I understand this is something that may be achieved using a query but i have no idea where to start.
If anyone can help give me an idea of how to do this it would be greatly appreciated.
I apologise if this is poorly worded or not completely clear, i can elaborate if questions are asked.
many thanks,
Kris
You can make a query like this to return only the entries that are less than 3 days old:
SELECT *
FROM MyTable
WHERE CreateTime > DateAdd("d", -3, Now())
In your form, you can define a timer interval and a timer event handler (see the "Event" tab on the properties window). You could use it to requery your list (Me!lstNews.Requery).
In the table you can define the column "CreateTime" as data type "Date/Time" and define its default value as =Now(). This way no text box is required to enter this data.
EDIT:
How to configure the timer in an Access Form (time is in milliseconds):