MS-Access Web DB "type mismatch" when setting date as string? - ms-access

This is specifically for MS-Access Web Databases (requires Sharepoint hosting) which has many limitations compared to their client counterparts, like no VBA, instead you get form macros and data macros to manage data.
I've run into a weird bug on one of my applications. I have a query used to check stock levels against a "minimum stock level" also saved in the table. The query is pretty intense and there are over 4,000 records now to check against. These querys normally take about 75s. So I have made a little label that gets updated every time the form is loaded showing the time and date the query was last run, and the duration in seconds it took. (so users can see how fresh the data is and decide if it needs to be run again)
Now, the weird thing is it works fine in my Access client, but when I sync my changes to the server and try it in a web browser I get a "type mismatch" error. A small table is used to store the start and end times whenever the query is run, that's how I get the timestamp data. These fields are in a "Date/Time" format, obviously. But it seems the problem here is changing the date format to a string format so it can be put in a label on the form. The Access client seems perfectly capable of doing this, while the web client stumbles and falls.
My problem is, how do I change data in a date/time format to a string format in a Web database? I can't figure out how to do this. The tools are so limited. I may have to end up answering my own question here but I'm posting this for others just in case.

To return a value from a data macro as string, you have to format the internal date/time format as a string. In Access an internal date/time value is a double number with the integer part as number of days since 1900, and the “decimal” time part is a fraction of 24 hours. Unfortunately if you simply wrap the date/time in the str$() function we had for 20+ years, then you get something JUST like if you type this into the debug window:
? cdbl(now())
41955.5478587963
The solution is to simply pull out each part. And “nice” is while in few cases a data macro will cast the data type, it does in this case and thus the STR$() command is not required.
The expression you thus can use is this:
Month([d]) & "/" & Day([d]) & " Time = " & Hour([d]) & ":" & Minute([d])
So say to pluck out the VERY LAST start time column from say a invoice table, since we don’t have a dmax(), then we simply sort the table in the order we want and pull out the first row.
Our data macro will thus look like:
Note how in above I simply typed in the SQL and SET the order on the date/time column. I want the MOST recent invoice start date and time. For those new to SQL, then I suggest you build a query in the query builder and specify a query in above lookup feature, since many are not "comfortable" typing in free hand SQL as I did above.
Now, in your browser side (UI) macro, you can use this code:
The above returns a formatted string that you can stuff into a text box, or as per above code change the caption of a label.

Unfortunately with silly problems like this, it becomes a path-of-least resistance thing.
Since my intended result was simply to get "a timedatestamp from a table to show up on a form (so users could see when a query was last run)", this became redesigning my form in Access to be a text field instead of a label. Text fields can be adjusted to accept "Time/Date" formats, so this is exactly what I did, it now pulls the timestamp data directly from the last record of the table and requires no extra formatting to appear in the web browser. I redesigned the text field to appear and function more like a label, and my desired function was achieved.
However, since my question specifically asks, "how do you change a time/date format into a string format in a Web db?", I will leave it here in case someone actually does solve it.

Related

MS Access Forms Date/Time Entry Trouble

So I have a couple tables that I want to filter on their Date/Time fields. Here's a snapshot of the form controls that I'm experimenting with.
This will report will probably end up run on a monthly basis, and so the filter "Between Forms!Sorting!OldestDate and Forms!Sorting!NewestDate" will normally work fine. However, sometimes it's useful to just run it on a single day, as in the picture, in which case I need the filter to work out to "Between #M/D/YYYY 0:00:00# and #M/D/YYYY 23:59:59#". Setting up the format on the controls to actually record the time as well as the date, however, has not been working out.
I thought, first, maybe the time wasn't displaying bc my text boxes were too small, so I tried adding the bottom text box. The display in the snapshot is what I desire, however, if I click out of the text box the date disappears and only the time is displayed. It also does not display any time at all until I go in and manually add a time.
Is there a way to force the display of both the short date and the long time? Or is there a way to, say, set the default TimeValue for NewestDate to 23:59:59?
Right now the only "solution" I might have is CVDate(CDbl(Forms!Sorting!NewestDate)+0.99999) appearing multiple times in my WHERE clause, which will make things harder to keep track of or catch mistakes in.
I have vba experience, though I've never tried to use it to mask/edit a form parameter as it is passed to a query. I am using Access through MS Office Professional Plus 2019.
First, in your query, specify as parameters:
[Forms]![Sorting]![OldestDate] DateTime,
[Forms]![Sorting]![NewestDate] DateTime;
Then you can filter on:
[YourDateField] >= [Forms]![Sorting]![OldestDate] And [YourDateField] < DateAdd("d", [Forms]![Sorting]![NewestDate])
If [YourDateField] is text, change that to DateTime. If that is not possible (it should be), use:
DateValue([YourDateField])

