Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 28th, 2006, 03:59 PM
Authorized User
 
Join Date: Sep 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default wSheet Protect and SQL Query problem

I have 12 worksheets that have four different queries that run based on the selection from a userform menu. To prevent changes I lock the sheets using the UserInterfaceOnly option of the Protect property. Everything else in my script runs fine except for the query. I believe it is because I do not refresh the data but instead I delete any old queries and add a new query to the wSheet. I know I could do this by temporarily unlocking the wSheet but I'd like to avoid that since it does leave the code open if an error occurs or someone stops the script while processing.



Sub Workbook_Open()
    AutomatedUpdate.LockSheets 'Set the password and UserInterfaceOnly option for each worksheet
    AutomatedUpdate.CheckForNightUpdate 'Checks the current time and executes different scripts based on the time of day
End Sub


Sub LockSheets()
    Dim wSheet As Worksheet
    For Each wSheet In ActiveWorkbook.Worksheets
        wSheet.Protect _
            Password:="MyPassword", _
            UserInterFaceOnly:=True
    Next wSheet
End Sub




Thanks in advance for any assistance

DaveG
 
Old January 5th, 2007, 08:50 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 173
Thanks: 0
Thanked 3 Times in 3 Posts
Default

I think you're stuck with this protection problem. You'll have to unprotect / protect the worksheet when running the update to get it to work.

If you're worried about leaving the sheet unprotected on unexpected program termination there are a couple of things you can do reduce the likelihood of leaving the sheet unprotected.
1) You can cut out user interferance by using Application.XlEnableCancelKey = xlDisabled (or xlErrorHandler). This will stop the user being able to interupt program flow with Ctrl + Break.
2) Introduce an error handling section such that on any error the program flow is directed to a closing clean-up section that will include the re-protection of the required sheets.

Code:
Sub Test()

    On Error GoTo ErrHandler

    ' Main program

    Exit Sub

ErrHandler:
    MsgBox ("Error Occurred!")
    ActiveSheet.Protect
    ' Other clean up

End Sub
Maccas






Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Query Problem mike.chary SQL Server 2005 1 September 26th, 2007 06:19 AM
SQL Query problem skarthikk SQL Server 2000 1 August 7th, 2006 02:20 AM
SQL query problem - Need Help Settt Biztalk 0 September 12th, 2005 09:32 AM
Sql Query Problem mateenmohd SQL Language 0 January 13th, 2004 05:31 AM
sql query problem mateenmohd Classic ASP Databases 8 June 19th, 2003 02:05 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.