Solved

Add text code in new control with ADD-IN

Posted on 2001-07-21
12
199 Views
Last Modified: 2012-05-04
I need create a simple code for gotfocus and lostfocus events when I create a new control in design mode.

I am using a ADD-IN with detect when I create a new control and if control is a textbox, but i dont know how I create text code for this new control

for example, when I create a Text1 textbox control i need create a followin text :
Sub Text1_Gotfocus()
  Call VerifyRule
End Sub
0
Comment
Question by:mancio
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 4

Expert Comment

by:wileecoy
Comment Utility
Have you considered using the control array method?  That way you would only have to have the code in one place no matter how many controls of that array (type) are added.

For example:

Sub Text1_GotFocus(Index as Integer)
     Call VerifyRule
End Sub

This would work for all controls going by Text1, the index property would be incremented for each additional control added.

hth
0
 
LVL 5

Expert Comment

by:KDivad
Comment Utility
Interesting. Wondered that myself.

wileecoy,
He's using a sort of "code generator". He needs to know how to get the code window to add code to it.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
hearing...
0
 
LVL 1

Author Comment

by:mancio
Comment Utility
wileecoy, I beleave that you dont understood.

I need to create a same code for each text box in any forms (and any projects).

In this case, i have a gotfocus rotine that I use in each text box in any projects, but I need type Call VerifyRule in each textbox that I create.
I beleave that Add-in is soluction, but I dont know how I create a code in Add-in.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
Maybe doing this:
' on general declarations section of main form
' If you don't have one, you will need to use a class.
Dim WithEvents txt As VB.TextBox

For each event that was created with, you need to add an Index parameter and a select case statement (if it is necesary) to manage multiple textboxes.

Whenever you create a new text and add to array you should write something like this:

set txt(index) = controls.add..... and the rest of code...

Hope it helps.
0
 
LVL 1

Author Comment

by:mancio
Comment Utility
Richie
I dont understood you.
When I type Call VerifyRule in set txt(index)=control.add

Can you be to give me more details ?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
When you use WithEvents keyword, VB creates all the procedures/methods/properties for that control.
Example: after you have put
dim withevents txt as vb.textbox
in general declarations section, you get all methods just like if you were adding a control on a form thrghout Toolbox (take a look at combos in code panes for objects and procedures!).
It should looks like this:

Private Sub txt_Click(Index As Integer)
    Call VerifyRule
End Sub

Hope it helps.


0
 
LVL 5

Expert Comment

by:KDivad
Comment Utility
Wondering if I'm the only one seeing the "add-in" part of the question...
0
 
LVL 4

Accepted Solution

