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

Create a browse for image button for linked image

Status
Not open for further replies.

Triacona

Technical User
Jun 11, 2009
462
0
0
GB
Dear All,

Thanks for a great forum!

I have a challenging problem...
I have a form with a subform, the subform is used for images.
The image is linked and uses the PropertyName-Number field as the image name, the path is predefined.
I have the following code in the subform PropertyPicturesSubform
Code:
Private Sub Form_Current()

Dim path As String
    FileExists

End Sub

Sub FileExists()

Dim fso

Dim path As String
    path = "\\wbc-fp-1\users\etienne\data\My Pictures\Houses\" & [PicureName-Number] & ".jpg" ' change to match the file w/Path


 Set fso = CreateObject("Scripting.FileSystemObject")

    If Not fso.FileExists(path) Then
        [green]' MsgBox path & " was not located.", vbInformation, "File Not Found"[/green]
        Me.PropIm.Picture = "\\wbc-fp-1\users\etienne\data\My Pictures\Houses\BLANK.jpg"

    Else

        Me.PropIm.Picture = path ' MsgBox path & " has been located.", vbInformation, "File Found"
        PicturePath = path
   
    End If

End Sub
What I want to do is enable to user to choose the image they want, with a browse for image button.
So when clicked the button code opens a find file dialog box and the user then chooses the file and then the code takes the name of the file and inserts it into the PicureName-Number field.
And then links it as it does above.

Any help would be greatly appreciated.

Thanks [smile]

Thank you,

Kind regards

Triacona
 
Hi Duane, thanks for your help [thumbsup]
I managed to sort it to create a browse button with the code below:
The only problem I'm having now is an error when going to a blank record.
Run-time Error '94': said:
Invalid use of null
The highlighted code is
Code:
path = [PicureName-Number] ' change to match the file w/Path
When hovering over path it equals ""
Tools -> references MS office object library xxxx needs to be selected.
Code:
Private Sub Form_Current()

Dim path As String

    FileExists


    
End Sub
Sub FileExists()

Dim fso

Dim path As String
    path = [PicureName-Number] ' change to match the file w/Path



Set fso = CreateObject("Scripting.FileSystemObject")

    If Not fso.FileExists(path) Then

       ' MsgBox path & " was not located.", vbInformation, "File Not Found"
        Me.PropIm.Picture = "\\wbc-fp-1\users\etienne\data\My Pictures\Houses\BLANK.jpg"
    
    Else

         Me.PropIm.Picture = path ' MsgBox path & " has been located.", vbInformation, "File Found"
        ' PicturePath = path
       
    End If
    


End Sub

'PicureName-Number
Private Sub BrowseForImage_Click()
On Error GoTo Err_BrowseForImage_Click


Dim fdg As FileDialog, vrtSelectedItem As Variant
Dim strSelectedFile As String
  
Set fdg = Application.FileDialog(msoFileDialogFilePicker)
  
With fdg
  .AllowMultiSelect = False
  .InitialView = msoFileDialogViewDetails
    If .Show = -1 Then
      For Each vrtSelectedItem In .SelectedItems    'onby be 1
        strSelectedFile = vrtSelectedItem
      Next vrtSelectedItem
      Me![PicturePath] = strSelectedFile
      Me.PropIm.Picture = strSelectedFile
      [PicureName-Number] = Me![PicturePath]
    Else     'The user pressed Cancel.
    End If
End With
  
Set fd = Nothing



Exit_BrowseForImage_Click:
    Exit Sub

Err_BrowseForImage_Click:
    MsgBox Err.Description
    Resume Exit_BrowseForImage_Click
    
End Sub


So do I need to stop the user from going past the last record (access navigation tools) and create a new rec button and get rid of the access navigation buttons or is there some other way of solving it.
Any help would be appreciated, thank you! [bigsmile]
Also thanks to ADezii.


Thank you,

Kind regards

Triacona
 
You are attempting to assign Null to a string variable which causes the error.
Try:
Code:
path = [PicureName-Number] & "" ' change to match the file w/Path

Duane
Hook'D on Access
MS Access MVP
 
Done it! Yay![bigsmile]
Code:
Sub FileExists()

Dim fso

Dim path As String
    If IsNull([PicureName-Number]) Then
        path = "\\wbc-fp-1\users\etienne\data\My Pictures\Houses\BLANK.jpg"
    Else
        path = [PicureName-Number] ' change to match the file w/Path
    End If


Set fso = CreateObject("Scripting.FileSystemObject")

    If Not fso.FileExists(path) Then

       ' MsgBox path & " was not located.", vbInformation, "File Not Found"
        Me.PropIm.Picture = "\\wbc-fp-1\users\etienne\data\My Pictures\Houses\BLANK.jpg"
    
    Else

         Me.PropIm.Picture = path ' MsgBox path & " has been located.", vbInformation, "File Found"
        ' PicturePath = path
       
    End If
    


End Sub

Just one more question how would I concatenate the string of PictureName-Number?

Thanks for all your help[smile]


Thank you,

Kind regards

Triacona
 
Dear Duane,

Thanks for your reply [smile]
I want to get the last 15 characters of the path, i.e. the name of the file.
In queries it would be a LEFT or RIGHT command, how would one do that in a text box, referencing the path and then cutting it down to the file name?

Thanks again for your help.

Thank you,

Kind regards

Triacona
 
Hi All,

I have tried
Code:
[PicureNameNumber] = (Right(strSelectedFile, 12))
Within Private Sub BrowseForImage_Click()

Thanks :)


Thank you,

Kind regards

Triacona
 
Hi all,
oops submitted the above too fast.

The above code only works for 8 character long file names.
Is there a way to detect the file name, like a windows api, excluding the path, like the display in windows explorer.

Thanks for the forthcoming help [smile]

Thank you,

Kind regards

Triacona
 
Have a look to the InStrRev function.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top