MS-Access VBA Date() works on one PC but not others - vba

I am designing my first database from the ground up and I have learned a lot in the last few weeks. One thing that has been eating at me though is that on my login page I have a simple unbound text box with its control source as =Date().
This works perfectly well on the computer I use most days, but any other computer in the facility I am working out of displays #NAME? instead of the date.
I have tried changing to =Now() and it works fine on all computers. Apparently only =Date() has issues. If the fields control source is changed back to =Date() I am informed that The function you entered can't be used in this expression.
I have checked the MS-Access versions and tried on a machines with and without access.
I really need Date() to work because it is used elsewhere in more vital areas of my code and I may not be able to use Now() in its place. Any ideas as to why this may not work on any PC besides my own?

Date() is controlled by the Visual Basic For Applications X.XX reference. Make sure the reference is installed and any competing references are removed.
To do this, open the VBA window and go to Tools --> References and ensure the proper one is checked.

I suspect the issue is user's region and language settings for the date. I noticed this when I changed computers and my files wouldn't save anymore due to the slashes in the date. You should try the format() function so that your dates always have the same format and can be used across machines independent of the user's settings.
Sub Foo()
Debug.Print Date
Debug.Print format(Date, "yyyy-mm-dd")
End Sub
Output
7/23/2015
2015-07-23

Related

MS Access crashes every time I use Immediate Window

