Here is an example using the code you downloaded. Create a command button on
your form. In the Properties Dialog for the button, select the Events tab.
Select Code builder, and paste this in. You will need to modify it to suit
your names. Also, this imports an Excel spreadsheet so you will have to make
some modifications to look for .csv files and use the TransferText instead.
Dim strDefaultDir As String
Dim strFileName As String
Dim strFilter As String
Dim lngFlags As Long
Dim strGetFileName As String
On Error GoTo cmdImport_Click_Error
'Set up the Default path and file
strDefaultDir = "\\rsltx1-bm01\busmgmt\Arthur\"
strFileName = "IQ Navigator.xls"
'Flags Hides the Read Only Check and Only allow existing files
lngFlags = ahtOFN_HIDEREADONLY Or ahtOFN_FILEMUSTEXIST
'Set filter to show only Excel spreadsheets
strFilter = ahtAddFilterItem(strFilter, "Excel Files (*.xls)", "*.xls")
'Call the Open File Dialog
Do While True
strGetFileName = ahtCommonFileOpenSave( _
OpenFile:=True, _
InitialDir:=strDefaultDir, _
Filter:=strFilter, _
FileName:=strFileName, _
Flags:=lngFlags, _
DialogTitle:="Import IQ Navigator")
Me.Repaint
If Len(strGetFileName) = 0 Then 'User Clicked CANCEL
If MsgBox("Cancel Import", vbQuestion + vbYesNo, _
"Import IQ Navigator") = vbYes Then
MsgBox "Import Canceled", , "Import IQ Navigator"
Exit Do
End If
Else
CurrentDb.Execute ("DELETE * FROM Received_File;"), dbFailOnError
DoCmd.TransferSpreadsheet acImport, , "Received_File",
strGetFileName, True
Call RemoveNullRecs
Me.txtTotalHours = DSum("[total hours]", "received_file", _
"[pc code] like '*D4056*'")
MsgBox "Import Complete", , "Import IQ Navigator"
Exit Do
End If
Loop
cmdImport_Click_Exit:
On Error Resume Next
Exit Sub
cmdImport_Click_Error:
MsgBox "Error " & Err.Number & " (" & Err.DESCRIPTION & _
") in procedure cmdImport_Click of VBA Document
Form_frmImportIQNavigator"
GoTo cmdImport_Click_Exit
Andy said:
I want to create a button on a form that will prompt the user to select a
.csv file which will then be imported using an import specification and then
queries run against the imported file. I'm fine with the queries etc, it's
just the import one-click process.
I've managed to get a dialog box to run using
http://www.mvps.org/access/api/api0001.htm, but when I select the file to
import, nothing happens.
I think I need to build this process into a macro so the import spec can be
defined and the additional queries defined.
Am running Access 2003 and ashamed to say my VBA knowledge/skills is
virtually non-existent.
Thanks