Make a file read only when stored/opened from access database. (1 Viewer)

aman

Registered User.
Local time
Yesterday, 23:04
Joined
Oct 16, 2008
Messages
1,250
I am writing the following code to link a file to Access database. The link gets stored in the table and also appears on the listbox but when i double click on the listbox to open a file then it is opened in a compatible mode . My question is how can i open the file in read only mode?

Following code to store file name/path in a listbox and store in a table:
Code:
Private Sub bImport1_Click()

Dim strMsgReturn As String, strFilePath As String, strFileName As String, strFolderPath As String
Dim strNewFilePath As String, strFileType As String
Dim FName As String
'Const GetLinkedFilesPath = "L:\Access Databases\Group Manufacturing\Mortgages Direct\Scanned Documents\"
 Dim GetLinkedFilePath As String

GetLinkedFilePath = CreateImportFolder()

  Dim strsql                  As String
  Dim db                      As DAO.Database
  Dim rs                      As DAO.Recordset

    strFolderPath = Trim(Me.txtFile)
    
    Do Until Right((strFolderPath), 1) = "\"
        strFolderPath = Left(strFolderPath, Len(strFolderPath) - 1)
    Loop
        
    'Determine file name
    strFileName = Mid(Me.txtFile, Len(strFolderPath) + 1)
    
    'Determine file type e.g. ".doc" so it can be added to new filename if needed
    strFileType = Trim(strFileName)
    
    If InStr(strFileType, ".") = 0 Then 'file has no file type suffix . . .reject it!
        MsgBox "This file cannot be used as the file type is unknown    ", vbCritical, "Unknown file type"
        Exit Sub
    Else 'file type suffix OK. . . .
        Do Until Left((strFileType), 1) = "."
        strFileType = Mid(strFileType, 2)
        Loop
    End If
       
    
    strNewFilePath = GetLinkedFilePath & strFileName
    'MsgBox "strNewFilePath=" & strNewFilePath
    If Len(strNewFilePath) > 255 Then
        strMsgReturn = MsgBox("The file name is too long (>255 characters)     " & vbNewLine & _
          "Do you want to save it with a new name?", vbCritical + vbYesNo, "Copy selected file?")
        If strMsgReturn = vbYes Then
            strFileName = InputBox("Enter a new name for this file", "New file name")
            strNewFilePath = GetLinkedFilePath & strFileName & strFileType
            FileCopy strFilePath, strNewFilePath
        Else
            MsgBox "The file was not linked as its file name was too long (>255 characters)     "
        End If
    End If
    
    'Check whether file already exists
    If (Dir(strNewFilePath) = "") Then 'file missing  . . . so copy it
        FileCopy Me.txtFile, strNewFilePath
    
    Else
Rename:
        strMsgReturn = MsgBox("Another file with this name already exists on the network.     " & vbNewLine & _
          "Do you want to save it with a new name?", vbCritical + vbYesNo, "Copy selected file?")
        If strMsgReturn = vbYes Then
            strFileName = InputBox("Enter a new name for this file", "New file name")
            strNewFilePath = GetLinkedFilePath & strFileName & strFileType
            
            If (Dir(strNewFilePath) <> "") Then GoTo Rename 'this file also exists, so try again
            FileCopy Me.txtFile, strNewFilePath 'copy the file
        Else ' Use existing linked file - CR v4683
            Exit Sub
        End If
    End If
       
  Me.LinkedFile = strNewFilePath
  'MsgBox strFileName
  Set db = CurrentDb()
  strsql = "SELECT * FROM tImport WHERE 1=0"
  Set rs = db.OpenRecordset(strsql, dbOpenDynaset, dbSeeChanges)

  rs.AddNew
  rs!FilePath = Me.LinkedFile
  rs!FileName = strFileName
  rs!ActivityRef = Me.txtRefNo
  rs!FormRef = Fref
  rs.Update
  
  MsgBox "The new file has been attached", vbInformation + vbOKOnly, "Added"
  Me.txtFile = ""
  
  DoCmd.Close acForm, Me.Name

End Sub

The following code to open a file when you double click on it in a listbox:
Code:
Private Sub lstDocs_DblClick(Cancel As Integer)
Dim i As Integer
For i = 0 To lstDocs.ListCount - 1
If Me.lstDocs.Selected(i) = True Then
   OpenNativeApp Me.lstDocs
   Exit For
End If
Next i
End Sub

Code:
Option Explicit

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpszOp As String, ByVal lpszFile As String, ByVal lpszParams As String, ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long

Private Declare Function GetDesktopWindow Lib "user32" () As Long
Const SW_SHOWNORMAL = 1
Const SE_ERR_FNF = 2&
Const SE_ERR_PNF = 3&
Const SE_ERR_ACCESSDENIED = 5&
Const SE_ERR_OOM = 8&
Const SE_ERR_DLLNOTFOUND = 32&
Const SE_ERR_SHARE = 26&
Const SE_ERR_ASSOCINCOMPLETE = 27&
Const SE_ERR_DDETIMEOUT = 28&
Const SE_ERR_DDEFAIL = 29&
Const SE_ERR_DDEBUSY = 30&
Const SE_ERR_NOASSOC = 31&
Const ERROR_BAD_FORMAT = 11&