How can I correct this code to be able to get a difference in weights for each client?

I am working in MS Access. My end goal is a database report that shows the total amount lost so far for each client, based on the texts they send in daily. I've been able to extract the number from the message, but I need to be able to have a running report each day with how much they've lost since they first texted in. I tried this solution with no success: Access SQL Query: Find the most recent date entry for each employee for each training course
After that, I'v been working with the code from here: https://www.techonthenet.com/access/queries/weight.php. I've tried to write a query that brings up the Last Visit Date, but I keep getting type mismatches with this:
DMax("LogDate","NumberQ","TargetFK=" & TargetFK)
NumberQ is a query, where columns are TargetFK (Short text), FirstName (Short text), LastName (short text), LogDate (Date), Message (short text), and Weight (calculated field).
My question is, what am I doing wrong, and is there a better way to do it?
Since TargetFK is a text field, you need quotations around it:
DMax("LogDate","NumberQ","TargetFK='" & [TargetFK] & "'")

ms access data type issue when using a linked table to an excel document

I have a spreadsheet which is a report from an external supplier. The date columns are formatted completely different from how a little acccess database works, so i decided to record a macro in excel to alter the 3 columns to a dd/mm/yyyy format (im uk btw). This all works great and the column isshowing as above and is listed as a 'DATE' format also.
Now i use access to link to this excel sheet by means of a linked table, but i noticed that any queries that i wish to filter on those fields in the form of a where clause, it does not pull in the expected result IE it pulls all dates and not the ones between what i asked for in the queryalmost as if it is not recognizing them as dates see the where clause below
WHERE LatestGamma.ConfirmedPortingDate Between [Please Provide 1st Date (dd/mm/yyyy)] And [Please Provide 2nd Date (dd/mm/yyyy)];
I also tested it by using actual dates in the between cluase and it still appears to ignore it.
When i look at the properties of linked table for those columns, it shows that the fields are: dd/mm/yyyy;# but are showing as text??
When i look at the excel macro to see what code the macro used for changing the date format of the columns i see:
Columns("F:H").Select
Selection.NumberFormat = "dd/mm/yyyy;#"
I have another excel sheet that is used as a linked table and date columns are working fine and show as date/time and not Text. Its almost as if excel is putting some meta data into the columns that is making access interprate them as text (even though they are formatted as date)
Im stuck her some help/advise would be great
Its hard to tell without looking at the file, but it seems the main culprit is the excel macro which is not converting the string to date format correctly. You can try converting string into date format by using something like this in excel VBA
Sub convert()
Dim str1 As String
str1 = Range("f6").Value
startdate = CDate(str1)
End Sub
You probably will have to loop thru all the cells to convert the string (text) into date format.
Sorted it in the end. It was more or less what i was thinking IE the column although was showing as a Date type seemed to be storing it as some sort of text. Anyway the way round it was to use the text to columns button and force it to be a date in DMY format.
Once i had tried it i then recorded the actions as a macro and copied the resulting code to my main macro at the end, bingo ! Access now sees it as a date and not text.
Not sure if it is a bug or not but Excel can be a real pain sometimes especially with phone numbers, having to store them as a data type text to keep the leading zero and then immediately converting them to General to allow vlookups etc. I guess that is the same sort of thinkg going on above showing as a date yet actually having like a suedo text meta thing going on...

