Solved

Adding events and handlers to controls created at runtime

Posted on 2004-08-31
3
375 Views
Last Modified: 2006-11-17
I'm trying to make an advanced search engine for a customer account database.  This is Win2k, Access 2000, VB6.  It starts with a form containing a text box and a command button.  The button will create a new form that contains a bunch of label controls that display the search results.  I want each of these labels to respond to the double click event and pull up a form displaying all the info for the record that was clicked.

I have experience with Java and Javascript but very little with Visual Basic.  I don't know how to attach an event listener and an event handler to a control that was just created at runtime.  In Javascript I could just set the onDblClick property for each control to point to a function and then inside that function I could use event.srcElement to find out which record was clicked.

Can this be done in a similar fashion with Visual Basic?  

I have searched the Internet and the search results all want to show me how to attach code to a command button on a form that is being created in design view, but I already know how to do that.

I have already created the form and the controls showing the search results (thanks to some help from this website) and all I have left is to attach the double click event listeners to the controls and I'll be all set.

Here is the code that is attached to the search command button if this helps to clarify where I am at.


Option Compare Database


Private Sub Search_Click()

Dim frmSearchResults As Form
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim cl, cl2, cl3 As Control
Dim strSQL, strFormName, strLastRecord As String
Dim clrRed, clrBlack As Long

clrRed = RGB(255, 0, 0)
clrBlack = RGB(0, 0, 0)

Set frmSearchResults = CreateForm(, "frmResults")
strFormName = frmSearchResults.Name
frmSearchResults.RecordSelectors = False
frmSearchResults.NavigationButtons = False

DoCmd.Restore
Set cl = CreateControl(strFormName, acLabel, acDetail, "", "", 100, 100, 5000, 200)
cl.Caption = "Search Results for: '" & Forms.frmSearch.Text6 & "'"
Set db = CurrentDb()
strSQL = "SELECT * FROM dbo_mcaccount WHERE acct_nam LIKE '*" & Forms.frmSearch.Text6 & "*' ORDER BY acct_nam"
Set rs = db.OpenRecordset(strSQL)
If Not rs.EOF Then
    rs.MoveFirst
End If
x = 1
Do While Not rs.EOF
    If Not (rs!acct_nam = strLastRecord) Then    ' Adds a line between records with different acc names
        Set cl = CreateControl(strFormName, acLine, acDetail, "", "", 50, 200 + x * 200, 7000, 0)
    End If
       
    Set cl = CreateControl(strFormName, acLabel, acDetail, "", "", 200, 200 + x * 200, 3000, 200)
    Set cl2 = CreateControl(strFormName, acLabel, acDetail, "", "", 3000, 200 + x * 200, 4000, 200)
    Set cl3 = CreateControl(strFormName, acLabel, acDetail, "", "", 5500, 200 + x * 200, 2000, 200)
    cl.OnDblClick = "[Event Procedure]"        ' THIS DOESN'T DO ANYTHING
    x = x + 1
    strLastRecord = rs!acct_nam
    cl.Caption = rs!acct_nam             ' display customer info on the label controls
    cl2.Caption = rs!acct_alias_nam
    cl3.Caption = rs!acct_num
    If rs!status_cd = "A" Then            ' If customer is active, then display in black, otherwise red for non-active
        cl.ForeColor = clrBlack
        cl2.ForeColor = clrBlack
        cl3.ForeColor = clrBlack
    Else
        cl.ForeColor = clrRed
        cl2.ForeColor = clrRed
        cl3.ForeColor = clrRed
    End If
   
    rs.MoveNext
Loop



DoCmd.OpenForm (strFormName)       ' open form in form-view

End Sub

0
Comment
Question by:rhinoceroshead
  • 2
3 Comments
 
LVL 12

Accepted Solution

by:
fulscher earned 65 total points
ID: 11943213
rhinoceroshead,

You can do this, although it's not very intuitive. If you go to the design view of the form, you can attach code to the events. In the design view, this shows as "[Event Procedure]". Instead of this, you can set this property to an equal sign and a function name, e.g.

Command1.OnClick = "=MessageBox("Hello, world!")

This will set the OnClick event of the command1 button to the MessageBox function. Note that the functions referred to this way must be global and they must return a variant (if memory serves correctly). so, this answers one part of your question.

Now, how to refer to controls created at run time? Easy:

    Me.Controls("Combo0").OnDblClick = "=MsgBox(""Hello, world"")"

Just make sure that each control has a name.

Hope this helps
Jan
0
 

Author Comment

by:rhinoceroshead
ID: 11943542
Jan,

Beautiful.  I've got the "=eventHandler()" working now.  I must not have been clear about the second question.  I meant that I was looking for a way to reference the object that fired the event from within the object handler since the same handler will handle all the different events, but it's no big deal, I can just pass the object name as a function argument to the object handler.

Thanks a lot!

-Shannon
(rhinoceroshead)
0
 
LVL 12

Expert Comment

by:fulscher
ID: 11943860
Shannon,

Unfortunately there's no bit of information telling you which control has fired the event (AFAIK), so, as you already have found out, you should pass the control name or any other unique information bit as a parameter to the event handler:

Me.Controls("Combo0").OnClick = "=EventHandler(""Combo0"")"

Of course, you could go even a step further and set the parameter to a query to be executed, the name of a form to be opened with the ID of the record to be displayed, etc.

Jan
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now