PDA

View Full Version : Too WTFy?



Oorang
04-12-2009, 02:16 PM
So uhm I just caught myself doing this little gem...
Should I smack myself now?

Private Sub btnExitNow_Click()
On Error Resume Next
Access.Quit (Not mdlMain.BypassMode) * -acQuitSaveNone '<-Will prompt to save if in bypass mode.
End Sub

CreganTur
04-13-2009, 08:52 AM
Ummm... I'm not really sure what the deal with this code is... except for the fact that googling part of it seems to bring up a lot of hacker and malicious coding websites.

Explain?

Oorang
04-13-2009, 09:53 AM
lol well I guess that answers that:) But what in the world did you google anyway? Anyhow, there is a property in mdlMain that indicates whether or not the user entered in bypass mode ("holding shift"). If they are in bypass mode and click the button the database will quit and ask them if they want to save. If they are not in bypass mode then it will quit with no prompt.

CreganTur
04-13-2009, 11:03 AM
Why would they need to be asked to save when in bypass mode?

Oorang
04-13-2009, 11:12 AM
Well it was more for me. In nearly all circumstances they will not be in bypass mode, so I would want them to quit with no save prompt. The db is pretty locked down, so odds are if you are in bypass mode you are an administrator. And if you just made a whole bunch of changes and forgot to save, should you click the button... it would be nice not to loose your work. It was just a Q&D way to determine if it should prompt or not.

I could have just:

Private Sub btnExitNow_Click()
On Error Resume Next
If mdlMain.BypassMode Then
Access.Quit
Else
Access.Quit acQuitSaveNone
end if
End Sub
But in my mad rush for power and glory I was doing as few lines as possible... Hence the first version. Then I reread what I did, had a chuckle and posted it up:)

CreganTur
04-13-2009, 11:30 AM
Well it's actually a pretty elegant solution... and informative. I didn't know that Quit could take parameters.

Also, what's the purpose of the asterisk?

Oorang
04-13-2009, 08:06 PM
It's multiplication:) The value of acQuitSaveNone is 2. And the value of acQuitPrompt is 0.On a math operation "True" will implicitly convert to -1, "False" will convert to 0. So if you multiply the result of BypassMode (which is boolean) by negative 2 (or negative acQuitSaveNone) and Bypass mode is false the "not" flips it to "true" and the math expression will be -1*-2=2 which is acQuitSaveNone. BypassMode is True it's flipped to false (0) and multiplied (yielding 0) which is acQuitSaveNone.

kbsudhir
08-11-2009, 04:20 AM
Guys what is "mdlMain" in the code..??

Thanks
Sudhir

What is "mdlMain" is code....?

Is it the name of the database..??

Regards
Sudhir

Oorang
08-11-2009, 07:22 AM
mdlMain is the name of a module:) In this particular DB we were using mdl and cls to prefix module names (a practice which I personally dislike). The "Main" module contains methods that run on start-up; or pertain to the database as a whole, but need to be in standard module. Our Main module contained a Boolean BypassMode property. Which tells you whether you are in bypass mode or not.

Why would you do such a thing? If your database is locked all the way down (Userlevel security, split database, mde frontend, bypass mode disabled, etc.) you never ever want design changes saved by the user. In the event they do some make a change that could become permanent (which should not be possible) you don't want them to have a chance to save it.

Meanwhile if the user is not in bypass mode, that user is probably you, the developer, in the mdb master copy. And if for some reason you close out of the form, you probably want a chance to save.

You can add a BypassMode property like this:
1.) Have an access-macro named "AutoExec". Naming it this will cause the macro whenever the database is opened normally. It will not run if the db was opened in BypassMode (ie: holding down shift when you open).
2.) Have a standard module (the procedure names are irrelevant) with code that looks something like this:
Option Explicit
'Do not use "Option Private Module" as this module needs to be available to
'the AutoExec Access-Macro.

Private m_blnNormalMode As Boolean

Public Function Startup()
'Note: This is a function and not a Sub in order to be callable by "RunCode"
' in the AutoExec macro.
m_blnNormalMode = True
End Function

Public Property Get BypassMode() As Boolean
BypassMode = Not m_blnNormalMode
End Property
3.) Then in the AutoExec macro use "RunCode" to call "Startup"

If the database is started normally, then then startup code will run, the NormalMode variable will be set to true, and because BypassMode returns the logical opposite of NormalMode it will read "False" as in "I am not in bypass mode".

If the user enters in normal mode, the code never runs, the NormalMode variable stays false (the default value) and whenever the BypassMode property is called, the logical opposite (True) is returned.

kbsudhir
08-11-2009, 09:33 AM
I have given the option that one of my forms should be oprn by default & disabled the access tables etc during startup.

Now can I ensure that I open a form & also call the code to check whether the database is bypassed.

Below is the plan:

1. IF database is bypassed it will generate aquery to check whether the user is an administrator, if yes "no issues" :-)

If no, it capture the userid & date & time of such a bypass & update a table & exit from the access.

Regards
Sudhir

I have added a module & have all the codes in that as suggetsed.
When I try save teh macro "Autoexec", it is not getting saved, instead I am getting an error "Record Is deleted".

Now if I use, "..." at the "Function name".

I will get "functions" below it I have

1. Builtin functions
2. ABC

Now if i click on abc then "The Microsoft Jet database engine could not find the object "module 1". Make sure the object exists and that you splee its name and the path correctly."

If I click ok. then the module1 will be displayed & also the startup function is also dispalyed.

I double ckick it & then press ok.

Now startup() is displayed but when I save again I get the same error as I mentioned in the starting. Please guide.

I do not know why I am getting this error in my office.

I tested it in my home in a test database, it worked fine. Hence I need guidance on how to get over the error which I am getting as mentioned in the previous post.

I want to know if anybody bypassed the database & made changes in the data, can we stop that in any manner.

The problem here is I can use Login ID & Password as few macros are beind used to upload the data into the database. Which menas I have to go fix each & every macro which is communicating with this database.

Hence, lets say Bypassmode is null as the startup was bypassed. Can we trigger any code, immediately when any thing is done in the database. Even if the user clicks the on a table, form, queries etc to quit the access without giving any chance to the user to make kind of changes...????

Any guidance is valuable.

Regards
Sudhir

Or if we could check the bypassmode during the exit of the database & trigger a macro to undo all the changes made...!!!!!!!!!!!!!!

I do not know whether its possible or not, just a thought came up.