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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using VBA to display userID 1

Status
Not open for further replies.

xitu

Technical User
Oct 1, 2003
55
US
I am working in a network folder (multiusers environment). Is there anyway I know who's opening the file?

Thanks,
XT
 
What file? Do you mean you want to know who last opened it, who currently has it open, or???

You need to supply more details, please.

VBAjedi [swords]
 
If it is a shared access DB, this FAQ may help:
faq705-4894

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
I am writing VBA code in ArcGIS and save my code in a network folder. Since we are building modules and save them in a template, we may step on each other's toes. The last user hits the Save button will overwrite everything. Before hitting the Save button, I want to know is there anyone is working on the VBA code.

Thanks,
XT
 
It sounds like what you really need is Source Management software. What happens if you start working on the code at 9am, then another developer starts at 9:15am. The other developer goes to lunch at 11am and saves his work and closes the app. You go to lunch at 12pm. Even if you had a way to see who was accessing the source files, it wouldn't help with this. In this case, not only would your changes be made on the wrong code, when you saved your work, you would completely wipe out two hours of your coworkers efforts.

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
This is not a "busy" project. Chances to wipe out others' code are very low. We have only three developers and usually don't work at the same time but I just want to make sure ... and know how to deal with VBA.

Thanks,
XT
 
VBAjedi,

I would like to know who currently has it open.

Thanks,
XT
 
It doesn't really sound like it is a vba issue. It is purely a file access issue. This is a VBScript that should show you who is accessing a share. You should be able to modify it for your needs.
Code:
sHost="YourServerName"    'the host of the share
set fsvc=getobject("WinNT://"& sHost & "/LanmanServer")
set cResources=fsvc.Resources
for each oResource in cResources
    with oResource
        wscript.echo .path & vbTab & .lockcount & vbcrlf & .user
    end with
next
set cResources=nothing
set fsvc=nothing

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
If the risk of simultaneous changes is really that low, and you don't want to use Source Management software, why have the file shared at all? Just save it as a normal file so only one person can have write access at any given time.

VBAjedi [swords]
 
TomThumbKP,
How do I modify if my template file is TEMPLATE.mxt and store in a network folder \\AAA\BBB\CCC ?

VBAjedi,
Two users can open the TEMPLATE.mxt and save at the same time. It does not have a lock file like Excel or Access.

Thanks,
XT
 
You should b able to change the YourServerName value to whatever the name of your server is. THen run it and see what you get for output. It has been a while since I worked with this particular piece of code.

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
xitu, does ArcGIS create temporary files like Word does? If you open a document called MyFile.doc in Word, a hidden temporary file called ~$yFile.doc is created in the same directory. If ArcGIS behaves in a similar way you could test for the presence of this file.

Nelviticus
 
Nelviticus,

I have tried this but no luck.

Private Function MxDocument_OpenDocument() As Boolean
Dim strFileName As String

' Full path and name of file.
strFileName = "\\AAA\BBB\CCC\Template.mxt"

' Call function to test file lock.
If Not FileLocked(strFileName) Then
' If the function returns False, open the document.
'Documents.Open strFileName <=== Not working in ArcGIS
'MsgBox ("You are allowed to modify the mxt")
End If
End Function


Function FileLocked(strFileName As String) As Boolean
On Error Resume Next

' If the file is already opened by another process,
' and the specified type of access is not allowed,
' the Open operation fails and an error occurs.
Open strFileName For Binary Access Read Write Lock Read Write As #1
Close #1

' If an error occurs, the document is currently open.
If Err.Number <> 0 Then
' Display the error number and description.
MsgBox "Error #" & Str(Err.Number) & " - " & Err.Description
FileLocked = True
Err.Clear
End If
End Function

Thanks,
XT
 
I don't think I can really answer your question, but if I was in your situation I'd take a mental step back and think about things differently.

Firstly, are you sure that ArcGIS doesn't already handle conflicts like this? Get someone else to create and save a test template, then try to open it on your machine while they're editing it. Make some changes and save it, then get the other user to do the same. If ArcGIS is well-written it should at least give one of you a warning at some point.

If that doesn't work and you can't use code to test whether the file is locked, try another method. One idea is to write some code that creates a text file in the same directory when the someone opens the document, then deletes it when they close it (emulating Word's behaviour as detailed above). You could test for the existence of this text file on opening, and if it's present you can assume that someone else has the document open.

Just a couple of ideas.

Nelviticus
 
I still say that if there is a no-zero probability of two developers editing at the same time, then Source Management Software is the best solution. Why reinvent the wheel?

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
Tom,

I agree with you. However, sometimes it's hard to convince your company to shell out cash to go buy a quality wheel when they hired you to be a wheel-builder!

:)

Know any free Source Management software?

VBAjedi [swords]
 
WINCvs provides a free CVS server for source management and TortoiseCVS provides a free windows based interface for it. The sum investment is about 1 hour to set it up.

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
Thanks for that tip - have a glowing heavenly orb! :)

VBAjedi [swords]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top