Browse folder dialogue, how to reference the cancel event

D

Dale

I'm using the following code activated on the "onclick" event of a command
button on a very simple form for users to browse to "folder-x" to import a
text file(s). Clicking on the button causes the "browse to a folder"
dialogue to pop up as expected but I do not know how to reference the cancel
event on this form generated from code. When activating cancel, whatever
text files that happen to be in the folder the dialogue opens in will be
imported.

Attached is the code for the browse dialogue and the onclick event of the
cmd button...thank you.


Option Compare Database
Option Explicit

Type shellBrowseInfo
hwndOwner As Long
pIDLRoot As Long
pszDisplayName As Long
lpszTitle As String
ulFlags As Long
lpfnCallback As Long
lParam As Long
iImage As Long
End Type

Const BIF_RETURNONLYFSDIRS = 1
Const MAX_PATH = 260

Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Declare Function SHBrowseForFolder Lib "shell32" (lpbi As shellBrowseInfo)
As Long
Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long,
ByVal lpBuffer As String) As Long

'Then use the following function, supplying it the title you want to use for
the dialog, and the handle of the calling form. (use the Me.hwnd property of
the form):

Public Function GetFolder(dlgTitle As String, Frmhwnd As Long) As String

Dim Nullchr As Integer
Dim IDList As Long
Dim Result As Long
Dim Folder As String
Dim BI As shellBrowseInfo

With BI
.hwndOwner = Frmhwnd
.lpszTitle = dlgTitle
.ulFlags = BIF_RETURNONLYFSDIRS
End With

IDList = SHBrowseForFolder(BI)
If IDList Then
Folder = String$(MAX_PATH, 0)
Result = SHGetPathFromIDList(IDList, Folder)
Call CoTaskMemFree(IDList) 'this frees the ole pointer to
IDlist
Nullchr = InStr(Folder, vbNullChar)
If Nullchr Then
Folder = Left$(Folder, Nullchr - 1)
End If
End If

GetFolder = Folder

End Function

Private Sub Command0_Click()
Dim InputDir, ImportFile As String, tblName As String

'InputDir = "D:\Performance Indicator\"

InputDir = GetFolder("Select folder to import from", Me.Hwnd)
If InputDir = "" Then
Exit Sub
Else
ImportFile = Dir(InputDir & "\*.txt")

Do While Len(ImportFile) > 0
tblName = "tblPerIndic_New_Data"

DoCmd.TransferText acImportDelim, "importperf", tblName, InputDir &
"\" & ImportFile
ImportFile = Dir
Loop
End If
End Sub
 
D

Douglas J. Steele

That's certainly different from what I'm seeing using that code.

Whenever I click on the Cancel button, the call returns a zero-length string
(""), which you're already trapping for. (BTW, it's slightly better to use
Len(InputDir) = 0 than InputDir = "")

AFAIK, no usable Cancel event is raised by the dialog.
 

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