Link to home
Start Free TrialLog in
Avatar of michaeljusino1017
michaeljusino1017

asked on

Variable in a Message Box

Ok I have the following code:

Option Compare Database
Option Explicit
Private lngFile As Long
Private Sub Form_Load()
If Dir("D:\Data\Lockout.txt") <> "" Then
  MsgBox "Sorry, someone else is using this file. Try again later."
  Application.Quit
End If
lngFile = FreeFile
Open "D:\Data\Lockout.txt" For Output As lngFile
End Sub

Private Sub Form_Unload(Cancel As Integer)
Close lngFile
Kill "D:\Data\Lockout.txt"
End Sub

In the MsgBox how can I put MsgBox "Sorry (lookup userid field from usertable who is using the file) is using the file"

For exampl, say John is using the file and Mike want to open....

Mike should get the message "Sorry John is using the file, Try again later"
Avatar of dmitryz6
dmitryz6

MsgBox "Sorry " & dlookup("fieldname","tablename") & " is using the file"
Avatar of mbizup
Are you using CurrentUser, Environ(UserName) or something else to lookup the user ID?

try something like this:
MsgBox "Sorry, " & dlookup("UserID", "tblUsers", "UserName = '" & Environ(UserName) & "'") & " is using this file. Try again later."
Dmitry got there first.
Avatar of michaeljusino1017

ASKER

i'm using Environ(Username) but I dont want to display my name, I want to display the name of the person who is currently using the file.....

Also, I noticed:

lngFile = FreeFile
Open "D:\Data\Lockout.txt" For Output As lngFile

doesn't actually open a file or put one in the directory...is there another way to call a function to actually create the file Locout.txt and save it in D:\Data\
Hi michaeljusino1017,
Are you hoping someone is going to tell you how to find out who has a particular file open?
This info is obtained using the API NetFileEnum, but the user would have to be a Windows System/Network Administrator to be able to use that API.

Pete
no I am just trying to lock the file, and keep it from being opened by another user.  It seem opening it exclusively still allows other users to open the file.  What I am trying to do is create a file the minute the MS Access File is opened and then locking the MS Access file by checking to see if there is a text file inthe directory.  The minute someome closes the MS Access file the text file is deleted and the MS Access file is accesible to anyone on the network

At the same time, I want to display the user that is currently using the file so that if someone really needs to get in to the file badly, say an adminstrator, they can call that person up and say, close the file
In this case I would create lock file using Environ(UserName)

Option Compare Database
Option Explicit
Private lngFile As Long
Private Sub Form_Load()
If Dir("D:\Data\*") <> "" Then
  MsgBox "Sorry " &  Left(Dir("D:\Data\*"),InStr(Dir("D:\Data\*"),"-")-1) & " is using the file"
  Application.Quit
End If
lngFile = FreeFile
Open "D:\Data\" & Environ(UserName) & "-Lockout.txt" For Output As lngFile  '<-Create with current user name  file
End Sub

Private Sub Form_Unload(Cancel As Integer)
Close lngFile
Kill "D:\Data\Lockout.txt"
End Sub

In the MsgBox how can I put MsgBox "Sorry (lookup userid field from usertable who is using the file) is using the file"
ASKER CERTIFIED SOLUTION
Avatar of dmitryz6
dmitryz6

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial