smiler44
01-16-2010, 12:40 PM
I've searched the internet but cantt make anything I've found work. The code may be good but perhaps I am just putting it in the wrong place.
when my form loads I would like to display a different image each time in an image box or different pictures in a couple of image boxes.
The files are in the format of A 123.jpg The numers could vary from 1 to 4 and may even have a letter or two after them. The A and sapce will be constant.
Please could you offer not only code but where to put it, in a module or form. Im usibg vb version 5
thank you in advance
smiler44
DCrake
01-18-2010, 12:59 AM
Place the nems of the fiels in a table with a primary key. Then do a randomise on the primary key to get the image name then load the image as normal.
David
smiler44
01-23-2010, 01:12 PM
Thank you I'll have a go. Don't know how to do a randomise so I may be back for help, if you want to tell me how before I ask I'd be greatful.
Could I do it on a column that contains entries in the format of c1 or c12 or c 123 or c 1234 if it is not a primary key or indexed?
smiler44
smiler44
01-24-2010, 11:51 AM
A bit of searching on the internet and some reading of books and I got the answear.
I'm a bit lucky in the fact that my images are stored as A 123
This is how I have managed to randomly select an image. I'll break it into parts.
I think the 9 in Int((9 - 0 + 1) * Rnd + 0) means generate a number up to number 9
Sub usedtocallrandomdigitfuntion()
call randomdigit
end sub
Function randomdigit() As Integer
Randomize
randomdigit = Int((9 - 0 + 1) * Rnd + 0)
End funtion
A random number has now been created. the function was then modified
for a couple of reasons, the file number that was randomly selected may not exists and I hadd to add the Aspace to the front of the number as well as include the path from where to select the image/picture.
just before the End Function statement this was added
Dim path As String
Dim mypic As String
Dim Drive As String ' new
Dim letter As String
Dim adv As String 'increment random digit
adv = "1" ' used if picture not found
letter = "a " ' a space
mypic = letter & randomdigit
Drive = "c" 'what drive to look on. on my project this is variable
path = Drive & ":\myproject\pictures\"
mypic = path & pic & ".jpg"
''''''''''''''''''''''
' now checks to see if file exists. if not then look for file 1 number higher
directoryfound = Dir(mypic, vbDirectory)
Do Until (Len(directoryfound)) > 0 ' 0 is retuned if no file is found
If (Len(directoryfound)) = 0 Then
If randomdigit > 2500 Then 'there are no files with a number higher 'then 2500
randomdigit = 1 'if random digit bigger then 2500 then make random 'digit 1
End If
randomdigit = randomdigit + adv ' if file not found increment by 1
mypic = path & letter & randomdigit & ".jpg" 'try again
directoryfound = Dir(mypic, vbDirectory)
End If
Loop
I then found some code that allowed me to load the picture into a small picture box keeping its ratio.
I'm well chuffed
smiler44