Thread: login username
View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Arvi Laanemets Arvi Laanemets is offline
external usenet poster
 
Posts: 510
Default login username

Hi

Some sugestions for OP.

Instead keeping allowed users in text file, write them into this Open event,
p.e into an array - you can define and fill it at top of procedure, so it'll
be easy to edit in future. P.e.

Private Sub Workbook_Open()

Dim arrUsers(3) As Variant
Dim intUsers As Integer, i As Integer
Dim usrname As String

arrUsers(1)="First user"
arrUsers(2)="Second user"
arrUsers(3)="Third user"
intUsers=3

usrname = Environ("Username")

For i=1 To intUsers
If arrUsers(i) = usrname Then
MsgBox "Hello " & usrname & " OK to open file"
GoTo 100
End If
Next i
MsgBox " Not authorised"
ThisWorkbook.Close savechanges:=False
100:
End Sub

To avoid the user bypassing macros, make all essential sheets very hidden,
with only a dummy sheet with warning about unauthorizaed access visible. Add
a couple of code rows at end of sub (before End Sub), which make right
sheets visible and hide the dummy sheet, when allowed user opens the file.
And the last step will be to protect your VBA-Project with password.

Of course a determinated and skillfull user can hack it without any
problems, but not an average Joe


--
Arvi Laanemets
( My real mail address: arvi.laanemets<attarkon.ee )


"Mike H" wrote in message
...
Hi,

I think the code below will do what you want but a couple of cautionary
notes. Any macro based solution is dependent upon the user enabling macros
and if you want to see how difficult that is then Google it or search
these
forums. In addition what is to stop someone editing you text file. If
you're
tring to keep information confidential except for privileged users then
this
isn't a solution many would have faith in.

Private Sub Workbook_Open()
usrname = Environ("Username")
Open "C:\users.txt" For Input As 1 'edit to your path/file
Do While Not EOF(1)
Input #1, authname
If authname = usrname Then
MsgBox "Hello " & usrname & " OK to open file"
GoTo 100
End If
Loop
Close #1
MsgBox " Not authorised"
ThisWorkbook.Close savechanges:=False
100:
Close #1
End Sub


Mike

"Ranjit kurian" wrote:

I have created a txt file which contain employees NT names

I need a macro, so that when ever the person try to open my excel file,
it
should check the NT name of his/her in txt file,and if the NT name exist
in
txt file he should be allowed to open my excel file...