Paul E. Schoen said:
BUT, now I want to be able to use an Open Dialog to select the image, to
make it easier for my customer. I don't know how to add that sort of
control. It is used when you select an image file in the properties
dialog, but I want it in the user interface. Ideas?
I was able to do this, although it wasn't easy. You must click on VB
Tools|References so you can use the FileDialog:
======================================================================
Private Sub cmdFileDialog_Click()
'Requires reference to Microsoft Office 12.0 Object Library.
Dim fDialog As Office.FileDialog
Dim varFile As Variant
'Set up the File Dialog.
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
'Do Not Allow user to make multiple selections in dialog box.
.AllowMultiSelect = False
'Set the title of the dialog box.
.Title = "Please select an image file"
'Clear out the current filters, and add our own.
.Filters.Clear
.Filters.Add "Image Files", "*.JPG; *.GIF"
'Show the dialog box. If the .Show method returns True, the
'user picked at least one file. If the .Show method returns
'False, the user clicked Cancel.
If .Show = True Then
'Loop through each file selected and add it to the list box.
For Each varFile In .SelectedItems
Me.tbImage = ExtractFileName(varFile)
Next
UpdateImage (Me.tbImage)
Else
MsgBox "You clicked Cancel in the file dialog box."
End If
End With
End Sub
======================================================================
I could nor believe that I was unable find a simple native function to
extract the file name from a complete filespec. Thanks to Dogpile and
http://www.johntopley.com/oldblog/kb/vb/0005.html
Private Function ExtractFileName(ByVal vStrFullPath As String) As String
Dim intPos As Integer
intPos = InStrRev(vStrFullPath, "\")
ExtractFileName = Mid$(vStrFullPath, intPos + 1)
End Function
======================================================================
I made a separate function that I can call in some of the others that use
the same code:
Private Function UpdateImage(ByVal FileName As String)
sPath = CurrentProject.Path + "\"
On Error GoTo tbImage_Error
If tbImage <> "" Then
Form_HorseInfo.imgHorse.Picture = sPath + tbImage
Else
Form_HorseInfo.imgHorse.Picture = (None)
End If
Exit Function
tbImage_Error:
Form_HorseInfo.imgHorse.Picture = (None)
End Function
======================================================================
HTH. I'm gonna get a beer and go to bed with my dog...
Paul