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:
The following code to open a file when you double click on it in a listbox:
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