Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 31
  1. #16
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Unless I'm missing something those seven queries could and should be one simple update statement:

    Code:
     
        strSql = "UPDATE " & TableFound & " SET StatusCode = 'COM' "
        strSql = strSql & " , StatusTime = '" & Format(Time, "hhmm") & "' "
        strSql = strSql & " , CurrentStatusComment = 'T/T COMPLETED' "
        strSql = strSql & " , TimeUpdated = '" & CurrentTime() & "' "
        strSql = strSql & " , DateUpdated = '" & Date & "' "
        strSql = strSql & " , UserUpdatedKey = '" & sUserKey & "' "
        strSql = strSql & " , StatusDate = '" & Format(Date, "yyyy-mm-dd") & "' WHERE " & StatusCriteria
    As the table and where clause are the same for each statement.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  2. #17
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by Edgar View Post
    Are you using MS SQL? Can you run this?
    Code:
    EXEC sp_lock mytable
    I tried this and got an error so I looked up the syntax for sp_lock and it seems that it needs a session ID instead of a tablename. I couldn't seem to find anything on how to get the session ID of the table. It also wouldn't just check the single record I am attempting to update, it seems like it would check the entire table which would just throw false positives when someone's editing a different record.


    Quote Originally Posted by Minty View Post
    Unless I'm missing something those seven queries could and should be one simple update statement:

    Code:
        strSql = "UPDATE " & TableFound & " SET StatusCode = 'COM' "
        strSql = strSql & " , StatusTime = '" & Format(Time, "hhmm") & "' "
        strSql = strSql & " , CurrentStatusComment = 'T/T COMPLETED' "
        strSql = strSql & " , TimeUpdated = '" & CurrentTime() & "' "
        strSql = strSql & " , DateUpdated = '" & Date & "' "
        strSql = strSql & " , UserUpdatedKey = '" & sUserKey & "' "
        strSql = strSql & " , StatusDate = '" & Format(Date, "yyyy-mm-dd") & "' WHERE " & StatusCriteria
    As the table and where clause are the same for each statement.
    They could be haha. I was thinking the same thing recently after thinking about it but my priority is getting rid of this bug first and then cleanup.

  3. #18
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    I suspect your problem will go away if you run it as a pass-through. SQL server is really good at handling locks and multiple access.
    You may also find that changing your login's permissions might make things better as well.

    I haven't found any way of finding if a specific record is locked either.
    The locking could be down to poor design in the source database.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  4. #19
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by Minty View Post
    I suspect your problem will go away if you run it as a pass-through. SQL server is really good at handling locks and multiple access.
    You may also find that changing your login's permissions might make things better as well.

    I haven't found any way of finding if a specific record is locked either.
    The locking could be down to poor design in the source database.
    I actually found a method that works a lot quicker to check if its locked.

    Code:
    Public Function WorkticketUpdate()        On Error GoTo Error_Connection
            Call SetVars
            
            Dim WTqdf As DAO.QueryDef, WTrst As DAO.Recordset, WTdb As DAO.Database, UpdateSQL As String, Number
            
            'Checks to see if the record is locked
            Set WTdb = CurrentDb
            Set WTqdf = WTdb.CreateQueryDef("", "SELECT * FROM " & Replace(TableFound, "_", ".", , 1) & " WHERE " & StatusCriteria & ";") 'Replace is needed because when access saves linked tables it replaces "." with "_"
            WTqdf.Connect = "ODBC;Driver={SQL Server Native Client 11.0};Server=server;Database=database;Network=DBNMPNTW;UID=UID;PWD=PWD;"
            Set WTrst = WTqdf.OpenRecordset
            Set WTqdf.ReturnsRecords = False
            WTrst.Edit
            
            'Shortens the SQL statement
            UpdateSQL = "UPDATE " & Replace(TableFound, "_", ".", , 1) & " SET StatusCode = 'COM', " & _
                                                                                "StatusTime = '" & Format(Time, "hhmm") & "', " & _
                                                                                "CurrentStatusComment = 'T/T COMPLETED', " & _
                                                                                "TimeUpdated = '" & CurrentTime() & "', " & _
                                                                                "DateUpdated = '" & Date & "', " & _
                                                                                "UserUpdatedKey = '" & sUserKey & "', " & _
                                                                                "StatusDate = '" & Format(Date, "yyyy-mm-dd") & "' " & _
                                                                                "WHERE " & StatusCriteria
            'Sets the sql string for the actual update and executes it
            WTqdf.SQL = UpdateSQL
            WTqdf.Execute
    
    Exit Function
    
    
    Error_Connection:
        Dim dbeError As Error
        For Each dbeError In DBEngine.Errors
            Debug.Print "(" & dbeError.Number & "): " & dbeError.Description
        Next
        Call Msgbox("Cannot mark step complete. Someone may be editing the DHR" & vbCrLf & "Error: " & Error & " " & Err.Number)
    End Function
    If the record is locked then WTrst.edit will throw an error reading "(3027): Cannot update. Database or object is read-only."

    Originally I was using WTrst.findfirst when I tried this method because that is what was recommended in the answers I found on google for locked records but that was slow so then I searched for faster ways to do the findfirst function and queries were recommended. Now it works instantaneously.

    I do have that huge ugly block of empty space by the UpdateSQL variable. Any tips on how I could clean that up? (Also tips on how to clean the rest up would be cool too!)

    edit: or atleast it seems to work. I need to do a bit of testing after I get updated permissions. I keep getting the read only. I need to get updated permissions and then check.

  5. #20
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    To tidy it up use the code I supplied, it's pretty much identical, it just uses strSQL as the variable for the statement.
    It's my preferred method of concatenating long strings, there is a limit to using the continuation lines ( & _ ) of about 15 lines so you never hit that.

    If you use something like Notepad ++ you can press shift and alt to block select text and insert / delete blocks of text in a really efficient way. SSMS has the functionality.
    Once you get used to it you'll never look back, and get really annoyed that you can't do the same in the VBA editor.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  6. #21
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by Minty View Post
    To tidy it up use the code I supplied, it's pretty much identical, it just uses strSQL as the variable for the statement.
    It's my preferred method of concatenating long strings, there is a limit to using the continuation lines ( & _ ) of about 15 lines so you never hit that.

    If you use something like Notepad ++ you can press shift and alt to block select text and insert / delete blocks of text in a really efficient way. SSMS has the functionality.
    Once you get used to it you'll never look back, and get really annoyed that you can't do the same in the VBA editor.
    So before I go asking for updated permissions, I just wanna verify this code works. I can update the SQL servers tables through the use of linked tables in access but when attempting to use a passthrough query (on both my read and RW account) I just get the error saying that the database or object is read only. Do you happen to know if writing through linked tables vs passthrough are different permissions on SSMS?

  7. #22
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Quote Originally Posted by Vita View Post
    So before I go asking for updated permissions, I just wanna verify this code works. I can update the SQL servers tables through the use of linked tables in access but when attempting to use a passthrough query (on both my read and RW account) I just get the error saying that the database or object is read only. Do you happen to know if writing through linked tables vs passthrough are different permissions on SSMS?
    In theory RW permissions would be sufficient for an SELECT or an UPDATE statement to be executed.
    I assume you are using a generic User account in your connection string, If you connect to the same server in SSMS with those same credentials can you execute the query from the pass-through?

    If you can but you can't from access as a pass-through that would be strange.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  8. #23
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by Minty View Post
    In theory RW permissions would be sufficient for an SELECT or an UPDATE statement to be executed.
    I assume you are using a generic User account in your connection string, If you connect to the same server in SSMS with those same credentials can you execute the query from the pass-through?

    If you can but you can't from access as a pass-through that would be strange.
    So I got the permissions updated but the line WTrst.Edit still gives me an error of read-only. Even in the test database that isn't being used anywhere else.
    Any ideas?

  9. #24
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Usually with DAO recordsets based on SQL Server tables you need to use the option dbSeeChanges, so your line should be:
    Code:
    Set WTrst = WTqdf.OpenRecordset(dbOpenDynaset, dbSeeChanges)
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  10. #25
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by Gicu View Post
    Usually with DAO recordsets based on SQL Server tables you need to use the option dbSeeChanges, so your line should be:
    Code:
    Set WTrst = WTqdf.OpenRecordset(dbOpenDynaset, dbSeeChanges)
    Cheers,
    I tried this and I get the same error.
    (3027): Cannot update. Database or object is read-only.

    I also tried dbOpenSnapshot and got this:
    (3251): Operation is not supported for this type of object.

  11. #26
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    I believe its because of the fact that the query is a passthrough query. Though now I feel like I've gone in a circle as I'm back to trying to figure out how to read if the record is locked or not. (or set time/retry amount for the odbc command.)

  12. #27
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    You said you're getting the error on the WTrst.Edit line so you didn't get to the pass-through, I thing setting the ReturnRecords=False might have something to do with it. Can you try this updated code:
    Code:
    Public Function WorkticketUpdate()        \
    On Error GoTo Error_Connection
    Call SetVars
            
    Dim WTqdf As DAO.QueryDef, WTrst As DAO.Recordset, WTdb As DAO.Database, UpdateSQL As String, Number
    
    
    'Checks to see if the record is locked
    Set WTdb = CurrentDb
    
    
    Set WTrst = WTdb.OpenRecordset("SELECT * FROM " & Replace(TableFound, "_", ".", , 1) & " WHERE " & StatusCriteria & ";",dbOpenDynaset,dbSeeChanges) 'Replace is needed because when access saves linked tables it replaces "." with "_"
    
    
    WTrst.Edit 'if locked should go to error handler
    
    
    
    
    Set WTqdf= WTdb.CreateQueryDef("")
    
    
    WTqdf.Connect = "ODBC;Driver={SQL Server Native Client 11.0};Server=server;Database=database;Network=DBNMPNTW;UID=UID;PWD=PWD;"
    WTqdf.ReturnsRecords = False
    
    
    'Shortens the SQL statement
    UpdateSQL = "UPDATE " & Replace(TableFound, "_", ".", , 1) & " SET StatusCode = 'COM', " & _
    		"StatusTime = '" & Format(Time, "hhmm") & "', " & _
    		"CurrentStatusComment = 'T/T COMPLETED', " & _
    		"TimeUpdated = '" & CurrentTime() & "', " & _
    		"DateUpdated = '" & Date & "', " & _
    		"UserUpdatedKey = '" & sUserKey & "', " & _
    		"StatusDate = '" & Format(Date, "yyyy-mm-dd") & "' " & _
    		"WHERE " & StatusCriteria
    
    
    'Sets the sql string for the actual update and executes it
    WTqdf.SQL = UpdateSQL
    WTqdf.Execute 
    
    
    Exit Function
    
    
    
    
    Error_Connection:
        Dim dbeError As Error
        For Each dbeError In DBEngine.Errors
            Debug.Print "(" & dbeError.Number & "): " & dbeError.Description
        Next
        Call Msgbox("Cannot mark step complete. Someone may be editing the DHR" & vbCrLf & "Error: " & Error & " " & Err.Number)
    End Function
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  13. #28
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Have you tried Minty's suggestion to log in the SSMS with your updated credentials and see if you can run the pass-through in there (debug.Print the UpdateSQL variable and pasted in the query window in SSMS)?

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  14. #29
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by Gicu View Post
    You said you're getting the error on the WTrst.Edit line so you didn't get to the pass-through, I thing setting the ReturnRecords=False might have something to do with it. Can you try this updated code:
    Code:
    Public Function WorkticketUpdate()        \
    On Error GoTo Error_Connection
    Call SetVars
            
    Dim WTqdf As DAO.QueryDef, WTrst As DAO.Recordset, WTdb As DAO.Database, UpdateSQL As String, Number
    
    
    'Checks to see if the record is locked
    Set WTdb = CurrentDb
    
    
    Set WTrst = WTdb.OpenRecordset("SELECT * FROM " & Replace(TableFound, "_", ".", , 1) & " WHERE " & StatusCriteria & ";",dbOpenDynaset,dbSeeChanges) 'Replace is needed because when access saves linked tables it replaces "." with "_"
    
    
    WTrst.Edit 'if locked should go to error handler
    
    
    
    
    Set WTqdf= WTdb.CreateQueryDef("")
    
    
    WTqdf.Connect = "ODBC;Driver={SQL Server Native Client 11.0};Server=server;Database=database;Network=DBNMPNTW;UID=UID;PWD=PWD;"
    WTqdf.ReturnsRecords = False
    
    
    'Shortens the SQL statement
    UpdateSQL = "UPDATE " & Replace(TableFound, "_", ".", , 1) & " SET StatusCode = 'COM', " & _
            "StatusTime = '" & Format(Time, "hhmm") & "', " & _
            "CurrentStatusComment = 'T/T COMPLETED', " & _
            "TimeUpdated = '" & CurrentTime() & "', " & _
            "DateUpdated = '" & Date & "', " & _
            "UserUpdatedKey = '" & sUserKey & "', " & _
            "StatusDate = '" & Format(Date, "yyyy-mm-dd") & "' " & _
            "WHERE " & StatusCriteria
    
    
    'Sets the sql string for the actual update and executes it
    WTqdf.SQL = UpdateSQL
    WTqdf.Execute 
    
    
    Exit Function
    
    
    
    
    Error_Connection:
        Dim dbeError As Error
        For Each dbeError In DBEngine.Errors
            Debug.Print "(" & dbeError.Number & "): " & dbeError.Description
        Next
        Call Msgbox("Cannot mark step complete. Someone may be editing the DHR" & vbCrLf & "Error: " & Error & " " & Err.Number)
    End Function
    Cheers,
    This doesn't seem to work. While it doesn't give me an error, it also doesn't detect when the record is locked by the ERP/third party software. It jumps over to the update and then freezes access up for about 2 minutes then gives me this:
    Code:
    (0): [Microsoft][SQL Server Native Client 11.0]Query timeout expired(3146): ODBC--call failed.

    Quote Originally Posted by Gicu View Post
    Have you tried Minty's suggestion to log in the SSMS with your updated credentials and see if you can run the pass-through in there (debug.Print the UpdateSQL variable and pasted in the query window in SSMS)?

    Cheers,
    I have not. I plan to but I don't have direct access to the SQL server so I am trying to schedule some time with one of the 2 people that do.

  15. #30
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    You don't need direct access to the server, just an ability to connect to it?
    You can run SSMS from your local pc as long as you can connect to the SQL server.

    There's no point running the query on an Admins login, if they can't run it nobody could
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

Page 2 of 3 FirstFirst 123 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 18
    Last Post: 10-17-2022, 06:13 AM
  2. CurrentDb.Execute Question
    By d9pierce1 in forum Programming
    Replies: 10
    Last Post: 09-02-2020, 10:53 AM
  3. RunSQL vs. QueryDef.Execute vs. CurrentDB.Execute
    By caubetiep1337 in forum Queries
    Replies: 8
    Last Post: 12-16-2015, 05:35 PM
  4. vba code help - Currentdb.execute
    By joycesolomon in forum Programming
    Replies: 5
    Last Post: 08-11-2014, 11:25 AM
  5. Update sql using currentdb.execute
    By laavista in forum Access
    Replies: 13
    Last Post: 08-15-2011, 03:51 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums