Results 1 to 3 of 3
  1. #1
    TOPSie is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    101

    Creating an Event proc for a Dynamic control (TextBox)


    I am creating a set of TextBoxes dynamically based on records from a table.
    This is the code within the loop to create one TextBox – works OK
    I am trying to add an OnClick event as you see:
    Code:
     Set ctl = CreateControl(strForm, acTextBox, acDetail, , , wLeft, wTop, wWidth, wHeight)
        ctl.Name = "txt" & rsTemp![TL_Number]
        ctl.OnClick = "=GABLButtonClick('" & rsTemp![TL_Number] & "')"
    This is the embryo code for the OnClick event

    Code:
    Public Sub GABLButtonClick(wButton As String)
    MsgBox wButton
    End Sub
    However when I Click on a text box I get this message:-

    The expression you entered has a function name that Microsoft access can't find

    If I pause the code and look at the design of the TextBox then the OnClick event has


    Click image for larger version. 

Name:	txtEvent.png 
Views:	11 
Size:	34.8 KB 
ID:	45492
    Got the right value there – if you press the … next to it – then you get taken to the right place in the code. However it differs from a design generated event which has a value of [Event Procedure] (as per the Mouse Down which I created in design mode to illustrate.

    So missing something somewhere

  2. #2
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,389
    This may get you a bit closer.

    Code:
    ctl.OnClick = "[Event Procedure]"

    will create this in the form's code module:

    Code:
    Private Sub txt82001_Click()
    
    
    End Sub
    Now to figure out how to get the actual code lines in the procedure...
    Maybe you code the event first, before creating the textbox.

    Edit: My test indicates that indeed you can create the sub in the form's code module before the textbox control exists.
    So this could work for you if you know the textbox name when creating the sub.
    Last edited by davegri; 06-14-2021 at 01:48 PM. Reason: See edit

  3. #3
    TOPSie is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    101
    Yes it seems to work - the deeper I dig the more I realise that this may actually be unsuitable for a "finished article" - but having modified my code as below I now have a (potentially large number of) textboxes, each with a functioning OnClick event
    Code:
     Set frm = Forms(strForm)
       Set mdl = frm.Module
    .
    .
    .
    Set ctl = CreateControl(strForm, acTextBox, acDetail, , , wLeft, wTop, wWidth, wHeight)
        ctl.Name = "txt" & rsTemp![TL_Number]
        If rsTemp![TL_Picture] Then
            ctl.BackColor = cColGreen
        End If
        If Nz(rsTemp![TL_Image], "") <> "" Then
            ctl.Tag = rsTemp![TL_Image]
        End If
        ctl.OnClick = "[Event Procedure]"
         With mdl
            Line = .CountOfLines
            .InsertLines Line + 1, "Sub " & "txt" & rsTemp![TL_Number] & "_Click()"
            .InsertLines Line + 2, "GABLButtonClick(" & rsTemp![TL_Number] & ")"
            .InsertLines Line + 3, "End Sub"
        End With

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 6
    Last Post: 06-04-2018, 06:46 AM
  2. Replies: 7
    Last Post: 03-09-2018, 11:11 PM
  3. About Dynamic TextBox
    By SolmarDiotozou in forum Programming
    Replies: 2
    Last Post: 10-10-2014, 11:34 AM
  4. Calling Stored Proc in MS Access 2007 without creating query?
    By DistillingAccess in forum Programming
    Replies: 1
    Last Post: 08-03-2010, 09:38 AM
  5. dynamic textbox
    By vam in forum Programming
    Replies: 1
    Last Post: 06-04-2006, 08:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums