Open a file using code

dbprogman

Registered User.
Local time
Today, 22:53
Joined
Mar 29, 2005
Messages
35
How do I open a file within access and if I did how would I construct the code to open a file that part of its name can change. I don't want the user to have to interact using the file open dialogbox.
Thanx
 
What do you mean by "Open"? Do you want access to open connectivity to the file so you can read/write to it or do you want it to open up as if the user had double-clicked it?

Tom
 
dbprogman said:
How do I open a file within access and if I did how would I construct the code to open a file that part of its name can change. I don't want the user to have to interact using the file open dialogbox.
Thanx
How can you program it to ''guess'' what the file name would be since you state it can change? Why not let the user "browse" for the file since all Windows based software programs have a browsing function to allow the user to find their files?

Here is a working example... Browse [Find a directory or file]
 
Code:
Public Const SW_HIDE = 0
Public Const SW_MINIMIZE = 6
Public Const SW_RESTORE = 9
Public Const SW_SHOW = 5
Public Const SW_SHOWMAXIMIZED = 3
Public Const SW_SHOWMINIMIZED = 2
Public Const SW_SHOWMINNOACTIVE = 7
Public Const SW_SHOWNA = 8
Public Const SW_SHOWNOACTIVATE = 4
Public Const SW_SHOWNORMAL = 1

Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
      (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
       ByVal lpParameters As String, ByVal lpDirectory As String, _
       ByVal nShowCmd As Long) As Long

[COLOR=Green]'sAction can be either "Open" or "Print"[/COLOR]
Public Sub ExecuteFile(sFileName As String, sAction As String)
    Dim vReturn As Long
    
    If ShellExecute(Access.hWndAccessApp, sAction, _
                sFileName, vbNullString, "", SW_SHOWNORMAL) < 33 Then
        DoCmd.Beep
        MsgBox "File not found."
    End If
End Sub
 
Let me expand

I am actually importing a file and wish to have this automated so as users do not have the ability to interact. The file that's being imported though having some part of its name remain constant one part will change regulary. I need to now how to construct the code so as it will pick the correct file based on the part that doesn't change. Wheww!!!

File being imported is an XML file
I am using Access 2003

I attempted to use a wildcard in the construct of the code but it refused to import the file basically it couldn't find it.
e.g getobject "c:\xml files\friday22mar2005.xml
getobject " c:\xml files\friday*.xml

Any help is good help and thank you
 
Do a search on FileSystemObject or Scripting.FileSystemObject it will show you how to search for a file... by that you can get the file name... or even search (loop through) a directory and look at all the file names in that folder.

Use string comparisons to determine when you've found the file... then use that file name with your import code.
 
Last edited:
This sub will import all text files @ C:\Import TXT files\ and
move them to the C:\Archived TXT Files\ folder. This example imports
delimited text files with the import specification named TextImportSpecs
and the imported files do not have field names. You can easily modify it
to do what you want with your xml files.

Code:
Private Sub bImportFiles_Click()
On Error GoTo bImportFiles_Click_Err
 
    Dim objFS As Object, objFolder As Object
    Dim objFiles As Object, objF1 As Object
    Dim strFolderPath As String
 
    strFolderPath = "C:\Import TXT files\"
    Set objFS = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFS.GetFolder(strFolderPath)
    Set objFiles = objFolder.files
 
    For Each objF1 In objFiles
        If Right(objF1.Name, 3) = "txt" Then
            DoCmd.TransferText acImportDelim, "TextImportSpecs", "tblImportedFiles", strFolderPath & objF1.Name, False
            Name strFolderPath & objF1.Name As "C:\Archived TXT Files\" & objF1.Name 'Move the files to the archive folder
        End If
    Next
 
    Set objF1 = Nothing
    Set objFiles = Nothing
    Set objFolder = Nothing
    Set objFS = Nothing
 
bImportFiles_Click_Exit:
    Exit Sub
 
bImportFiles_Click_Err:
    MsgBox Err.Number & " - " & Err.Description
    Resume bImportFiles_Click_Exit
 
End Sub
 
Last edited:
Extreme thanx

Thanks to all who replied it was very much appreciated.

Thanx
dbprogman :)
 

Users who are viewing this thread

Back
Top Bottom