How to stop the form from opening from a second user....

Hello, stars,

Am trying to build an application that will be shared with about 4 or 5 users.  Want to allow them in this app to do other procedures.  I want to stop the option of successive users from using an Add Record form.  Here is what basically happens.  When they press a <Record Maintenance> button on the opening page, they are presented with all the current records in the database.  They can modify any field except the unique customized record number.  When they wish to add a new record to the table, they press a command button. A new form appears for input of this information.  While this form is open, I wish that no other user have the permission to open the form as the same customized number will be assigned.  That presents a conflict on closing one of the forms or loss of a number if the first user should cancel out the input.  How can I disable the command button if the Add button has a form open in another instance?

Thank you for stopping by and schooling me.

Who is Participating?
mgrattanConnect With a Mentor Commented:
You can create a table that stores only the NEXT unique record number that has not yet been assigned.  Each time a user clicks a button to create a new record, the table that contains your number can be updated to have that number incremented.  For example:

Private Sub btnNewRec_Click()
Dim db as DAO.Database, rs as DAO.Recordset
Set db = Currentdb
Set rs = db.OpenRecordset("tblUniqueRecNo", dbOpenDynaset)

rs!RecNo = rs!RecNo + 1
Set rs = Nothing
Set db = Nothing

End Sub

Using this method, you won't have to worry about restricting users from having the form open simultaneously.
Function IsLoaded(ByVal strFormName As String) As Boolean
 ' Returns True if the specified form is open in Form view or Datasheet view.
    Const conObjStateClosed = 0
    Const conDesignView = 0
    If SysCmd(acSysCmdGetObjectState, acForm, strFormName) <> conObjStateClosed Then
        If Forms(strFormName).CurrentView <> conDesignView Then
            IsLoaded = True
            msgbox"you cannot open this form at this time", vbokonly
        exit function
         'open your form
        End If
    End If
End Function
Sorry, put that code in the on click event of the command button.
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

You can use a table and add a record when the form is opened and the table is empty.  You could notify the user to try again later and Cancel the button click if the table has a record.  And then you would empty the table when exiting the form.

The IsLoaded function will only work for instances within the same session of the database, not for other users.
mgratten's solution is the one I use. I have a system where 5 of the users are keying new records all day long so allowing only one add at a time is not an option. I store the last used number in a table and when Add Record is clicked it grabs the number, adds 1 and writes it back so the next user has access to it. you would still have the issue of lost numbers though if someone cancelled. That is not an issue for my system fortunately but if it is for you you'll have to come up with a way to handle it. Possibly if a user cancelled you could write that number to an unused numbers table. Then when you were getting your next number you would check for an entry in that table first. If one existed you could use the number then delete it and if none existed get your number from the other table.
FlSteveAuthor Commented:
OK, now that you have found my re-incarnation, you provided the code that I needed to get started, too.  Thanks, Mike.  You are exactly right about the IsLoaded function per instance.  Tried that and tried the disable of the command button.  But, in any successive instance, it did not apply.  This creating a table or inserting a record into a table makes good sense and I needed just a push on how to do this.  Thanks so much.  This is my first attempt with multiple users in which I need to lock a feature at one time.  

Also, the idea posed by another good fellow, of placing unused numbers in a suspense file is an excellent idea.  Have considered that concept and may just apply if the customer is willing to go with it.  Thank you for that suggestion.  

Thank all of you for stopping.   Some of us learned something and all had experiences to share.  

FlSteve  - Have a super day.

This is the code that I use to get the next number. May not be the best way but it works great. It lives in the OnClick event of the Add Record button

        Set db = CurrentDb()
        Set rst = db.OpenRecordset("tblMaxInsulationApplNo", dbOpenDynaset)
    'start the new record
        DoCmd.GoToRecord , , acNewRec
        cmdDeletePermit.Enabled = False
        cmdChangePermit.Enabled = False
        cmdPrintPermit.Enabled = False
        cmdMakePayment.Enabled = False

applno is the application number. I have to force a format on it but you would just need to set applno.value = rst!field.value

        ApplNo.Value = Format(Right(rst!MaxInsulationApplNo, 6) + 1, "000000")
write the used number back to the maxnumber table
       Do While Not rst.EOF
            rst![MaxInsulationApplNo] = ApplNo
        Set rst = Nothing
        Set db = Nothing
Glad to help Steve!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.