Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2006
    Posts
    2

    Unanswered: Adding Relative Image Paths

    I am trying to add a relative image path (i.e. just the filename and extension) to a database field. Using the the following code that comes with the Northwind database I can only store the full image path. Can anyone help?

    Sub getFileName()
    Dim fileName As String
    Dim result As Integer
    With Application.FileDialog(msoFileDialogFilePicker)
    .Title = "Select Employee Picture"
    .Filters.Add "All Files", "*.*"
    .Filters.Add "JPEGs", "*.jpg"
    .Filters.Add "Bitmaps", "*.bmp"
    .FilterIndex = 1
    .AllowMultiSelect = False
    .InitialFileName = CurrentProject.path & "\Images_Contacts"
    result = .Show
    If (result <> 0) Then
    fileName = Trim(.SelectedItems.Item(1))
    Me![ImagePath].Visible = True
    Me![ImagePath].SetFocus
    Me![ImagePath].Text = fileName
    Me![FirstName].SetFocus
    Me![ImagePath].Visible = False
    End If
    End With
    End Sub

  2. #2
    Join Date
    Nov 2003
    Posts
    1,487
    Place this function into the same code module as your File Dialog code:

    Code:
    Private Function GetFileNameFromPath(PathStrg As String) As String
    Dim Strg as String
    Dim i As Integer
    If PathStrg = "" Then
    	GetFileNameFromPath=""
    	Exit Function
    End If
    For i = Len(PathStrg) To 1 Step -1
    If Mid$(PathStrg, i, 1) <> "\" Then
    	 Strg = Mid$(PathStrg, i, 1) & Strg
    Else
    	 Exit For
    End If
    Next i
    GetFileNameFromPath = Strg
    End Function
    To use this within your file dialog code, you might have it something like this:

    Code:
    Sub getFileName()
      Dim fileName As String
      Dim result As Integer
      With Application.FileDialog(msoFileDialogFilePicker)
    	.Title = "Select Employee Picture"
    	.Filters.Add "All Files", "*.*"
    	.Filters.Add "JPEGs", "*.jpg"
    	.Filters.Add "Bitmaps", "*.bmp"
    	.FilterIndex = 1
    	.AllowMultiSelect = False
    	 ' Set the File Dialog window to show a Image preview in dialog window. 
    	 .InitialView = msoFileDialogViewPreview 
    	.InitialFileName = Application.CurrentProject.Path & "\Images_Contacts"
    	result = .Show
    	If result <> 0 Then
    	 fileName = Trim(.SelectedItems.Item(1))
    	 Me.[ImagePath] = GetFileNameFromPath(fileName)
    	 ' Or you could forget about the above function and use:
    	' Me.[ImagePath] = Mid$(filename, Len(.InitialFileName)+1, Len(filename))
    	Me.[FirstName].SetFocus
       End If
      End With
    End Sub
    Realisticly, you would want to set your file dialog routine as a Function which would simply return the selected path and file name(s). This way you could use the same function anywhere, any time. Then update your Form Controls base on the returned value. Something like this (from the OnClick event of a Command Button named GetFile):

    Code:
    Private Sub GetFile_Click()
      Dim FileStrg As Strg
      FileStrg = GetFileNameFromPath(GetImageFile(Application.CurrentProject.Path & "\Images_Contacts"))
      ' Me.[ImagePath will hold the file name only (not the path to it).
      If FileStrg <> "" Then Me.[ImagePath] = FileStrg
      Me.[FirstName].SetFocus
    End Sub
    Your File Dialog Function then would look like this and should reside within a main database code module. The Function should be declared as Public:

    Code:
    Public Function GetImageFile(StartPath As String) As String
      Dim fileName As String
      Dim result As Integer
      GetImageFile = ""
      With Application.FileDialog(msoFileDialogFilePicker)
    	.Title = "Select Employee Picture"
    	.Filters.Add "All Files", "*.*"
    	.Filters.Add "JPEGs", "*.jpg"
    	.Filters.Add "Bitmaps", "*.bmp"
    	.FilterIndex = 1
    	.AllowMultiSelect = False
    	 ' Set the File Dialog window to show a Image preview in dialog window. 
    	 .InitialView = msoFileDialogViewPreview 
    	.InitialFileName = StartPath
    	result = .Show
    	If result <> 0 Then
    	 fileName = Trim(.SelectedItems.Item(1))
    	 GetImageFile = filename
    	End If
      End With
    End Function
    Note....there is no Error Checking within the supplied code above. This will be up to you to provide as you see fit.

    There is an Windows API function that does this as well.
    Hope this helps...

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


  3. #3
    Join Date
    Jan 2006
    Posts
    2
    Many thanks for that - it works great!

Posting Permissions

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