Full Version: Is It Possible To Set A Limit On How Many Times A Form Can Be Opened
UtterAccess Discussion Forums > Microsoft® Access > Access Forms
gloworm
Like the title says:

Is it possible to set a limit on how many times a form can be opened?

If someone opens it one time, form saves fine.
Open it a second time and it will not save.

Is this a possibility?
theDBguy
Hi,

You may be mixing up the terms here a bit. When you open a form in "normal" view and then close it, the form is not being saved at all. I think what you are referring to is the record being displayed on the form that is being saved to the table.

Now, remember, a form can display different records from the table. So, how can you determine which record was saved the last time the form was opened so that you can prevent the user from saving it again? Also, what is the purpose of stopping the user from saving the record a second time? If this is to circumvent your problem of the form generating a "blank" record, then I think you should try to fix the cause of that problem instead of trying to create a workaround.

Just my 2 cents... 2cents.gif
jleach
Hi,

You can use a static variable (or a module level variable) to track the number of opens...

CODE
Option Compare Database
Option Explicit

Private m_OpenedCount As Integer

Private Sub Form_Open(Cancel As Integer)
  If m_OpenedCount = 2 Then
    Cancel = True
  Else
    m_OpenedCount = m_OpenedCount + 1
  End If
End Sub


This would be reset each time the app is closed and reopened. You'd have to store that in a table to track it across more than one session.

That said, this seems an odd requirement... usually if there's some requirement (e.g. - don't do this if some criteria is true), we'll handle it on an actual data/task calculation rather than counting the instances that a form is opened. The count of form instances is prone to... misperformance, I'll label it, for lack of a better term.

What is the reasoning for not allowing the form to open a second time (or not allowing it to update a second time, in which case you'd move that logic to the BeforeUpdate or BeforeInsert event instead of the Open event)

hth
gloworm
QUOTE (jleach @ May 30 2012, 03:14 PM) *
Hi,

You can use a static variable (or a module level variable) to track the number of opens...

CODE
Option Compare Database
Option Explicit

Private m_OpenedCount As Integer

Private Sub Form_Open(Cancel As Integer)
  If m_OpenedCount = 2 Then
    Cancel = True
  Else
    m_OpenedCount = m_OpenedCount + 1
  End If
End Sub


This would be reset each time the app is closed and reopened. You'd have to store that in a table to track it across more than one session.

That said, this seems an odd requirement... usually if there's some requirement (e.g. - don't do this if some criteria is true), we'll handle it on an actual data/task calculation rather than counting the instances that a form is opened. The count of form instances is prone to... misperformance, I'll label it, for lack of a better term.

What is the reasoning for not allowing the form to open a second time (or not allowing it to update a second time, in which case you'd move that logic to the BeforeUpdate or BeforeInsert event instead of the Open event)

hth



I have a form that is not allowing it to be saved for some reason that I cant figure out.
It saves sometimes, and then it will not at other times.
I was grasping at straws trying to make heads or tails out of what is happening.



jleach
What sort of errors are you getting when the record is failing to save the second time?
theDBguy
Hi Jack,

QUOTE (jleach @ May 30 2012, 03:25 PM) *
What sort of errors are you getting when the record is failing to save the second time?

I think it is related to the issue in this earlier thread.

Just my 2 cents... 2cents.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.