Link to home
Start Free TrialLog in
Avatar of Lucy168
Lucy168

asked on

LOADING A PICTURE INTO A PICTURE BOX IN A FORM, BROWSING THROUGH A DIRECTORY

I am trying to load a picture in a form by browsing through a directory and selecting a picture and am trying to store it in an image or picture box in a from. I then want this to be stored here, with the name of the file stored in an MS ACCESS Field. I have entered the following code below but it doesnt work.

Private Sub cmdStallionPicture_Click()
Dim intFile As Integer
Dim NewFile As Picture
   
On Error GoTo ErrHandler
cdbDialog1.Filter = "JPG(*.JPG)|*.JPG"
cdbDialog1.FilterIndex = 1
cdbDialog1.InitDir = "C:\Forest View Stud Farm DBMS\Images\Stallions\"
cdbDialog1.ShowOpen
NewFile = cdbDialog1.FileName

intFile = FreeFile
Open NewFile For Input As intFile
imgStallionPicture.Picture = Input(LOF(intFile), intFile)
Close #intFile

ErrHandler:
Exit Sub

End Sub
Avatar of bobbit31
bobbit31
Flag of United States of America image

imgStallionPicture.Picture = LoadPicture("<path to picture>")
Avatar of DeAn
DeAn

Private Sub cmdStallionPicture_Click()
   On Error GoTo ErrHandler
   cdbDialog1.Filter = "JPG(*.JPG)|*.JPG"
   cdbDialog1.FilterIndex = 1
   cdbDialog1.InitDir = "C:\Forest View Stud Farm  DBMS\Images\Stallions\"
   cdbDialog1.ShowOpen

   imgStallionPicture.Picture = LoadPicture(cdbDialog1.Path & "\" & cdbDialog1.FileName)

   'ToDo: insert cdbDialog1.FileName into db

   Exit Sub
ErrHandler:
   ' handle error...
End Sub

I wasn't sure about your db insert code, but cdbDialog1.FileName is the filename you wish to store.

hope it helps
Avatar of Lucy168

ASKER

Thanks, but cdbDialog1.FileName gives me the whole location path, including the file name. How do I just get the filename?

How do I store the value of a text box in a form onto the LoadPicture path:

I.e.
txtMareNo = 1

imgScanPicture.Picture = LoadPicture("C:\Forest View Stud Farm DBMS\Images\Scans\ & txtMareNo")
 
imgScanPicture.Picture = LoadPicture("C:\Forest View Stud Farm DBMS\Images\Scans\" & txtMareNo.text)
that is of course if txtMareNo is a textbox on your form
sorry bout that lucy.  this will give you the filename only.

Mid(CommonDialog1.FileName, InStrRev(CommonDialog1.FileName, "\") + 1)
even though it seems to work in prev post, you should include these optional args to InStrRev() function.  
I've broken the one-liner into 2 to make it more clear for you.

Dim Position As Long
Dim FName As String

'InStrRev returns position of "\" so we add 1 and that is the start of the filename.
Position = InStrRev(CommonDialog1.FileName, "\", -1, vbTextCompare) + 1

' Mid returns a (sub)string starting at Position to the end of the string
FName = Mid(CommonDialog1.FileName, Position)

hope that helps you.





Avatar of Lucy168

ASKER

When I browse through records in a form using the data control, I can't get a new picuture to load for each record. The filename is stored in a field on the database:

imgScanPicture.Picture = LoadPicture("C:\Forest View Stud Farm DBMS\Images\Scans\" & txtMareNo.text & ".JPG")


Where should I put this or what should I do so that a new picture will appear on the image box when I browse to a new record (i.e. 2nd record having MareNo = 2 and the file in C:\Forest View Stud Farm DBMS\Images\Scans\2.JPG)
I thought you were saving the file extension.  the code I gave was returning the filename with ext.. filename.jpg.  So if you save the file as filename.jpg as a string in the db, you don't need the & ".JPG"
Avatar of Lucy168

ASKER

That was just an example. I have got that done but I cant get it to load on the image box of a new record on a new form, using this LoadPicture method.

Any ideas please?
oh, try using a PictureBox control instead of Image control
no, that doesn't matter if picbox or image.

so what happens when you try loading it?

maybe the data a fixed length string in the db?
do you need to trim extra spaces?

imgScanPicture.Picture = LoadPicture(Trim(DBPictureVar))
Avatar of Lucy168

ASKER

I have a stallions form with a data control. Each form displays a record. I can get the image loaded into the image box, but when I go to a next record that picture is still there and it shouldn't for a new stallion. When I exit and return to that form/record the filename is stored but the picture is not displayed.

So I want to save the image in that image box of that record. I noticed there is a datasource and field name for the image box properites. Would I need to set this? If so what do I set it to, the filename field?  
ASKER CERTIFIED SOLUTION
Avatar of DeAn
DeAn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial