[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

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

Posted on 2003-03-10
14
Medium Priority
?
144 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
  • 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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

607 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