Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help improving my very basic file selector code 2

Status
Not open for further replies.

Moss100

Technical User
Aug 10, 2004
579
0
16
GB
Hello -

I have a form which has a field - Me.txt_Email_Attachment_1 and a button btn_attachment_add_1

I want to open the file picker, browse to a file and insert the file name and path into the textbox.

The code I have is basically working, but I would like to improve this with error checking etc. I am trying to learn code and so am hoping that someone could help me improve the sub.

For instance if I select a file then code runs fine, but if I cancel the file picker dialog box, the code errors.

I would greatly appreciate any help

Thank you Mark

CODE:

Private Sub btn_attachment_add_1_DblClick(Cancel As Integer)

Dim dialog As FileDialog
Set dialog = Application.FileDialog(msoFileDialogFilePicker)

With dialog
.AllowMultiSelect = False
.Show
Me.txt_Email_Attachment_1 = .SelectedItems.Item(1)

End With
End Sub
 
A simple way:
Code:
With dialog
  .AllowMultiSelect = False
  .Show
  If .SelectedItems.Count > 0 Then
    Me.txt_Email_Attachment_1 = .SelectedItems.Item(1)
  Else
    MsgBox "NO file selected"
  End If
End With

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
A few suggestions:
[ul]
[li]Use TGML Code tags so your code is easier to read [/li]
[li]Star posts that have resolved your past questions [/li]
[li]Check the Show property[/li]
[/ul]

Note the use of the code tags, indentation, comments color
Code:
Private Sub btn_attachment_add_1_DblClick(Cancel As Integer)
  Dim dialog As FileDialog
  Set dialog = Application.FileDialog(msoFileDialogFilePicker)
 
  With dialog
    .AllowMultiSelect = False
    .Show
    If .Show = True [COLOR=#4E9A06]'check for a file name[/color]
        Me.txt_Email_Attachment_1 = .SelectedItems.Item(1)
     Else [COLOR=#4E9A06]'if nothing selected notify the user[/color]
        MsgBox "Please select a file"
    End If
  End With
End Sub

Duane
Hook'D on Access
MS Access MVP
 
Thank you both very much. The second block of code did not work. I 'think' it needed a 'Then' inserting, but after I did that, the file selector would run twice.

Much appreciated anyhow - thank you both.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top