?
Solved

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

Posted on 2003-03-10
14
Medium Priority
?
141 Views
Last Modified: 2010-05-01
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
0
Comment
Question by:Lucy168
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 3
14 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 8107025
imgStallionPicture.Picture = LoadPicture("<path to picture>")
0
 
LVL 3

Expert Comment

by:DeAn
ID: 8108843
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
0
 

Author Comment

by:Lucy168
ID: 8119274
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")
 
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 18

Expert Comment

by:bobbit31
ID: 8120043
imgScanPicture.Picture = LoadPicture("C:\Forest View Stud Farm DBMS\Images\Scans\" & txtMareNo.text)
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 8120049
that is of course if txtMareNo is a textbox on your form
0
 
LVL 3

Expert Comment

by:DeAn
ID: 8121007
sorry bout that lucy.  this will give you the filename only.

Mid(CommonDialog1.FileName, InStrRev(CommonDialog1.FileName, "\") + 1)
0
 
LVL 3

Expert Comment

by:DeAn
ID: 8121193
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.





0
 

Author Comment

by:Lucy168
ID: 8124305
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)
0
 
LVL 3

Expert Comment

by:DeAn
ID: 8124382
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"
0
 

Author Comment

by:Lucy168
ID: 8124447
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?
0
 
LVL 3

Expert Comment

by:DeAn
ID: 8127079
oh, try using a PictureBox control instead of Image control
0
 
LVL 3

Expert Comment

by:DeAn
ID: 8133362
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))
0
 

Author Comment

by:Lucy168
ID: 8136529
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?  
0
 
LVL 3

Accepted Solution

by:
DeAn earned 60 total points
ID: 8142893
Lucy168, since the data for the picture is in the textbox, txtMareNo, I don't think you need to set the image data.  You're loading the picture from the string specified in the textbox (if I understand).  I would trim the db value if its a fixed length field that holds the filename and then refresh the image after loading the picture.

see if this works:

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

imgScanPicture.Refresh
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question