Get the Image Name
Next, for 32 and 64 bit users, we can get the image name. We need this for the text box. Here's the code to add:
If fName <> "" Then
SlashPos = InStrRev(fName, "\")
ImageName = Mid(fName, SlashPos + 1)
End If
If you click the Cancel
button on the Open File
dialogue box then the file name will be blank. The If Statement checks for that. If it's not blank then we extract the image name with these two lines:
SlashPos = InStrRev(fName, "\")
ImageName = Mid(fName, SlashPos + 1)
First, we get the position of the last backslash (\). This is then used in the Mid
string method. Between the round brackets of Mid
we have the string we're trying to chop, which is in the fName
variable. After a comma, we then need a starting point to start chopping text. The starting point is the position of the backslash plus 1. Because we haven't specified an end position, Mid will chop to the last character in the fName
string. This will get us the name of the image, which we stored in the ImageName
variable.
The only other thing we need to do is to load the image into the Image Box on the form. Here's the code for you to add:
If Dir(fName) <> "" Then
Image2.Picture = LoadPicture(fName)
Image2.PictureSizeMode = 3
CopyImage = fName
cmdSave.Enabled = True
tbImageName.Text = ImageName
Else
MsgBox "Could not load image - no such file"
End If
This is more or less the same as we did before, for the Load Image Information
button. We're doing two things differently: one, we switch on the Save
button by setting the Enabled
property to True
; two, we place the image name into the tbImageName
text box.
There is one line in the code above that may seem a little odd. This one:
CopyImage = fName
What this line does is to store a copy of the file name in the CopyImage
variable. This is the variable you set up in the General Declarations area. We'll need this file name when we copy the picture to the images folder. This is done when the Save New Details
button is clicked. If we made CopyImage
local to the cmdAddNew
button then the cmdSave
button wouldn't be able to see it.
But the whole of the code for the cmdAddNew
button should look like this:
Try it out. Run your form and click the Add New Image
button. Select any image from your computer. When you click the Open
button on the dialogue box you should find that the image appears on your form, along with the image name in the text box.