How to generate in VBA a "File, Open window" (1 Viewer)

MarcelMegens

Registered User.
Local time
Today, 10:10
Joined
Jun 8, 2004
Messages
16
I have created an VBA from a macro See below:

The name of the text-files varies from time to time, the rest of the process doesnt. Can someone point me in the right direction (or send an example) how I could replace the hardcoded filename"C:\Documents\CHK.csv" with the code that would open an explorer window, where I can simply select the file, and click on open or import, after which this file is import and the remaining queries can run?

(With the "explorer-window" I mean a window similar to what you get if you want to open a word or excel file, via File, Open....)

'------------------------------------------------------------
' MCR_ImportCHK_from_c_Documents
'
'------------------------------------------------------------
Function MCR_Import_from_c_Documents()
On Error GoTo MCR_Import_from_c_Documents_Err

DoCmd.SetWarnings False
DoCmd.CopyObject "", "CHK", acTable, "baseCHK"
DoCmd.TransferText acImportDelim, "CHK", "CHK", "C:\Documents\CHK.csv", False, ""
DoCmd.OpenQuery "Qry_Convert2", acViewNormal, acEdit
DoCmd.OpenQuery "Qry_Convert3", acViewNormal, acEdit
DoCmd.OpenQuery "Qry_ConvertDeleteXXXlink", acViewNormal, acEdit


MCR_Import_from_c_Documents_Exit:
Exit Function

MCR_Import_from_c_Documents_Err:
MsgBox Error$
Resume MCR_Import_from_c_Documents_Exit

End Function
 

c_smithwick

Underpaid Programmer
Local time
Today, 01:10
Joined
Jan 8, 2010
Messages
102
Enter the following function:

Code:
Function GetFile(Optional OpenAt As String) As String
    Dim lCount As Long
     
     
    With Application.FileDialog(msoFileDialogOpen)
        .Show
        For lCount = 1 To .SelectedItems.COUNT
            GetFile = .SelectedItems(lCount)
        Next lCount
    End With
End Function

Then change your Function to:

Code:
Function MCR_Import_from_c_Documents()
Dim strFile as string

On Error GoTo MCR_Import_from_c_Documents_Err

strFile = GetFile
DoCmd.SetWarnings False
DoCmd.CopyObject "", "CHK", acTable, "baseCHK"
DoCmd.TransferText acImportDelim, "CHK", "CHK", strFile, False, ""
DoCmd.OpenQuery "Qry_Convert2", acViewNormal, acEdit
DoCmd.OpenQuery "Qry_Convert3", acViewNormal, acEdit
DoCmd.OpenQuery "Qry_ConvertDeleteXXXlink", acViewNormal, acEdit


MCR_Import_from_c_Documents_Exit:
Exit Function

MCR_Import_from_c_Documents_Err:
MsgBox Error$
Resume MCR_Import_from_c_Documents_Exit

End Function
 

dcb

Normally Lost
Local time
Today, 10:10
Joined
Sep 15, 2009
Messages
529
Note the c_smithwick method requires ref to "Microsoft Office xx.x Object Library"
 

Users who are viewing this thread

Top Bottom