Table already open through user interface and cannot be manipulated programmatically

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • robin27th
    New Member
    • Aug 2010
    • 11

    Table already open through user interface and cannot be manipulated programmatically

    Hi,
    I have a form which uses table 'Patients' as Record Source and type Dynaset. I have a 8 digit text id field which starts with 'PT' (PT000001). I wrote the following code to get the next maximum ID on a new record.

    Code:
    Private Sub Form_Current()
        Dim db As DAO.Database
        Dim rs As DAO.Recordset
        Dim sql As String
        Dim strNextID As String
        
        If Me.NewRecord Then
            sql = "SELECT TOP 1 CInt(Mid([Id],3,8)) AS MaxID " & _
                        "FROM Patients " & _
                        "ORDER BY CInt(Mid([Id],3,8)) DESC ;"
            Set db = CurrentDb
            Set rs = db.OpenRecordset(sql, dbOpenDynaset, dbOptimistic)
            strNextID = "PT" & String(6 - Len(rs!maxid), "0") & rs!maxid + 1
            Me.Id = strNextID
            Me.Id.SetFocus
        End If
     
    End Sub
    But when I try to add new record, I get the following error.

    "The table Patients is already opened exclusively by another user, or it is already open through the user interface and cannot be manipulated programmaticall y. (Error 3008)"

    Could anyone suggest a work around on this issue?

    Thanks,
    Robin
  • patjones
    Recognized Expert Contributor
    • Jun 2007
    • 931

    #2
    What line is the error occurring on? You can find this out by setting a break point on the first executable line of the sub then stepping through it until the error is raised.

    Also, what exactly is the format of your ID values? Maybe you could give us a short list of examples. Thanks.

    Pat

    Comment

    • robin27th
      New Member
      • Aug 2010
      • 11

      #3
      Pat,
      The error occurs at line 12.
      Code:
              Set rs = db.OpenRecordset(sql, dbOpenDynaset, dbOptimistic)
      Basically the ID values have PT prefix and rest six digit increases i.e. PT000001,PT0000 02,PT000003.

      I used a numeric ID and used dmax which works fine.
      Code:
              lngNextID = DMax("Id", "Patients") + 1
      I guess its an issue with opening a recordset on a bound table. I tried a query instead of the table but same error.

      Robin
      Last edited by robin27th; Sep 13 '10, 06:42 PM.

      Comment

      • patjones
        Recognized Expert Contributor
        • Jun 2007
        • 931

        #4
        I was going to suggest using DMax in some form. DMax is the usual way of handling this problem. This is not to say that SELECT TOP can't work...but as you can see it has the disadvantage of needing to use a recordset.

        So your situation is resolved then?

        Pat

        Comment

        • robin27th
          New Member
          • Aug 2010
          • 11

          #5
          ...deleted.
          Last edited by robin27th; Sep 13 '10, 06:42 PM. Reason: duplicate reply.

          Comment

          • robin27th
            New Member
            • Aug 2010
            • 11

            #6
            ...deleted.
            Last edited by robin27th; Sep 13 '10, 06:41 PM. Reason: duplicate reply.

            Comment

            • robin27th
              New Member
              • Aug 2010
              • 11

              #7
              Sorry, I am on a slow connection, so same reply posted several times erroneously.

              Dmax would not work on a text field and I might encounter similar situation in future. So I would rather want to know how to work with a recordset, if possible. Any suggestion?

              Robin

              Comment

              • patjones
                Recognized Expert Contributor
                • Jun 2007
                • 931

                #8
                Can you try changing line 12 to the following and let me know if you still get the error (I suspect the error will continue but want to rule out a simple possibility first):

                Code:
                Set rs = db.OpenRecordset(sql, dbOpenTable, dbOptimistic)

                Pat

                Comment

                • patjones
                  Recognized Expert Contributor
                  • Jun 2007
                  • 931

                  #9
                  Also, the option dbOptimistic is meant to be used in the fourth argument of OpenRecordset.. .so you should really have:

                  Code:
                  Set rs = db.OpenRecordset(sql, dbOpenTable, , dbOptimistic)

                  Since you are opening the recordset to retrieve data only you could even try:

                  Code:
                  Set rs = db.OpenRecordset(sql, dbOpenTable, dbReadOnly, dbOptimistic)

                  Pat

                  Comment

                  • patjones
                    Recognized Expert Contributor
                    • Jun 2007
                    • 931

                    #10
                    I apologize for all the replies. I duplicated your situation in my test database and found that this works nicely:

                    Code:
                    Dim rs As DAO.Recordset
                    Dim strSQL As String
                    Dim lngMaxID As Long
                    
                    strSQL = "SELECT MAX(ID) AS MaxID FROM (SELECT CInt(Mid(productID, 3, 8)) AS ID FROM tblTest)"
                    Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
                    
                    lngMaxID = rs("MaxID")
                    lngMaxID = lngMaxID + 1
                    
                    Set rs = Nothing

                    I eliminated the dbOptimistic option. I also used slightly different SQL that utilizes a sub-query, but your SQL should work fine also. Additionally, it is good practice to clear the recordset object by using the Nothing keyword.

                    Pat

                    Comment

                    • robin27th
                      New Member
                      • Aug 2010
                      • 11

                      #11
                      Both of your suggestion gave the 'invalid argument' error. However the following statement worked.

                      Code:
                              Set rs = db.OpenRecordset(sql, dbOpenDynaset, dbReadOnly)

                      Thanks for the clue Pat.

                      Robin

                      Comment

                      • patjones
                        Recognized Expert Contributor
                        • Jun 2007
                        • 931

                        #12
                        No problem, glad it worked out.

                        Comment

                        Working...