image on form

B

Bill H.

I have an image control on a form, and have deleted the control's picture
property, using VBA to insert the desired path to the photo.

Problem is, when I put the path into an unbound control and click on a
"copy" button, which updates the image's picture control to the desired
path, the photo does not appear on the form. Unless I move the record
pointer. Using refresh doesn't work, and using requiry send me back to
record 1.

How do I get the photo to appear?

Thx.
 
P

Paul E. Schoen

Bill H. said:
I have an image control on a form, and have deleted the control's picture
property, using VBA to insert the desired path to the photo.

Problem is, when I put the path into an unbound control and click on a
"copy" button, which updates the image's picture control to the desired
path, the photo does not appear on the form. Unless I move the record
pointer. Using refresh doesn't work, and using requiry send me back to
record 1.

How do I get the photo to appear?

Thx.

I'm still learning, but I think I have been able to do essentially what you
are trying, although I am using a bound control. You might have a problem
with the path:

Private Sub tbImage_Exit(Cancel As Integer)
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 Sub
tbImage_Error:
Form_HorseInfo.imgHorse.Picture = (None)
End Sub

I enter the file name of the image in the text box (tbImage) that is bound
to the table text field "Image". When I exit the text box control, this sub
fires and the image appears. I also have the following:

Private Sub Form_Current()
sPath = CurrentProject.Path + "\"
Rem ... Removed other code shown in my other post
On Error GoTo tbImage_Error
If tbImage <> "" Then
Form_HorseInfo.imgHorse.Picture = sPath + tbImage
Else
Form_HorseInfo.imgHorse.Picture = (None)
End If
Exit Sub
tbImage_Error:
Form_HorseInfo.imgHorse.Picture = (None)
End Sub

So when I navigate to each record, the appropriate image appears. I plan to
have all the images stored in the same folder as the executable, so the
sPath makes it less cumbersome than storing the full pathname in the field.

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?

TIA,

Paul
 
A

Albert D. Kallal

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?

You can use:

Dim f As FileDialog
Set f = Application.FileDialog(msoFileDialogFilePicker)
f.Show
MsgBox "file choosen was " & f.SelectedItems(1)

Keep in mind that if the above does not compile, then you need a refernce to
the

above needs: Microsoft Object 12.0 Object library

If you want, you can use "late" binding, and go:


Dim f As Object
Set f = Application.FileDialog(3)
f.AllowMultiSelect = True
f.Show

MsgBox "file choosen = " & f.SelectedItems.Count

While we want to advoid adding "extra" references in our applciaons, adding
the above libary is a safe bet....

And, if you only wanted "image" files, then go:

Dim f As FileDialog
Set f = Application.FileDialog(msoFileDialogFilePicker)
f.Filters.Add "Images", "*.gif; *.jpg; *.jpeg"
f.Show
MsgBox "file choose was " & f.SelectedItems(1)

To get help on the above, whack ctrl-g to get to the debug window. In the
debug window, type in

FileDialog

and the hit the f1 key....
 
P

Paul E. Schoen

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
 

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