Results 1 to 3 of 3

Thread: Photos on Form

  1. #1
    Join Date
    Dec 2003

    Question Unanswered: Photos on Form

    I know this question has probably been asked a dozen or more times, however, I have searched the forum and am just not "getting it". I have also used the help on Microsoft Access and still having problems.


    I have a youth database. This database has over 1500 records for various youth that have been in our program.

    I would like to figure out how to show a picture of the youth (the picture being stored in a different file - not in the database) when the youth's record is shown on a form.

    Any suggestions. Please "kindergarten" language preferred. I hate to waste your time, but a step by step interpretation would be wonderful. I've already wasted hours otherwise trying to figure things out. Thanks!


  2. #2
    Join Date
    Nov 2003
    Some suggestions:

    Add a field to the Child info table which will hold the Path and file name of the photo. Name it Photo. Make the field a Text type with a field length of 128

    Add a Image contol onto your form and name it ChildPhoto. Size it to the size you would like to have your pictures. Set the Size Mode property of the Image control to Zoom. Set the Picture Type property to Linked.

    Add a TextBox control to your same Form. Name it PhotName and set the Visible property to No (you can have it visible if you like). Set the Control Source property to the new Photo field you added to table.

    Now add a Command button control near or below your Image Control and name it BrowsePhotoButton. Set the Caption property to Get Photo. You will need this file browser dialog box to locate photos or picuture files.

    Copy and paste the code below into the VBE code window for the OnClick event of the BrowsePhotoButton Command button. It is a routine to display a file browse and select dialog box:

       ' Need 'Microsoft Office 10.0 Object Library' set in References
       ' for this to work.  Place the code into the OnClick event of a
       ' Command Button or place it into a Function procedure.
       On Error GoTo dlg_Error
       Dim dlg As Object
       Dim vrtSelectedItem As Variant
       ' 1 is the File Open Dialog       -  msoFileDialogOpen
       ' 2 is the File SaveAs Dialog     -  msoFileDialogSaveAs
       ' 3 is the File Browser (Picker)  -  msoFileDialogFilePicker
       ' 4 is the Folder Browser (Picker)-  msoFileDialogFolderPicker
        Set dlg = Application.FileDialog(msoFileDialogFilePicker)
        With dlg
          ' Set the File Dialog Window Title
          .Title = "Select Child Photo..."
          ' Clear any File Dialog Filters
          ' Add a New File Dialog Filters. Filters don't apply to a Folder Dialog
          .Filters.Add "Images", "*.bmp; *.gif; *.jpg; *.jpeg; *.wmf", 1
          .Filters.Add "All files", "*.*"
          ' Set the Name for the action button in our File Dialog
          .ButtonName = "Take Child"
          ' Turn OFF File multi Select. We don't need this now.
          .AllowMultiSelect = False
          ' Set the File Dialog window to show Thumbnamils
          .InitialView = msoFileDialogViewThumbnail
              ' All Available Views:
                    ' msoFileDialogViewDetails
                    ' msoFileDialogViewLargeIcons
                    ' msoFileDialogViewList
                    ' msoFileDialogViewPreview
                    ' msoFileDialogViewProperties
                    ' msoFileDialogViewSmallIcons
                    ' msoFileDialogViewThumbnail
                    ' msoFileDialogViewWebView
          ' Set folder in which the File Dialog will start from.
          .InitialFileName = CurDir
          ' If the Action button is selected and a file has indeed been
          ' selected then place the path & file name into the PhotoPath
          ' field on Form and set the Image Control (StudentPicture) to
          ' display it.
          If .Show = -1 And Not IsNull(.SelectedItems) Then
             For Each vrtSelectedItem In .SelectedItems
               Me.PhotName = vrtSelectedItem
               Me!ChildPhoto.Picture = Me.PhotName
          End If
        End With
        Set dlg = Nothing   ' Free memory
        Exit Sub
        MsgBox Error
        Resume Exit_dlg_Error the OnCurrent event of your Form, copy and paste this code:

        On Error GoTo err_Form_Current
        If Not Me.PhotName = "" Or Not IsNull(Me.PhotName) Then
            Me!ChildPhoto.Picture = Me.PhotName
            Me!ChildPhoto.Picture = ""
        End If
        Exit Sub
        MsgBox err.Description
        Resume exit_Form_Current
    Save your db. Run your db. For each child, select the BrowsePhotoButton Command button on your form. Locate and select the Picture file pertaining to each child in your database.

    Hope this helps...

    Self Taught In ALL Environments.....And It Shows!

  3. #3
    Join Date
    Apr 2006
    If you have the 2003 version of MS Access, the sample Northwind database does exactly what you want.

    To break it down, you need to add a new field/column (data type: string;denoted as ImagePath in the code)to indicate the path where the image is located.

    Place a command button in your form and place getfilename in its click event

    Private Sub NewCmdButton_Click()
    End Sub
    and add the code of the procedure that it is triggering

    Sub getfilename()
    Dim filename As String
    Dim result As Integer
    With Application.FileDialog(msoFileDialogFilePicker)
        .Title = "Select Image File"
        .Filters.Add "All Files", "*.*"
        .Filters.Add "bitmaps", "*.bmp"
        .Filters.Add "jpeg file", "*.jpg"
        .FilterIndex = 3
        .AllowMultiSelect = False
        .InitialFileName = currentproject.path
        result = .Show
        If (result <> 0) Then
            filename = Trim(.SelectedItems.Item(1))
            filename = EXTRACTFILEN(filename)
            Me![dwgpath].Locked = False
            Me![ImagePath].Visible = True
            Me![ImagePath].Text = filename
            End If
        End With
    End Sub
    .. this allows you to select a file through Application.FileDialog(msoFileDialogFilePicker)

    Add an Image to your form, (image : Imageholder
    I think you should initially select a picture and set the size and position, better yet create a bitmap with the text 'no picture available' so that and set it as your default value for the image path.

    then, just add this final piece of code to the form's on current event

    Private Sub Form_Current()
    If IsNull(Me![ImagePath] ) Then
        Me[Imageholder].Visible = False
        Me[Imageholder].Visible = True
        Me![Imageholder].Picture = Me![ImagePath]
    End Sub
    You may also use some string manipulation routines to set a default folder
    for the images, so you can just store actual filenames

    Of course if you do locate the sample database Northwind.mdb
    you have everything you need in there at the Employees Form in all of its unblemished glory, haha

    Well, it worked for one of my projects, that is why.
    Only quitters quit!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts