ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Unprotect on open (https://www.excelbanter.com/excel-programming/394757-unprotect-open.html)

wagz

Unprotect on open
 
I need code that will check the network username and unprotect a worksheet
when the workbook opens. So on open it will check the username and then
unlock the worksheets if it matches a list of approved users. If not, it
will remain locked.

I am using excel 2007.

Thanks for all the help in advance.



Patrick

Unprotect on open
 
I use an Access database on our shared network to validate against. You will
need to add the appropriate references.

Public Pass As Boolean
Dim cn As ADODB.Connection, rs As ADODB.Recordset, intColIndex As Integer
Public Sub UrIdentity()
Pass = False
Set TargetRange = Range("X1")
Set cn = New ADODB.Connection
DBFullName = "P:\Permanent_Data\Patrick\Security.mdb"
TableName = "Authorized"
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & DBFullName &
";"
Set rs = New ADODB.Recordset
With rs
.Open "SELECT * FROM " & TableName & " WHERE Emp LIKE '" &
UCase(frmLogIn.txtUserNumb) & "'", cn, , , adCmdText
For intColIndex = 0 To rs.Fields.Count - 1 ' the field names
TargetRange.Offset(0, intColIndex).Value = rs.Fields(intColIndex).Name
Next
TargetRange.Offset(0, 0).CopyFromRecordset rs ' the recordset data
End With
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
If Range("X1").Value < "Emp" And Range("Y1").Value < "" Then
Pass = True
Else
Pass = False
End If
Range("X1:Y1").ClearContents
Range("A2").Select
End Sub

Ferris[_2_]

Unprotect on open
 
On Aug 3, 10:46 am, wagz wrote:
I need code that will check the network username and unprotect a worksheet
when the workbook opens. So on open it will check the username and then
unlock the worksheets if it matches a list of approved users. If not, it
will remain locked.

I am using excel 2007.

Thanks for all the help in advance.


Environ("Username") will return the name of the user. You could use
that to validate against a source of your choosing.



All times are GMT +1. The time now is 09:30 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com