Adding pic in form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Ok on my form i have a field for a picture, my data table is all set up to
add the picture and what i would like is to be able to click on the field
where the picture will go and have the file pathway window show up. I think i
have to build an event with a mouse click but im not sure what code to input.

Also i cant figure out how to get the picuter to accutly show up in the box
i have created. for the field, in propterties and on the data tab i have
=[Pictures]![Photograph] the first 'pictures' is the data table with the
pictures in it. i have entered a few pics via the data table which has a
working hyperlink to the pic but what else do i need to do to make it show up
in the form.


Thanks!!!
 
Kyle:

I have done this, but it involves a few things. First, I use an image
control on my form that will display the picture. Second, I do NOT store the
images in my database, I store the images in some folder on my computer and
only store the path to the image file in my database. Third, I use API calls
to do the File/Search method from some button's click event. Fourth, once I
get the path to the image via the API call, I set the .ImagePath property of
my image control.

With that said, here is some code that I use in my button click even that
will do the Find for the file and display it in the control. If I missed
anything, you may have to search the Web for the API calls needed to do the
File Search method.

*** Define this TYPE structure and API calls in some global module ***

Type tagOPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
strFilter As String
strCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
strFile As String
nMaxFile As Long
strFileTitle As String
nMaxFileTitle As Long
strInitialDir As String
strTitle As String
Flags As Long
nFileOffset As Integer
nFileExtension As Integer
strDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Declare Function aht_apiGetOpenFileName Lib "comdlg32.dll" _
Alias "GetOpenFileNameA" (OFN As tagOPENFILENAME) As Boolean
Declare Function aht_apiGetSaveFileName Lib "comdlg32.dll" _
Alias "GetSaveFileNameA" (OFN As tagOPENFILENAME) As Boolean
Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long

**** Here is the click event of the button *********
Private Sub btnFindImage_Click()
Dim sImagePath As String

sImagePath = GetOpenFile(sCurrentDBPath & "images", "Select an Image File",
"*.jpg")
If Len(sImagePath) > 0 Then Me.ImagePath = sImagePath

End Sub

Public Function ahtAddFilterItem(sFilter As String, sDescription As String,
Optional varItem As Variant) As String

If IsMissing(varItem) Then varItem = "*.*"
ahtAddFilterItem = sFilter & sDescription & vbNullChar & varItem & vbNullChar

End Function


Public Function GetOpenFile(Optional varDirectory As Variant, _
Optional varTitleForDialog As Variant, _
Optional varSelectFile As Variant) As Variant

Dim sFilter As String, sFile As String, sTypes As String
Dim lFlags As Long
Dim varFileName As Variant

' Specify that the chosen file must already exist
lFlags = ahtOFN_FILEMUSTEXIST Or ahtOFN_HIDEREADONLY Or ahtOFN_NOCHANGEDIR

If IsMissing(varDirectory) Then varDirectory = ""
If IsMissing(varTitleForDialog) Then varTitleForDialog = ""
If IsMissing(varSelectFile) Then varSelectFile = ""