MS Access setting to ignore date conversion error

An Access DB imports a fixed width text file; one column is mostly dates.
When the date is not available, the file's creator actually uses the string "Null"
Access puts the row in the table with that field actually null.
But, when the files started coming with different field widths, I copied the DB, tweaked the starting/width values in the input spec, and imported. NOW, all the rows with null get logging in (table)_import_errors as an error converting text to date.
I have found no setting (not that I changed any) to explain it. One difference is that although both DBs are in Access 2000 format, the original is on a machine that still has Access 2000, while the new one is being handled by Access 2003.
Is that a behavior change in the Access version? Is pre-processing the file the only solution?
Thanks, David. That's what I would have done (except for the Excel part) if it had not fixed itself. I posted that, but apparently someone didn't like the public admission that Access has bugs.
The only thing that changed was that two other columns in the fixed width plain text input was wider. Yet Access "decided" to discard the whole row instead of just the date field for three consecutive attempts. The fourth time, it still reported it as an error but imported the rest of the row.
So, when Access misbehaves for no good reason, try again a time or two, then try explicitly coding the conversion from text.
Two possibilities:
Use a buffer field or buffer table that imports the date field into a text field. Then you can process that into the appropriate values in the final destination field.
Use a SQL import instead of DoCmd.TransferText. What you do in that case is use a connect string in the FROM clause so you can then process the date field in your SELECT:
SELECT Sheet1.FirstField, Replace(Sheet2.DateField, "NULL", Null) As DateField
FROM Sheet1 IN 'C:\Import\Spreadsheet.xls'[Excel 5.0;HDR=YES;IMEX=1;];
Convert that into an INSERT query and you're golden.

MS Access 2003: User was able to enter an invalid value; how?

I have a strange phenomen I can't explain nor reproduce and I'm hoping you have an idea how it was possible for the user to enter an invalid value.
I have an Access-MDB with a form containing an edit field that should only accept time values without any date-information.
The relevant properties of that edit field are as below:
bound to a Date/Time database value (since access know no time-only datatype)
Format: "Time, 24hrs"
Input Format: "99:99"
(I use the german version of Access, so the property names may be a little different, but you see the pattern)
Now I found out that a user was able to enter a date value into that field. I'm nearly 100% sure that it was an accident and no "clever hack" in the form of directly opening the table and editing the corresponding Date/Time field. Since the entry was made back in 2009 (nobody spotted the error), I have no chance to ask the user how it was done.
I found two wrong entries with the dates "01.06.2000" and "01.07.2000" and I guess the user wanted to enter the times "06:00" and "07:00".
I tried every input I can imagine (like "6.0", "6;0", "6,0", copy&paste) but I was unable to trick access and enter anything except digits and the colon.
Do you have any idea what is going on and how the user was able to enter these dates accidentially?
A Jet/ACE Date/Time value is never stored "without any date information". If you attempt to store only the time component, it will actually be stored with the same time on day zero (Dec 30, 1899).
We can only speculate how the incorrect data was added back in 2009. If you want the database engine to require your Date/Time values to be stored with day zero as the date component, you can add a table-level validation rule. From the table property sheet, try this for Validation Rule:
DateValue([your_date_field])=CDate("1899/12/30")
If you want to allow Null for your_date_field, try this version:
IsNull([your_date_field]) Or DateValue([your_date_field])=CDate("1899/12/30")
Several possibilities:
A developer (you?) did it by accident when looking at the raw table
The client Access software went momentarily crazy and corrupted the entry. This has happen to us (fortunately very rarely) where our Access tables will end up with non-ASCII characters in a string field.
A bug in Access runtime allowed the problem in the past, but has been corrected in a service pack.
Finally, if you put in 90 hours, does it overflow into 4 days and something? That might do it.