Solved

Add text code in new control with ADD-IN

Posted on 2001-07-21
12
205 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
[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
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 4

Expert Comment

by:wileecoy
ID: 6305193
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
ID: 6305650
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
ID: 6305677
hearing...
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 1

Author Comment

by:mancio
ID: 6308368
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
ID: 6313370
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
ID: 6313491
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6314502
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
ID: 6318256
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
ID: 6325022
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
ID: 6326959
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
ID: 7156308
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
ID: 7157008
DanRollins
I beleave that I forgot choose this answer.
Excuse-me
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb6 connector to mongodb 2 141
VBA Shell can't Find Word document 11 131
Macro Excel - Multiple If conditions 2 88
VBA: copy range dynamically based on config sheet v3 6 33
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

726 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