populating picture property of image from value in a table

schenel

New member
Local time
Today, 11:26
Joined
Nov 14, 2002
Messages
8
In the OnOpen event of my form I would like the form to look up values in a table that contain the image control name and the imagefilename for the control.

I would like the form to load the imagefilename into the picture property of the image control from the value in the table. The table contains the image control name and the filename of the image to be used.

I know I have to loop through the table and I've gotten the data values from the table. but I'm stuck on using the data value for the image control name and matching it to the control in the form so that I can populate the image control picture property.

I hope this makes sense!

For example:

Table1
ID ImageControl ImageFileName
1 Image1 file1.bmp
2 Image2 file2.bmp
3 Image3 file3.bmp


The form would have the following controls
Image1, Image2, Image3, Image4, Image5, Image6, Image7, etc.

On open the form would look up the table and wherever it sees the image control name it takes the value from the ImageFileName column and puts it in the image control picture property.

Any help would be much appreciated. Thanks,

Ernie
 
Well,

I found the answer to my own question and I thought it might be a good idea to share it everyone:

Private Sub Form_Current()
'------declaration statements
dim dbs as database, rst as recordset, sql as string
dim fld as field
dim i, cnt as integer
dim strX, strY, strImg, strCtl, strDir as string
dim frm as form, ctl as control

'------set variables
strDir = "d:\images\"
set frm = Forms!Form1
set dbs = currentdb
sql = "SELECT id, Xcoordinate, Ycoordinate, imagefilename FROM table1"
set rst = dbs.openrecordset(sql)
rst.movelast
cnt = rst.recordcount
rst.movefirst

'------Start loops here
For i = 1 to cnt
with rst
for each fld in .fields
select case fld.name
case "Xcoordinate"
strX = fld.value
case "Ycoordinate"
strY = fld.value
case "imagefilename"
strImg = fld.value
end select
next fld
strCtl = "img" & strX & strY
with frm
for each ctl in .controls
if ctl.name = strCtl then
with ctl
.picture = strDir & strImg & ".bmp"
.visible = true
end with
end if
next ctl
end with
rst.movenext
end with
next i
end sub
 

Users who are viewing this thread

Back
Top Bottom