' Define the filter string for the File/Open dialog box.
If IsMissing(varSelectFile) Then
sFilter = ahtAddFilterItem(sFilter, "All Files (*.*)", "*.*")
Else
' Check to see what files to limit the filter to based on what was
passed in.
If InStr(varSelectFile, ".mdb") Then
sFilter = ahtAddFilterItem(sFilter, "Microsoft Access Files
(*.mdb)", "*.mdb")
ElseIf InStr(varSelectFile, ".xls") Then
sFilter = ahtAddFilterItem(sFilter, "Microsoft Excel Files (*.xls)",
"*.xls")
ElseIf InStr(varSelectFile, ".jpg") Then
sFilter = ahtAddFilterItem(sFilter, "JPEG Files (*.jpg)", "*.jpg")
Else
sFilter = ahtAddFilterItem(sFilter, "All Files (*.*)", "*.*")
End If
End If

' Get the file name using the ahtCommonFileOpenSave API
varFileName = ahtCommonFileOpenSave( _
OpenFile:=True, _
InitialDir:=varDirectory, _
FileName:=varSelectFile, _
Filter:=sFilter, _
Flags:=lFlags, _
DialogTitle:=varTitleForDialog)

If Not IsNull(varFileName) Then varFileName = TrimNull(varFileName)

GetOpenFile = varFileName

End Function


Public Function ahtAddFilterItem(sFilter As String, sDescription As String,
Optional varItem As Variant) As String

If IsMissing(varItem) Then varItem = "*.*"
ahtAddFilterItem = sFilter & sDescription & vbNullChar & varItem & vbNullChar

End Function

Function ahtCommonFileOpenSave( _
Optional ByRef Flags As Variant, _
Optional ByVal InitialDir As Variant, _
Optional ByVal Filter As Variant, _
Optional ByVal FilterIndex As Variant, _
Optional ByVal DefaultExt As Variant, _
Optional ByVal FileName As Variant, _
Optional ByVal DialogTitle As Variant, _
Optional ByVal hwnd As Variant, _
Optional ByVal OpenFile As Variant) As Variant
' This is the entry point you'll use to call the common
' file open/save dialog. The parameters are listed
' below, and all are optional.
'
' In:
' Flags: one or more of the ahtOFN_* constants, OR'd together.
' InitialDir: the directory in which to first look
' Filter: a set of file filters, set up by calling
' AddFilterItem. See examples.
' FilterIndex: 1-based integer indicating which filter
' set to use, by default (1 if unspecified)
' DefaultExt: Extension to use if the user doesn't enter one.
' Only useful on file saves.
' FileName: Default value for the file name text box.
' DialogTitle: Title for the dialog.
' hWnd: parent window handle
' OpenFile: Boolean(True=Open File/False=Save As)
' Out:
' Return Value: Either Null or the selected filename
Dim OFN As tagOPENFILENAME
Dim strFileName As String
Dim strFileTitle As String
Dim fResult As Boolean

' Give the dialog a caption title.
If IsMissing(InitialDir) Then InitialDir = CurDir
If IsMissing(Filter) Then Filter = ""
If IsMissing(FilterIndex) Then FilterIndex = 1
If IsMissing(Flags) Then Flags = 0&
If IsMissing(DefaultExt) Then DefaultExt = ""
If IsMissing(FileName) Then FileName = ""
If IsMissing(DialogTitle) Then DialogTitle = ""
If IsMissing(hwnd) Then hwnd = Application.hWndAccessApp
If IsMissing(OpenFile) Then OpenFile = True
' Allocate string space for the returned strings.
strFileName = Left(FileName & String(256, 0), 256)
strFileTitle = String(256, 0)
' Set up the data structure before you call the function
With OFN
.lStructSize = Len(OFN)
.hwndOwner = hwnd
.strFilter = Filter
.nFilterIndex = FilterIndex
.strFile = strFileName
.nMaxFile = Len(strFileName)
.strFileTitle = strFileTitle
.nMaxFileTitle = Len(strFileTitle)
.strTitle = DialogTitle
.Flags = Flags
.strDefExt = DefaultExt
.strInitialDir = InitialDir
' Didn't think most people would want to deal with these options.
.hInstance = 0
.strCustomFilter = ""
.nMaxCustFilter = 0
.lpfnHook = 0
.strCustomFilter = String(255, 0)
.nMaxCustFilter = 255
End With

' Pass the desired data structure to the Windows API to display the
Open/Save As Dialog.
If OpenFile Then
fResult = aht_apiGetOpenFileName(OFN)
Else
fResult = aht_apiGetSaveFileName(OFN)
End If

If fResult Then
If Not IsMissing(Flags) Then Flags = OFN.Flags
ahtCommonFileOpenSave = TrimNull(OFN.strFile)
Else
ahtCommonFileOpenSave = vbNullString
End If

End Function

kyle said:
Ok on my form i have a field for a picture, my data table is all set up to
add the picture and what i would like is to be able to click on the field
where the picture will go and have the file pathway window show up. I think i
have to build an event with a mouse click but im not sure what code to input.

Also i cant figure out how to get the picuter to accutly show up in the box
i have created. for the field, in propterties and on the data tab i have
=[Pictures]![Photograph] the first 'pictures' is the data table with the
pictures in it. i have entered a few pics via the data table which has a
working hyperlink to the pic but what else do i need to do to make it show up
in the form.


Thanks!!!
 

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

Back
Top