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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding Photos to Forms

Status
Not open for further replies.

djmurphy58

Technical User
Feb 28, 2003
73
0
0
US
I am very much a novice with Visual Basic - in fact I know next to nothing about it.
Here's what I want to do. I have a form to which I want the user to be able to click a button and have a file dialog box open up. I'd like the user to select one or more photos to put on the form. I want the photos to be automatically sized to fit inside a pre-sized image fram on the form. I want the path of the photos stored in my table, and I want the photos to show on both the forms and the report that I created.
I know I need to create a command button and then write some sort of VB code. Could someone guide me through this.

Thank you
 
Have a look at the Employees form in the Northwind sample database.
 
ok, I referenced the Northwind Database and used its VBS code.

I keep getting the error:
"Compile Error - Variable not defined"
errormsg.Variable is highlighted under Private Sub Form Current.

Please advise
 
errormsg" is the name of the label on the form with a caption "Click Add/Change to add picture". You should add such a label to your form.
 
ok - nevermind. I got the erromsg figured out. I didn't have a label created called errormsg created in my form.

But now I get the error,
"runtime error 2165. You can't hide a control that has the focus"

Please advise
 
Move the focus to some other control.
 
Ok, I think I got it now.

A couple other questions:
1. How do I have the photo automatically sized to fit the size of my image frame
2. When the file open dialog box pops up and the user is prompted to select a photo, how chan I change the file that opens. I want a certain folder to open every time - that's the file where all the photos are stored on my computer.
 
Choose Zoom for the size mode in image properties.
Changing
.InitialFileName = CurrentProject.path
To the default path should work.
 
ok, so I've been successful with everything you've told me so far.

No, how do I get the photo to print on my report. I'm assuming I need to add either an image, and unbound object frame, or a bound object frame. I've tried all 3 but can't get the photo to show up. I'm assuming I need to fuss around with the properties???

Thanks
 
You need more or less the same thing as the form, that is an image control, with some code in the Detail Format event:

Code:
If Dir(Me.txtImagePath)<>"" Then 
   Me.imgImage.Picture = Me.txtImagePath
   Me.imgImage.Visible=True
Else
   Me.imgImage.Visible=False
End If

Or there abouts.

 
How are ya djmurphy58 . . .

Have a look at the following:

How to Display Images in a Form or in a Report Without Storing the Images in a Table

Handle/Display images in forms/database

[blue]Your Thoughts? . . .[/blue]

Calvin.gif
See Ya! . . . . . .
 
stupid question...

where is the detail format event found? Am I adding a "bound object frame
 
You are adding an Image control. The detail format event can be found by double-clicking the Detail grey bar in a report to open the Property sheet. The format event will be found on the Events tab.
 
OK. I did that. but I got a compile error:

"Method or data member not found"

.txtImagePath is highlighted
 
Here's a portion of the code I have for my form. The photo works on the form - so I know this code must be correct.

Private Sub Form_Current()
' Display the picture for the current employee record if the image
' exists. If the file name no longer exists or the file name was blank
' for the current employee, set the errormsg label caption to the
' appropriate message.
Dim res As Boolean
Dim fName As String

path = CurrentProject.path
On Error Resume Next
errormsg.Visible = False
If Not IsNull(Me!Photo) Then
res = IsRelative(Me!Photo)
fName = Me![ImagePath]
If (res = True) Then
fName = path & "\" & fName
End If

Me![ImageFrame].Picture = fName
showImageFrame
Me.PaintPalette = Me![ImageFrame].ObjectPalette
If (Me![ImageFrame].Picture <> fName) Then
hideImageFrame
errormsg.Caption = "Picture not found"
errormsg.Visible = True
End If
Else
hideImageFrame
errormsg.Caption = "Click Add/Change to add picture"
errormsg.Visible = True
End If

End Sub
 
I should have mention that these were imaginary names. Change txtImagePath to whatever is the name of the control with the image path.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top