by:
wileecoy earned 150 total points
Comment Utility
Not sure what exactly you need, but I got the following from MSDN (just do a search on ('CreateEventProc'):

It is wordy, but very descriptive.  

The following basically allows you to create procedures for controls, add lines to existing procedures, and also find specific lines of code within a designated procedure so that your add-in doesn't inadvertently add the same code over and over (for example, as the user adds additional textboxes that are part of a control array).

Again, not sure if this is what you are looking for, but if you read through it, let me know and we'll narrow it down.



----------------

CreateEventProc Method
         

Creates an eventprocedure.

Syntax

object.CreateEventProc(eventname, objectname) As Long

The CreateEventProc syntax has these parts:

Part Description
 
object - Required. An object expression that evaluates to an object in the Applies To list.
 
eventname - Required. A string expression specifying the name of the event you want to add to the module.

objectname - Required. A string expression specifying the name of the object that is the source of the event.


Remarks

Use the CreateEventProc method to create an event procedure.

For example, to create an event procedure for the Click event of a Command Button control named Command1 you would use the following code, where CM represents a object of type CodeModule:

TextLocation = CM.CreateEventProc("Click", "Command1")

The CreateEventProc method returns the line at which the body of the event procedure starts. CreateEventProc fails if the arguments refer to a nonexistent event.

---------------------

Ok -  you may already have that.... if not, that gets you the event procedure.

Now - to insert lines into existing code:

InsertLines Method
         

Inserts a line or lines of code at a specified location in a block of code.

Syntax

object.InsertLines(line, code)

The InsertLines syntax has these parts:

Part Description
object - Required. An object expression that evaluates to an object in the Applies To list.

line - Required. A Long specifying the location at which you want to insert the code.

code - Required. A String containing the code you want to insert.

Remarks

If the text you insert using the InsertLines method is carriage return?linefeed delimited, it will be inserted as consecutive lines.


InsertLines Method Example:

The following example uses the InsertLines method to insert a line, ?Option Explicit,? in the specified code pane.

Application.VBE.CodePanes(1).CodeModule.InsertLines 1, "Option Explicit

-----------

Ok - now you can enter a line in if you know what line of code you are at... what if you don't know the line number, but you know the procedure name (text1_LostFocus)...


ProcStartLine Property
         

Returns the line at which the specified procedure begins.

Syntax

object.ProcStartLine(procname, prockind) As Long

The ProcStartLine syntax has these parts:

Part Description

object - Required. An object expression that evaluates to an object in the Applies To list.

procname - Required. A String containing the name of the procedure.  

prockind - Required. Specifies the kind of procedure to locate. Because property procedures can have multiple representations in the module, you must specify the kind of procedure you want to locate.

**** All procedures other than property procedures (that is, Sub and Function procedures) use vbext_pk_Proc.


You can use one of the followingconstants for the prockindargument:

Constant Description
vbext_pk_Get - Specifies aprocedure that returns the value of a property.
 
vbext_pk_Let - Specifies a procedure that assigns a value to a property.
 
vbext_pk_Set - Specifies a procedure that sets a reference to an object.

vbext_pk_Proc - ****Specifies all procedures other than property procedures.


Remarks

A procedure starts at the first line below the End Sub statement of the preceding procedure. If the procedure is the first procedure, it starts at the end of the general Declarations section.


ProcStartLine Property Example:

The following example uses the ProcStartLine property to return the line at which the specified procedure begins in a particular code pane.

Debug.Print Application.VBE.CodePanes(3).CodeModule.ProcStartLine ("SetupTabs", vbext_pk_Proc)

------------

Similarly, the following returns the first line of a designated procedure...


ProcBodyLine Property
         

Returns the first line of a procedure.

Syntax

object.ProcBodyLine(procname, prockind) As Long

The ProcBodyLine syntax has these parts:

Part Description
object - Required. An object expression that evaluates to an object in the Applies To list.

procname - Required. A String containing the name of the procedure.

prockind - Required. Specifies the kind of procedure to locate. Because property procedures can have multiple representations in themodule, you must specify the kind of procedure you want to locate.

*****All procedures other than property procedures (that is, Sub and Function procedures) use vbext_pk_Proc.


You can use one of the followingconstants for the prockind argument:

Constant Description
vbext_pk_Get - Specifies a procedure that returns the value of a property.

vbext_pk_Let - Specifies a procedure that assigns a value to a property.

vbext_pk_Set - Specifies a procedure that sets a reference to an object.

vbext_pk_Proc - ****Specifies all procedures other than property procedures.


Remarks

The first line of a procedure is the line on which the Sub, Function, or Property statement appears.


ProcBodyLine Property Example:

The following example uses the ProcBodyLine property to return the line number of the first line of code in the specified procedure, SetupTabs, in a particular code pane.

Debug.Print Application.VBE.CodePanes(3).CodeModule.ProcBodyLine ("SetupTabs", vbext_pk_Proc)

------------

Also - search MSDN with 'Find Method (VBA Add-In Object Model)'.  That will give you a way to find out if your code is already there so you don't add it a second time...

Find Method Example:

The following example uses the Find method to verify that the specified block of lines, lines 1261 through 1279, of a particular code pane does contain the string "Tabs.Clear."

Application.VBE.CodePanes(2).CodeModule.Find ("Tabs.Clear", 1261, 1, 1280, 1, False, False)


If you need that code, I can also post it, but this is getting long ,whew.

-------------

hth.
0
 
LVL 1

Author Comment

by:mancio
Comment Utility
wileecoy, I beleave that your comment is a awnswer, but i will text in weekend and then i wiil accept your comment.
Please, send me a code to email claudio@mancio.com.br

Regards
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
Hi mancio,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept wileecoy's comment(s) as an answer.

mancio, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 
LVL 1

Author Comment

by:mancio
Comment Utility
DanRollins
I beleave that I forgot choose this answer.
Excuse-me
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

744 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

8 Experts available now in Live!

Get 1:1 Help Now