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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create vba by mouse click/dynamically 2

Status
Not open for further replies.

Pampers

Technical User
Apr 7, 2004
1,300
AN
Hi everyone,
I was wondering how you could write vba code dynamically, say by clicking a button. What I want to achieve is to add some mousemove-events vba for a form.

Pampers [afro]
Keeping it simple can be complicated
 
Your sig line speaks volumes...

Your explanation was way too simple, and so very complicated to understand. Elaborate some if you expect an actual answer.

SgtLoehr
 
Hi SgtLoehr,
Ah, sorry for that, I will try again.

Say I have a form with a command button on it (nothing else). Could I put some code on this button so it would insert/write vba code, for example add a mousemove-event for the form itself or for any other form for that matter.
Is this more clear?





Pampers [afro]
Keeping it simple can be complicated
 
You can add code to a form, but only, as far as I recall, in design view.
 
try create a form with three controls
command button CmdAddText
text box ormname
text box texttoadd

Code:
Private Sub CmdAddText_Click()
On Error Resume Next
Dim mdl As Module
DoCmd.Hourglass True

DoCmd.OpenForm Me.FormName, acDesign, , , , acHidden
Set mdl = Forms(Me.FormName).Module
mdl.AddFromString Me.texttoadd

DoCmd.Close acForm, Me.FormName, acSaveYes
DoCmd.Hourglass False
End Sub
 
Tnx guys,
Yep only able to add code in design view, but that is fine. Below some code to create form and to create a textbox on that form (from a buttonclick on an other form).
Now, how could I add an event (say double click) for the textbox myTextbox??

Code:
Private Sub Command4_Click()
   Dim myForm As Form
   Dim myControl As Control
   'Dim myEvent As ??
   Dim strFormName As String
   
    '1. Create form
    Set myForm = CreateForm()
    
    'Set Name property
    strFormName = "myFormTest"
    myForm.Caption = strFormName
    
    '2. Create Textbox
    Set myControl = CreateControl(myForm.Name, 109)
    
   'Set Textbox properties
    With myControl
        .Width = 1500
        .Height = 200
        .Top = 440
        .Left = 200
    End With

Pampers [afro]
Keeping it simple can be complicated
 
Hi pwise,
I'm trying your module suggestion...

Pampers [afro]
Keeping it simple can be complicated
 
Code:
Set mdl = myForm.Module
mdl.AddFromString Me.texttoadd
 
Well, it is getting there...

This is what if found (in MsAccess Help under: CreateEventProc Method) with use of the Module-object. The code produces a new form, with a new button control and a click-event procedure.

Code:
Private Sub Command15_Click()
    Dim frm As Form, ctl As Control, mdl As Module
    Dim lngReturn As Long

    On Error GoTo Error_ClickEventProc
    ' Create new form.
    Set frm = CreateForm
    ' Create command button on form.
    Set ctl = CreateControl(frm.Name, acCommandButton, , , , _
         1000, 1000)
    ctl.Caption = "Click here"
    ' Return reference to form module.
    Set mdl = frm.Module
    ' Add event procedure.
    lngReturn = mdl.CreateEventProc("Click", ctl.Name)
    ' Insert text into body of procedure.
    mdl.InsertLines lngReturn + 1, vbTab & "MsgBox ""Way cool!"""

Exit_ClickEventProc:
    Exit Sub

Error_ClickEventProc:
    MsgBox Err & " :" & Err.Description
    'ClickEventProc = False
    Resume Exit_ClickEventProc

End Sub


Pampers [afro]
Keeping it simple can be complicated
 
Another way:

Code:
   Sub AddClick()
       Dim mdl As Module
       Dim lngLine As Long
       Dim strCode As String

       'The code to be inserted.
       strCode = "Private Sub cboCombo_Click()" & vbCrLf _
       & vbvtab & "MsgBox ""Hi""" & vbCrLf _
       & vbvtab & "End Sub" & vbCrLf

       Set mdl = Forms!Form1.Module
        'get the line number ...
        lngLine = mdl.CountOfLines
        
        'and insert the code.
        mdl.InsertLines (lngLine + 2), strCode
   End Sub
 
Thanx a lot pwise and Remou,
I'm getting the picture.



Pampers [afro]
Keeping it simple can be complicated
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top