Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run code dependent on user 1

Status
Not open for further replies.

Ozrodent

Technical User
Jul 31, 2002
7
AU
Morning all,

I need to do some maintenance, (Backups) on my local machine which I have the software to do. No one else that uses my database needs to do this, Or has the software. I want this as an automated process, I had the bright idea of using the "Call" function in an on open event - the problem is that other users discover an error then the database does not open.

Any Help or advise to achieve this would be greatly appreciated


Phil
 
Well,

If you have Access security set up you can just get the current user, but if you don't you can use
Environ("Username")

To get your network username when the database opens up, so if it's not you, then it won't run. This is, of course, assuming you're using WinNT or 2000 or better...

Code:
[COLOR=blue]
Function[/color] GetUsername() [COLOR=blue]As String[/color]
    GetUsername = Environ("UserName")
[COLOR=blue]End Function[/color]

Or, since the software is only on one PC, you could set it up to only run on that PC:

Code:
[COLOR=blue]
Function[/color] GetComputerName() [COLOR=blue]As String[/color]
    GetUsername = Environ("ComputerName")
[COLOR=blue]End Function[/color]


Kyle
 
Kyle,
Thanks for that, but (there's always a but) how do I then call my program? bearing my Machine mane is AUELZWDPRS49F7A


 
There's always a "but", otherwise, this stuff wouldn't be fun!

Leave your code just as it is, and then add this:
Code:
[COLOR=blue]Sub .....[/color]
    [COLOR=blue]If[/color] Environ("ComputerName") = AUELZWDPRS49F7A [COLOR=blue]Then[/color]
        [COLOR=green]'Do your thing Here[/color]
    [COLOR=blue]End If[/color]

    [COLOR=green]'Rest of your code that everyone can use[/color]
[COLOR=blue]End Sub[/color]

Kyle
 
This is what I Have so far

Private Sub Form_Open(Cancel As Integer)
GetComputerName() As String
On Error GoTo GetComputerName_Err
GetComputerName = Environ("ComputerName")
If Environ("ComputerName") = "AUELZWDPRS49F7A" Then
'Call Shell("C:\Program Files 'My thing Here
End If

GetComputerName_Exit:
Exit Function

GetComputerName_Err:
MsgBox Error$
Resume GetComputerName_Exit
ResizeForm Me
End Function


And ALL I Get is Errors, This really is SO much fun
 
Well, you should be able to turn this:
Code:
Private Sub Form_Open(Cancel As Integer)
GetComputerName() As String
On Error GoTo GetComputerName_Err
    GetComputerName = Environ("ComputerName")
    If Environ("ComputerName") = "AUELZWDPRS49F7A" Then
        'Call Shell("C:\Program Files 'My thing Here
    End If

GetComputerName_Exit:
    Exit Function

GetComputerName_Err:
    MsgBox Error$
    Resume GetComputerName_Exit
    ResizeForm Me
End Function

Into thi:
Code:
Private Sub Form_Open(Cancel As Integer)
On Error GoTo GetComputerName_Err

    If Environ("ComputerName") = "AUELZWDPRS49F7A" Then
        'Call Shell("C:\Program Files 'My thing Here
    End If

GetComputerName_Exit:
    Exit Function

GetComputerName_Err:
    MsgBox Error$
    Resume GetComputerName_Exit
    ResizeForm Me
End Sub

Remember to change the "End Function" to "End Sub"

If you're still getting errors, try running this sub and see what happens:

Code:
Sub TestMe()
    MsgBox Environ("ComputerName") 
End Sub


Kyle
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top