Insert null value to date field in access using vba - date

Am trying to insert date from a table to another table using vba in access, But one of the date values from the table is blank. So while inserting the date to new table it shows Invalid use of null exception Run-time error 94.

If writing DAO code to clear a Date field, I found that I had to use "Empty". Null and "" won't work. So for field dtmDelivery (type Date), I had to use the following. strDelivery is just a string with the date in it.
Set rst = dbs.OpenRecordset("tblSomething", dbOpenDynaset)
If (strDelivery = "") Then
rst!dtmDelivery = Empty
Else
rst!dtmDelivery = strDelivery
End If
rst.Update

you can use the NZ() function to define a value that should be used instead of NULL. Internally a date is a float value where the number represents the days between 01.01.1900 and the date that should be stored. The part behind the decimal point represents hours/minutes (.25 for example is 6 AM). So you can use the NZ() funtion to replace NULL bei 0 (what would be interpreted as 01.01.1900).
Another solution would be to configure the target table in a way that it allows NULL values. You can do this easily in the design view of the table.

I think i figure it out , declare the variable to string. Then check whether value from the table is empty, then assign null to it.Other wise assign the value from the table.
for example
Dim newdate As String
Check whether the fetched values is null or not
If IsNull(rst![value]) Then
newdate = "null"
Else
newdate = rst![value]
End If

I encountered this problem and solved it this way.
SQL = "update mytable set mydatefield = '" & txtdate & "'"
On the txtdate on your form, put a date format to ensure date is either a real date or just blank.

Related

Why is my UPDATE query with jsonb popping an error?

I'm trying to update a row in my PostgreSQL database and it's saying it's not finding the x column. the thing is the column pg is trying to find is actually a parameter for the new value in the jsonb_set function, so I'm at my wits end.
It's hard to explain, so I included the query and the error it throws.
Tried adding quotes, double-quotes, brackets, inside and out... didn't work.
UPDATE public.sometable
SET somecolumn = jsonb_set(somecolumn, '{firstKey, secondKey}', someInputString), update_date=NOW(), update_username="someone#somewhere.com"
WHERE id=1
RETURNING *
I'm expecting the value of the row I'm updating to be returned, instead I get:
ERROR: column "someInputString" does not exist
LINE 1: ...n = jsonb_set(somecolumn , '{firstKey, secondKey}', someInputString)...
You have to deliver a valid json value as the third argument of the function:
UPDATE public.sometable
SET
somecolumn = jsonb_set(somecolumn, '{firstKey, secondKey}', '"someInputString"'),
update_date = now(),
update_username = 'someone#somewhere.com'
WHERE id = 1
RETURNING *
Note, I guess update_username is a text, so you should use single quotes for a simple text.
Db<>fiddle.

Use subquery variable inside to_date

I need to update my date with a subquery like this, i aint got a day number .
UPDATE grh
SET date = to_date('subquery.year-subquery.week_number','YYYYWW')
FROM (SELECT year,week_number
FROM grh) AS subquery
how could i use the subquery.year variable inside the to_date fonction ?
TRY 1 : So I've tried this :
UPDATE grh SET date = to_date(week_number, 'WW');
And i have this kind of error :
function to_date(integer,unknown) doesnt exists.
but if you look at this doc : https://www.techonthenet.com/postgresql/functions/to_date.php
They say that WW exists, for specifiing a week number. My 'date' column is a date format .
TRY 2 : this is working :
UPDATE grh SET date = to_date('42018', 'WWYYYY');
As soon as I try to use a variable like that, it doesnt work :
UPDATE grh SET date = to_date(string_agg(week_number,2018), 'WWYYYY');
In Postgres( and most other SQL flavors ), the concatenation operator is ||
UPDATE grh SET date = to_date(week_number||'2018', 'WWYYYY');
The linked documentation on to_date function :
to_date( string1, format_mask )
and the error is
function to_date(integer,unknown) doesnt exists.
so i think that you have a mismatch on the type of your first parameter
Edit1:
for the statement
UPDATE grh SET date = to_date(string_agg(week_number,2018), 'WWYYYY');
if you chekc the documentation of the function string_agg you will see the expected type of input params are string and at least the second one is a integer

An access query comparing the time part in a date field with values input in a form

