Select and import multiple files

G

Guest

Friends,

I am code stupid and need help. Below is the code that I have that
correctly opens a dialog box and then import that one file that is selected
by a user.

I am getting 24 files each day and would like the user to be able to select
more than one file and for this code to import the selected files. What do I
need to do?

Private Sub cmdFileDialog_Click()
Dim strFilter As String
Dim strInputFileName As String
strFilter = ahtAddFilterItem(strFilter, "Tab Files (*.tab)", "*.tab")
strInputFileName = ahtCommonFileOpenSave( _
Filter:=strFilter, OpenFile:=True, _
DialogTitle:="Please select an input file...", _
Flags:=ahtOFN_HIDEREADONLY)
DoCmd.TransferText acImportDelim, "Alibris Import Specification",
"AlibrisImportTable", strInputFileName, True
End Sub
 
D

Douglas J. Steele

You can specify ahtOFN_ALLOWMULTISELECT as an additional flag:

Dim strFilter As String
Dim strInputFileName As String
strFilter = ahtAddFilterItem(strFilter, "Tab Files (*.tab)", "*.tab")
strInputFileName = ahtCommonFileOpenSave( _
Filter:=strFilter, OpenFile:=True, _
DialogTitle:="Please select an input file...", _
Flags:=ahtOFN_HIDEREADONLY Or ahtOFN_ALLOWMULTISELECT)

You'll also need to make a change in ahtCommonFileOpenSave. At the end of
the function, change

If fResult Then
' You might care to check the Flags member of the
' structure to get information about the chosen file.
' In this example, if you bothered to pass in a
' value for Flags, we'll fill it in with the outgoing
' Flags value.
If Not IsMissing(Flags) Then Flags = OFN.Flags
ahtCommonFileOpenSave = TrimNull(OFN.strFile)
Else
ahtCommonFileOpenSave = vbNullString
End If

to

If fResult Then
' You might care to check the Flags member of the
' structure to get information about the chosen file.
' In this example, if you bothered to pass in a
' value for Flags, we'll fill it in with the outgoing
' Flags value.
If Not IsMissing(Flags) Then Flags = OFN.Flags
If (Flags And ahtOFN_ALLOWMULTISELECT) = 0 Then
ahtCommonFileOpenSave = TrimNull(OFN.strFile)
Else
ahtCommonFileOpenSave = OFN.strFile
End If
Else
ahtCommonFileOpenSave = vbNullString
End If


Now, strInputFileName will come back as a rather complicated string
containing all of the selected files.

The first thing in strInputFileName will be the full path to the folder,
followed by a Null character (Chr$(0)). After that will be each of the files
selected, each separated by a Null character. There will be 2 Null
characters at the end of the string.

You can work with this quite easily using the Split function.

In your routine that calls ahtCommonFileOpenSave, add a few more
declarations:

Dim intLoop As Integer
Dim strFolder As String
Dim varFiles As Variant

After strInputFileName has been returned by the call to
ahtCommonFileOpenSave, add the following:

varFiles = Split(strInputFileName, Chr$(0))
If IsNull(varFiles) Then
' no files were returned
Else
strFolder = varFiles(0)
For intLoop = 1 to UBound(varFiles) - 2

' You can now refer to varFiles(intLoop) inside this loop to get each of
' the individual files selected.
' You'll actually want to use strFolder & varFiles(intLoop) to get the
' complete path to each file.

Next intLoop
End If
 
G

Guest

This may have been exactly what I needed, but as I noted in my first line of
the last note, I am code stupid........ Translated... I cannot take
programmer notations and vocabulary and create code.

Here is the best I could do with the previous help. Please help me with the
code as it should be in VB..

Private Sub Command4_Click()
On Error GoTo Err_Command4_Click

Dim stDocName As String

stDocName = "AlibrisConfirmQuery"
DoCmd.OpenQuery stDocName, acNormal, acEdit

Exit_Command4_Click:
Exit Sub

Err_Command4_Click:
MsgBox Err.Description
Resume Exit_Command4_Click

End Sub
Private Sub Command5_Click()
On Error GoTo Err_Command5_Click

Dim stDocName As String

stDocName = "AlibrisOutputMacro"
DoCmd.RunMacro stDocName

Exit_Command5_Click:
Exit Sub

Err_Command5_Click:
MsgBox Err.Description
Resume Exit_Command5_Click

End Sub

Private Sub Command6_Click()

' API: Call the standard Windows File Open/Save dialog box. This can be
done by either using
' the Common Dialog Control in Access 97 or by using the APIs defined for
this purpose.
' To call the actual dialog from your code, see the included function
TestIt() within the
' module or use the following example as a guideline and.

Dim strFilter As String
Dim strInputFileName As String
strFilter = ahtAddFilterItem(strFilter, "Tab Files (*.tab)", "*.tab")
strInputFileName = ahtCommonFileOpenSave( _
Filter:=strFilter, OpenFile:=True, _
DialogTitle:="Please select an input file...", _
Flags:=ahtOFN_HIDEREADONLY Or ahtOFN_ALLOWMULTISELECT)

Dim intLoop As Integer
Dim strFolder As String
Dim varFiles As Variant

If fResult Then
' You might care to check the Flags member of the structure to get
information about
' the chosen file. In this example, if you bothered to pass in a
value for Flags,
' we'll fill it in with the outgoing flags value.


If Not IsMissing(Flags) Then Flags = OFN.Flags
If (Flags And ahtOFN_ALLOWMULTISELECT) = 0 Then
ahtCommonFileOpenSave = TrimNull(OFN.strFile)
Else
ahtCommonFileOpenSave = OFN.strFile
End If
Else
ahtCommonFileOpenSave = vbNullString
End If
varFiles = Split(strInputFileName, Chr$(0))
If IsNull(varFiles) Then
' no files were returned
Else
strFolder = varFiles(0)
For intLoop = 1 To UBound(varFiles) - 2

' You can now refer to varFiles(intLoop) inside this loop to get each of
' the individual files selected.
' You'll actually want to use strFolder & varFiles(intLoop) to get the
' complete path to each file.

Next intLoop
End If

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top