Solved

Adding events and handlers to controls created at runtime

Posted on 2004-08-31
3
379 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

763 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