I have an access query with predicates (conditions) on a date/time column called start_time. The condition is on two form fields defined with "Format" as "short time". The problem is that this query does not give correct results.
SELECT event_cust.*
FROM event_cust
WHERE Format([start_time],"hh:mm")
BETWEEN [Forms]![CustEventRptForm]![FromHour]
AND [Forms]![CustEventRptForm]![ToHour]
Also tried it using Format([start_time], "short time") BETWEEN ... - did not work either.
Do we need anything in addition to above code to get the correct results?
I have tested with literal values as shown below and I get correct results with that.
SELECT event_cust.*
FROM event_cust
WHERE Format([start_time],"hh:mm") BETWEEN '10:00' AND '13:00'
My guess is it's not interpreting the value from the form correctly.
Try adding quotes before and after the values:
SELECT event_cust.*
FROM event_cust
WHERE Format([start_time],"hh:mm") Between "" & [Forms]![CustEventRptForm]![FromHour] & ""
And "" & [Forms]![CustEventRptForm]![ToHour] & ""
Don't use string comparisons for date/times.
SELECT event_cust.*
FROM event_cust
WHERE TimeValue([start_time]) >= CDate([Forms]![CustEventRptForm]![FromHour])
AND TimeValue([start_time]) <= CDate([Forms]![CustEventRptForm]![ToHour])

Zend: Problems defining select option with NULL value for submission to INT MySQL field

I have a database field defined as INT with NULL Allowed. The data submitted to this field is produced by a select form element whos values are defined using addMultiOption and addMultiOptions in combination e.g.
$selectData = $dataModel->getPairs();
$updateForm->getElement('type_id')->addMultiOption(NULL, '- Not Selected -');
$updateForm->getElement('type_id')->addMultiOptions($selectData);
The problem is that the NULL I'm trying to assign to the "- Not Selected -" option is being converted to an empty string at some point and MySQL is throwing an error because an empty string is an incorrect integer value.
How can I modify this such that the NULL arrives at the MySQL insert as a NULL?
Regards,
Nick
EDIT: See Daniel Gadawski's answer below. Below is the final result.
$selectData = $dataModel->getPairs();
$updateForm->getElement('type_id')->addFilter(new Zend_Filter_Null());
$updateForm->getElement('type_id')->addMultiOption('', '- Not Selected -');
$updateForm->getElement('type_id')->addMultiOptions($selectData);
This allows the value NULL to be written to a MySQL Integer field with NULL Allowed.
Add Zend_Filter_Null to this field:
$updateForm->getElement('type_id')->addFilter(new Zend_Filter_Null());
Then just retrieve data from this form using $updateForm->getValues() and the empty strings will be replaced by null values.

XPath - is a date null

Using XPath, how do I figure out if a date or datetime field is null or blank?
I am using the concat method as a stand-in for the XPath if statement
concat(
substring(../preceding-sibling::my:PerDiem[1]/my:perDiemEnd, 1, ../preceding-sibling::my:PerDiem[1]/my:perDiemEnd = "" * string-length(../preceding-sibling::my:PerDiem[1]/my:perDiemEnd)),
substring(/my:ExpenseForm/my:ExpenseHeader/my:departureDateTime, 1, not(../preceding-sibling::my:PerDiem[1]/my:perDiemEnd = "") * string-length(/my:ExpenseForm/my:ExpenseHeader/my:departureDateTime))
)
More info:
In Infopath 2010, a repeating table has two date/time fields called perDiemStart and perDiemEnd. In the repeating table, the next perDiemStart is the previous perDiemEnd. This is easily done if the default value of perDiemStart is ../preceding-sibling::my:PerDiem[1]/my:perDiemEnd
But for the first perDiemStart (since a previous perDiemEnd does not exist, I suppose it would be null/blank). I want that first (blank) value to be a different: value of departureDateTime node
Node locations:
/my:ExpenseForm/my:ExpenseHeader/my:departureDateTime
/my:ExpenseForm/my:PerDiemDetails/my:PerDiems/my:PerDiem/my:perDiemStart
/my:ExpenseForm/my:PerDiemDetails/my:PerDiems/my:PerDiem/my:perDiemEnd
To check if it is filled:
perDiemStart[text()]
To check if it is empty/null:
perDiemStart[not(text())]
Does this help? http://blogs.msdn.com/b/syamp/archive/2011/03/13/fim-2010-xpath-how-to-check-null-value-on-a-datetime-attribute.aspx
Basically they detect null dates by getting the set of dates after an old date (e.g. 1900-01-01) and then using 'not' to see which nodes would be excluded.