Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Which Control Selected on Form Using a Control Array

Status
Not open for further replies.

rlamoreaux

Programmer
Aug 11, 2001
34
0
0
US
I built a 10 X 10 image control array on a form using vba. I'm able to index through this array and load images from differant records of a table. When loading the image I also set the controltiptext property to the record numberof the image, I want to use this to identify the record when the image is selected. Now I'm trying to click on 1 of these images and somehow get the record number so I can display the rest of the record of the image. Do I have to do 100 imageXX_Onclick sub routines or is there someway to tell what control is under the mouse when the onclick happens? all image control names are image00 to image99
 
It should be possible to use:

[tt]Screen.ActiveControl[/tt]


 
How are ya rlamoreaux . . .

[purple]No![/purple] . . . reason being you have to trigger some event to start your processing and [blue]there's no all inclusive event in access[/blue][sad] . . . albeit it would be nice! [wiggle]

Calvin.gif
See Ya! . . . . . .
 
rlamoreaux
I had forgotten that Image controls do not receive focus, so cannot be used with KeyPreview and KeyPress for the form, which is a useful way of coding for more than one control. The code below will write code for each image control in a form called frmForm.

Code:
Sub AddCodeToForm()
Dim ctl As Control
Dim frm As Form
Dim mdl As Module
Dim lno As Integer

DoCmd.OpenForm "frmForm", acDesign
Set frm = Forms!frmForm
Set mdl = frm.Module

    For Each ctl In frm.Controls
        If ctl.ControlType = acImage Then
            strCode = vbCrLf & "Private Sub " & ctl.Name & "_Click()" & vbCrLf _
                & vbTab & "DoCmd.OpenForm ""frmDetail"",,,""ID="" & Me." & ctl.Name _
                & ".Tag" & vbCrLf & "End Sub"
                lno = mdl.CountOfLines + 1
                mdl.InsertLines lno, strCode
        End If
    Next
        
End Sub
 
It is also possible to set the event for each control to the name of a procedure, which may be better than setting the tag property.

For example:
On Click: =ClickMe(
.Tag)

You could replace
.Tag with the actual ID in the Open event:
Code:
Sub ClickMe(intID)
DoCmd.OpenForm "frmDetail", , , "ID=" & intID
End Sub

Private Sub Form_Open(Cancel As Integer)
'Record set stuff here
For Each ctl In Me.Controls
    If ctl.ControlType = acImage Then
        ctl.OnDblClick = "=ClickMe(" & rs!ID & ")"
        'more Recordset
    End If
Next
'more Recordset
End Sub
 
Many thanks to REMOU The above code snippet sure saved me alot of work. Creating 100 subroutines seems like the best way to go.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top