Need to browse for an image file from a form

G

Guest

I am a newbie trying to replicate the 'browse for image' function explained
in the post titled: "OLE Object- the real question."After following the
instructions from the above post, I get an error when I click on the image
frame (to select an image to put in the form). I get the error message:
"Compile Error: User-defined type not defined." The Visual Basic window
opens and "Dim dlgFilePick As FileDialog" is highlit as the trouble spot.
When I go from design to form view I get a run time error and then hitting
the debug button highlights the "Me.Image30.Picture = Me!
filePath" code. I would appreciate someone letting me know what I am doing
wrong.

Here is what I am doing:
-Using Access 2003

1.) I have created a field in my table called 'FilePath'


2.) In the 'On Current' event of the form, I have pasted the following code

into the code builder window ('Image30' is the name of the image frame):

*********code start************
Private Sub Form_Current()
Me.Image30.Picture = Me!filePath
End Sub
*********code end************


3.) In the 'On Click' event for the unbound image frame (in the code builder)

I pasted the following:

*********code start************
Private Sub Image30_Click()

Dim strFilename As String, strFind As String
Dim lngID As Long

strFilename = GetTheFilename("Browse to the file")
If strFilename = "Cancelled" Then
Exit Sub
End If
Me!filePath = strFilename
lngID = Me!ItemID
strFind = "[ItemID]=" & lngID
Echo False
Me.Requery
Me.Recordset.FindFirst strFind
Echo True
End Sub
*********code end************


4.) This code was pasted in Module2(code) window:

*********code start************
Function GetTheFilename(strTitle As String) As String
Dim strFilename As String
Dim dlgFilePick As FileDialog
Dim vrtSelectedItem As Variant

On Error GoTo Err_GetFilename
Set dlgFilePick = Application.FileDialog(msoFileDialogFilePicker)
With dlgFilePick
.title = strTitle
'Let user select only one file
.AllowMultiSelect = False
If .Show = -1 Then
'The user pressed the action button.
GetTheFilename = .SelectedItems(1)

Else
'The user pressed Cancel.
GetTheFilename = "Cancelled"
End If
End With

Exit_GetFilename:
Set dlgFilePick = Nothing
Exit Function
Err_GetFilename:
MsgBox "Error " & Err.Number & ": " & Err.Description, ,
"GetTheFilename"
Resume Exit_GetFilename
End Function
*********code end************
 
D

Douglas J. Steele

Does Me!filePath have a value when the error's raised?

If not, try something like:

Private Sub Form_Current()
If Len(Me!filePath & "") > 0 Then
Me.Image30.Picture = Me!filePath
End If
End Sub

You could even get more sophisticated, and supply a "no picture available"
picture in the Else side of the If statement.


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)



PaulFort said:
I am a newbie trying to replicate the 'browse for image' function explained
in the post titled: "OLE Object- the real question."After following the
instructions from the above post, I get an error when I click on the image
frame (to select an image to put in the form). I get the error message:
"Compile Error: User-defined type not defined." The Visual Basic window
opens and "Dim dlgFilePick As FileDialog" is highlit as the trouble spot.
When I go from design to form view I get a run time error and then
hitting
the debug button highlights the "Me.Image30.Picture = Me!
filePath" code. I would appreciate someone letting me know what I am doing
wrong.

Here is what I am doing:
-Using Access 2003

1.) I have created a field in my table called 'FilePath'


2.) In the 'On Current' event of the form, I have pasted the following
code

into the code builder window ('Image30' is the name of the image frame):

*********code start************
Private Sub Form_Current()
Me.Image30.Picture = Me!filePath
End Sub
*********code end************


3.) In the 'On Click' event for the unbound image frame (in the code
builder)

I pasted the following:

*********code start************
Private Sub Image30_Click()

Dim strFilename As String, strFind As String
Dim lngID As Long

strFilename = GetTheFilename("Browse to the file")
If strFilename = "Cancelled" Then
Exit Sub
End If
Me!filePath = strFilename
lngID = Me!ItemID
strFind = "[ItemID]=" & lngID
Echo False
Me.Requery
Me.Recordset.FindFirst strFind
Echo True
End Sub
*********code end************


4.) This code was pasted in Module2(code) window:

*********code start************
Function GetTheFilename(strTitle As String) As String
Dim strFilename As String
Dim dlgFilePick As FileDialog
Dim vrtSelectedItem As Variant

On Error GoTo Err_GetFilename
Set dlgFilePick = Application.FileDialog(msoFileDialogFilePicker)
With dlgFilePick
.title = strTitle
'Let user select only one file
.AllowMultiSelect = False
If .Show = -1 Then
'The user pressed the action button.
GetTheFilename = .SelectedItems(1)

Else
'The user pressed Cancel.
GetTheFilename = "Cancelled"
End If
End With

Exit_GetFilename:
Set dlgFilePick = Nothing
Exit Function
Err_GetFilename:
MsgBox "Error " & Err.Number & ": " & Err.Description, ,
"GetTheFilename"
Resume Exit_GetFilename
End Function
*********code end************
 

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