Every time I use the MS Access Immediate Window within the VBA Editor, Access crashes if I type any procedure name followed by the spacebar.
For example, I have a procedure called "CreateCEUploadFile" which takes a string parameter for a year. So, I'd like to type "CreateCEUploadFile "2019". However, as soon as I hit the spacebar after the e of File, Access freezes for a second, then crashes.
I initially thought this was specific to a database I've created. This is a database which was initially created 7 years ago, and has been worked on steadily over that time. So, I tried:
Compiling, then compacting and repairing. No change.
Decompiling the file. No change.
Re-compiling the file. No change
Creating a new database file, importing all objects. No change.
Restarting the PC. No change.
Removing Office (Office 365 Pro), restarting the PC, re-installing Office, re-starting the PC. No change.
I've now experimented, and discovered that the same issue occurs in all database files on this PC (a laptop that I've been using without issues for around 2 years). It also occurs with any call to any function (whether my own or built in), as soon as I hit a character after the function name.
In the immediate window:
? now --> works
? date --> works
? format( --> crashes as soon as I hit the spacebar`
I have now just discovered that this same issue occurs in MS Excel's VBA Immediate Window too...
Okay, so I've just found the solution.
Using the old principle of "What's changed since I last got this to work" I discovered that it seems to be a problem with my installation of Dextop.
I wanted to have a way of having multiple virtual desktops, and the built-in virtual desktops system in Windows 10 wouldn't cut it. Every time I do anything in SSMS on one desktop, it's reflected in the other.
So, I had a look around and found good reviews for Dextop. It seemed to work well - I have two different databases open in Access (one on each Desktop), and two different instances of SSMS, one pointing at each server. However, it seems to be this that was causing the Immediate Window to crash the relevant application.
Exit Dextop - all works well.
Restart Dextop - symptom immediately re-appears.
So now to try to find another alternative to Dextop....

How to resolve conflicts at runtime?

Outlook has a neat conflict resolution system to solve the problem that sometimes your mails and the server mails are different. But sometimes it fails, and asks the user to solve it instead.
It would be troublesome to task a common user with that kind of job - Even more when a simple script or VSTO addin could do the job, without the need of user interaction.
But as far as I can tell by the official Microsoft documentation, there's no way to really do it, either by VBA, .NET or VBScript, as the framework lacks a "Mark this object as winner" method or anything similar.
There's some kind of way to do it anyway, even without officially supported methods and calls?
It would be better if I could implement it using a VB.NET VSTO Outlook Add-in, but any other way would be acceptable.
For reference, the conflict would be resolved in the following way: The newer one always wins, and the change made by the local user running the Add-in always wins if the first one fails.
The conflicts are being generated by a Addin that categorizes the emails based on a Access database - it runs just fine, but on synchronization with the server, some users get sync issues - others do not.
As a lowly summer intern, I have no way to change Exchange configuration or anything like that.
One of my solutions was this piece:
Public Sub TryToResolveConflict()
If MailItem.IsConflict Or MailItem.Conflicts.Count > 0 Then
If MailItem.AutoResolvedWinner = False Then
MailItem.Display()
MailItem.Close(OlInspectorClose.olSave)
End If
End If
End Sub
It just opens the mailitem and closes it again, saving it, if any conflicts are detected and it's not the AutoResolvedWinner.
As any other poor constructed solution based on ignorance, it does nothing at all.

VBA, Excel 2007, Inconsistant Runtime Error 13 on various user accounts

I am a coop student at a factory and I was developing a VBA program in excel for the operators to use for data collection. The program works perfectly on my bosses and my user account, but is throwing runtime errors when the operators try to use it; specifically, it is throwing a runtime error 13 (type mismatch) when I know the data is correctly entered.
Something as simple as Msgbox Cdec("26.1"), or Msgbox Cdbl("26.1") is throwing the mismatch error on the operators account, but it runs perfectly on my account (on the same computer). I initially thought it could have been to keyboard settings/language settings so I tried playing with the settings while entering data, but nothing changed. I checked to make sure that all of the same libraries were being referenced in the excel options.
I am completely at a loss and don't know where this problem is coming from. I would appreciate any suggestions.
I have figured out what the issue is. Because I reside in Ottawa, Canada which is bilingual, some of the operators had French regional settings. This caused them to use a comma instead of a period for a decimal point. By simply changing this property in the windows "regional settings", I was able to resolve the problem.

prevent button press across instances

I have created a database/app where a report is created when a particular button is clicked. just now, two people managed to hit the button at exactly the same time, which caused all sorts of not-good.
Is there a way to make a button invisible across instances once it's clicked by one person? Or some way to lock the database so nothing can be done until the person who clicked first is done?
I have a solution (basically, a global check variable that stops the report creation) but now I want to know if either of the other two options can be done.
It would really help to know more about your architecture here. What database? What language have you written your application in? Concurrent reading is usually an important and basic feature of most multi-user databases.
Seconding Daniel Cook's general notion, maybe explicating a bit: don't have the button run the report directly. Have it run a little subroutine that first checks a special purpose table where you represent report "runs" with a new record that has a start date-time and an end date-time. If there is a record sitting in the table with no (null) end-date, then the report must still be running, therefore, do NOT begin report, turn off button instead. Else, insert into that same table and then start running the report. Add to this a periodic, not-too-frequent callback on that button to perform the same check, and you've got something that comes close, but isn't "realtime", but should work in most architectures (not knowing anything about session management capabilities).
Here's what I did:
If DLookup("PayLock", "table", "pkID=1") Then 'it's locked - exit
MsgBox "Someone else has already started the pay process.", vbOKOnly
Exit Sub
Else
blah blah blah......
The "PayLock" field in the table holds the check variable. After "Else" comes the actual code to run when the button is clicked.
Just FYI, since they were asked:
it is split database
there are multiple users
yes, the report just reads data and exports it into an excel spreadsheet.
It looks like this is the only solution, which works, but seems inelegant. I keep discovering that the way I get around my lack of knowledge is the actual way to do it...

Having trouble with buttons in mde file: list box .rowsource

I have a form that has a list box and several buttons that run sqk statements for the list box.
sub on_onClick()
' error checking
me.listbox.rowsource = "SELECT tblMain.First, tblMain.last FROM tblMain ORDER BY tblMain.Last;"
so this kinda thing is what I use for the list box. it works fine for me in the .mdb, and after i have converted the database, split it, made the .mde I go into the mde and it works fine for me still. This is placed on a shared drive
HOWEVER, anyone else that tries to use this, none of the buttons are having an effect. Can't tell if they are not working, or the VBA is not returning any results for them. It works for me, but it is not working for them. So...this is for my local LAN team, I think they all have full control permissions??? The button clicks that get them to that form are working fine also.
please help!
A couple of things come to mind:
It is not clear from your question whether the mde file itself is on a shared drive or whether the back-end database is on the shared drive and each user has a copy of the mde file on their PC. If the users are using the same mde file on the network share then I would strongly suggest not taking this approach. (It probably isn't the cause of the issue, but it will get you into trouble down the road.) Give each user a copy of the mde (front end) and have the application point to the back-end database on the network share.
Make sure that the mde has the ability to relink the tables to the back-end mdb file.If you search SO you'll probably find examples of how to do this in code.
I suspect you may be running into an issue where the mde file is not able to find the back-end tables. In this case you won't receive an error message in your VBA code when you're setting the listbox's rowsource and it will appear as though nothing is happening.
Do they have Macro security disabled? By default, MAcro security is enabled in Access and none of your VBA code will run. To disable: open Access, Tools -> Macro -> Security and set to low.
I think it is generally best to use self-certified projects or a secure location rather than change security levels.
Some information on macro security:
Self Certify projects
General information on security principals for Office code and macro security
Trusted location defaults for Office 2003
Macro security in Office 2003