Public Sub OpenNativeApp(ByVal psDocName As String)
Dim r As Long, msg As String

r = StartDoc(psDocName)
If r <= 32 Then
    'There was an error
    Select Case r
        Case SE_ERR_FNF
            msg = "File not found"
        Case SE_ERR_PNF
            msg = "Path not found"
        Case SE_ERR_ACCESSDENIED
            msg = "Access denied"
        Case SE_ERR_OOM
            msg = "Out of memory"
        Case SE_ERR_DLLNOTFOUND
            msg = "DLL not found"
        Case SE_ERR_SHARE
            msg = "A sharing violation occurred"
        Case SE_ERR_ASSOCINCOMPLETE
            msg = "Incomplete or invalid file association"
        Case SE_ERR_DDETIMEOUT
            msg = "DDE Time out"
        Case SE_ERR_DDEFAIL
            msg = "DDE transaction failed"
        Case SE_ERR_DDEBUSY
            msg = "DDE busy"
        Case SE_ERR_NOASSOC
            msg = "No association for file extension"
        Case ERROR_BAD_FORMAT
            msg = "Invalid EXE file or error in EXE image"
        Case Else
            msg = "Unknown error"
    End Select
'    MsgBox msg
End If
End Sub


Private Function StartDoc(psDocName As String) As Long
Dim Scr_hDC As Long

Scr_hDC = GetDesktopWindow()
StartDoc = ShellExecute(Scr_hDC, "Open", psDocName, "", "C:\", SW_SHOWNORMAL)
End Function
 

isladogs

MVP / VIP
Local time
Today, 07:04
Joined
Jan 14, 2017
Messages
18,212
it may depend on the type of file (which you haven't specified) and how you are viewing it.

If its an Excel file, it is read only in Access anyway so you don't need to do anything.

One thing to be aware of is that if you can open a file from Access, you can almost certainly export data to that file and therefore edit it.
 

aman

Registered User.
Local time
Yesterday, 23:04
Joined
Oct 16, 2008
Messages
1,250
It could be any file like excel,word document ,pdf or any scanned document.

So how can i make them read only so that users are not allowed to edit the data ?

Thanks
 

isladogs

MVP / VIP
Local time
Today, 07:04
Joined
Jan 14, 2017
Messages
18,212
You haven't said how you are viewing the file.
As already stared if you view an excel spreadsheet as an Access table, it will be read only.
PDF files and scanned documents will also be read only.

However if you are opening the file in its default application such as Word or Excel, you need to do one of the following:
1. make the file read only in the first place
2. Add code to create a copy of the file which can then be deleted after use
It won't be read only but it won't matter if its edited.
3. Open the file in a non trusted location so its in protected mode
4. Move or copy the file to a network location where users do not have editing rights

There may well be other ways but that gives you plenty to consider.
In a similar situation, I used method 4 and it worked well
 

aman

Registered User.
Local time
Yesterday, 23:04
Joined
Oct 16, 2008
Messages
1,250
What if i need to write down the vba code to open the file as read only when double click event occurs on the listbox?
 

aman

Registered User.
Local time
Yesterday, 23:04
Joined
Oct 16, 2008
Messages
1,250
Ridders , The code i posted in my 1st post does the following things

1. Makes a copy of the file selected in a secure folder .
2. Opens up the file when double click event occurs on the listbox from the secure folder.

The only thing i want to do is make the file read only when its copied to secure folder.
Still can't figure it out what code needs to be written to make the file read only when its copied from any network location to secure folder.
 

isladogs

MVP / VIP
Local time
Today, 07:04
Joined
Jan 14, 2017
Messages
18,212
Ridders , The code i posted in my 1st post does the following things

1. Makes a copy of the file selected in a secure folder .
2. Opens up the file when double click event occurs on the listbox from the secure folder.

The only thing i want to do is make the file read only when its copied to secure folder.
Still can't figure it out what code needs to be written to make the file read only when its copied from any network location to secure folder.

Hi

Yes I'm aware what you have now & what you want.
My recommendation is to go for option 4.
Move or copy the file to a network location where users do not have editing rights

Whilst you may be able to set folder properties from Access, there is little point if you are specifying a particular folder where all documents are stored.
Its easy enough to test the idea by creating a folder & then editing its properties in Explorer using the Security tab



You'll need to deny the Write property for standard users & possibly others as well
 

Attachments

  • Capture.PNG
    Capture.PNG
    18.8 KB · Views: 650

aman

Registered User.
Local time
Yesterday, 23:04
Joined
Oct 16, 2008
Messages
1,250
Ridders, I can't see security option in folder properties. I think this is hidden because of security restrictions at work.
 

isladogs

MVP / VIP
Local time
Today, 07:04
Joined
Jan 14, 2017
Messages
18,212
Excellent - just how it should be!
Ask your network manager to set this up on your behalf
 

Users who are viewing this thread

Top Bottom