25 February 2010

Save changes to the following objects?


Aboout a year and a half ago we changed from using Access (full version) to Access 2007 runtime. It is cheaper (no need to install new Access on each user machine) and it is safer (I can control which objects do the users see) but it is more sensitive, so errors that I didn't have in Access 2003 full version appear all of a sudden in the runtime version. Access 2003 is simply more forgiving, especially if you tell it to load without openeing the database window. It is kind of horrible: you do something, test it, and after uploading the changes the users complains. The following error was in particular weird since I know it was tested in the Access 2007 runtime.
Another pain is to test it & develop on the same machine: each time you change version a prompt comes up with "installing...." which takes at least one minute of my precious time.
So today, after uploading the new version, my users complaint of the following error while hitting the "close" button of a form:
A form appears (see picture) asks you to save the forms. We are talking about users, not developers, and since when can you save changes in Access 2007 runtime?
I thought it was due to the multi user environment, but running a test version from my machine (for which I am obviously the only user) replicated the same problem.
The problem resulted from the following piece of code:

Private Sub Form_Current()

If get_se_listed(Me.pl_security) <> 0 Then
Me.pl_trade.DecimalPlaces = 0
Else
Me.pl_trade.DecimalPlaces = 2
End If

End Sub

Changing the decimal places (an option which is usually done in design time only, and not at run time) resulted in the definition of the form changing as well, and that prompts for the "save" problem. Goggling the issue had left me with only one (!) post about it in the forums, right here.
So first I eliminated the new changes, otherwise the users, non violent people in general, might kill me by the end of the day.
I decided to try and test for myself wheather prompting it not to save on close would actually work:
I added the code for the event "form_close" asking kindly to close without saving
docmd.close acForm, "sub_form_name", acSaveNo

It worked in Access 2003 but it didn't work in Access 2007 runtime version. At lease when I pressed the "no" it prompt me for saving (Again!) but didn't end up with a nasty "Execution for this application has stopped due to a run-time error" which I'm all to familiar with.
Adding the code to the form and not only to the sub_form had not helped. This time it did crash eventually.
In order to avoid the crash you can press 'Yes' and you can press 'No' but do not press 'Cancel'!
Also don't put the line
docmd.close acForm, "form_name", acSaveNo
or acSaveYes in the parent form name.

Trying to play with the Form_current event led to the conclusion that only for an empty function
Private Sub Form_Current()
End Sub
I wouldn't get the prompt. Please note that the form default view is in Datasheet. But I do need the design!

10 February 2010

This Round Thing

I work on Ms-Access, VBA and
I have a legacy code and inside it there is a self-written ROUND function, as follows:
Public Function Round(v1 As Variant, v2 As Byte) As Double

If Not IsNumeric(v1) Then
Round = 0
Else
Round = INT(v1 * 10 ^ v2 + 0.5) / 10 ^ v2
End If

End Function

I never knew why it was there but as the old saying goes: if it's not broken, don't fix it!

Today I found out that it is broken, i.e. it simply doesn't round to the required precision.
Thanks to Allen Browne, and more specifically to the MVPs of Access, I found out that
a. Access rounding may not existed when the database I am working on was created
b. Even if it did, it uses "banker's rounding" which may not be adequate
c. I shouldn't be using cast as INT, but instead, a different type of CAST.
That's because INT truncates the decimals, which is not my intention.
Like this:
Public Function Round(v1 As Variant, v2 As Byte) As Double

If Not IsNumeric(v1) Then
Round = 0
Else
Round = CLng(v1 * 10 ^ v2 + 0.5) / 10 ^ v2
End If

End Function
And that makes the rounding beautifully done!
The beauty of the function supplied by the MVP's of access is that it can deal with very large numbers, which is a bit problematic in my version.