Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2003
    Location
    Cleveland USA
    Posts
    184

    Unhappy Unanswered: How do I open a file in code?

    In my db, I have documents that relate to a given record. I've stored the filenames in a text field. Now that I have file names stored as text, how do I open the files from Access??? I want the user to click a button that says "Get File" and have it open in a separate window.

  2. #2
    Join Date
    Jun 2004
    Location
    Terrapin Nation
    Posts
    205
    what kind of files? there has to be an easy way to do it. look at the FileDialog in help and see if you can track down the command.

  3. #3
    Join Date
    Aug 2003
    Location
    Cleveland USA
    Posts
    184

    Unhappy

    The file can be any type- .doc, .rtf, .pdf, .jpg, etc. I want it to open in a new window in the correct software. I don't need a file dialog box since I already have the file name. I just want to take the filename from the textbox and open it from code. I will look around some more to check into your idea...

  4. #4
    Join Date
    Feb 2004
    Location
    Chicago, IL
    Posts
    1,312
    The the field that the filename is in a hyperlink? If you make it a hyperlink there is a method to follow the hyperlink. I don't remember exactly how it works (I'm at work with Access 97).

  5. #5
    Join Date
    Feb 2004
    Posts
    533
    Do a search on the 'Shell' command for more info.
    Code:
    ' Here's an example of the file open for a text file.
    ' It can easily be modified for Word file and other file.
    
    ' In my app I check the last three characters of the file name 
    ' to determine the Extension and then used a select case to call
    ' a sub procedure having the correct app name for the 
    ' type of file. There could be a more efficient way writing this
    ' without creating a sub for each file type to open, I'm sure.
    
    ' I use this particular sub in an Excel app but it should work
    ' just as well in access or only need a little modification.
    
    ' In this bit of code the 'NameFile' Variable is a module level
    ' variable set in the calling procedure. You'd need to set it
    ' prior to the shell command, or just Dim it in your procedure.
    ' This first tries to activate the app assuming it may already
    ' be open. If fails to activate Go's to the Shell command.
    
    
    Sub OpenaTxt()
    Dim AppName, AppFile As String
    Dim RetVal As Variant
    
        AppName = "NotePad"
        AppFile = "Notepad.exe"
        
        On Error GoTo NotRunning
        AppActivate (AppName)
        Exit Sub
        
    NotRunning:
        RetVal = Shell(AppFile & " " & NameFile, 1)
    End Sub
    ~

    Bill

  6. #6
    Join Date
    Aug 2003
    Location
    Cleveland USA
    Posts
    184

    Angry Thanks, now here's my new problem....

    I changed the field type to hyperlink and changed my code to create hyperlinks instead of text file names, and this part works. So, that brings me to another problem I just realized.

    In my popup file dialog box, it only shows .doc and .xls filetypes (Microsoft extensions, I'm presuming). It doesn't pick up .pdf, .txt, .rtf, or anything else. How do I get ALL files in my folder to show up? I tried to use ".FileType = msoFileTypeAllFiles" that I found in Help, but it doesn't recognize 'msoFileTypeAllFiles'. This could be because I'm creating an Access 2000 database with Access 2003 software.

    Here is my code:


    Private Sub GetFiles()
    Dim i As Integer

    Dim fs As Object

    Set fs = Application.FileSearch

    With fs 'Check the \Letters folder to get a listing of available files
    .LookIn = CurrentProject.Path & "\Letters"

    If .Execute(, , AlwaysAccurate:=True) > 0 Then
    For i = 1 To .FoundFiles.Count
    Me.lstFileNames.AddItem (.FoundFiles(i))
    Next i
    Else
    MsgBox "no files available"

    End If
    End With

    End Sub

  7. #7
    Join Date
    Sep 2004
    Posts
    161
    msoFileTypeAllFiles is a constant available only if you have a reference to Microsoft Office in your project (in A97) but you can try the value of msoFileTypeAllFiles
    .FileType = 1

  8. #8
    Join Date
    Aug 2003
    Location
    Cleveland USA
    Posts
    184

    Angry Now, onto the next problem!

    I found the answer I needed to get all the file types to show up in the list. DCKunkle responded to another post on a similar problem in the same code. I've posted it below in case anyone is interested.

    Now, I get a list of the files, which fill into hyperlink textboxes in a form. However, it cannot open non-Microsoft files- no .jpg, .pdf, etc. Grrrrrr.....


    Private Sub GetFiles()
    Dim strFilename As String
    Dim intFiles As Integer
    Dim i As Integer

    Dim fs As Object

    With fs 'Check the \Letters folder to get a listing of available files
    strFilename = Dir(CurrentProject.Path & "\Letters\*.*")

    intFiles = 0

    While strFilename <> ""

    Me.lstFileNames.AddItem strFilename
    intFiles = intFiles + 1
    strFilename = Dir()
    Wend
    End With

    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •