PC Review


Reply
Thread Tools Rate Thread

Calling a control event by a string value?

 
 
Rico
Guest
Posts: n/a
 
      23rd Jan 2007
Hello,

I have an issue that I'm actually dealing with in a VB6 app, but was
wondering if someone here happens to have an answer for this that I could
port to the VB6 app.

Is there a way to call an event from a string? I know I've seen code
that looks similar to the following;

Controls(control.name)("Validate") or
Controls(control.name).Events("Validate")

....at some point in my travels, but can't recall what the syntax was. The
reason I'm looking to do this is because the validate events on each field
are validating the data entered, setting the class properties and upon
leaving the form, are saving the values to the database through the
class.save method. The problem is when someone clicks on the "X" to close
the form down, the validate event does not fire. What I need to do is on
the UnloadQuery event of the form, cycle through the
controls and where the control does not equal the class property, run the
validate event. If the field is valid, then set the class property and
save, otherwise cancel the unloadquery event (similar to close or unload).

I know I've seen similar code and I'm sure it's not the last time it would
come in handy.

Any help would be appreciated.

Thanks!
Rick


 
Reply With Quote
 
 
 
 
Jamie Collins
Guest
Posts: n/a
 
      23rd Jan 2007


On Jan 23, 3:25 am, "Rico" <y...@me.com> wrote:
> I have an issue that I'm actually dealing with in a VB6 app, but was
> wondering if someone here happens to have an answer for this that I could
> port to the VB6 app.
>
> Is there a way to call an event from a string? I know I've seen code
> that looks similar to the following;
>
> Controls(control.name)("Validate") or
> Controls(control.name).Events("Validate")
>
> ...at some point in my travels, but can't recall what the syntax was. The
> reason I'm looking to do this is because the validate events on each field
> are validating the data entered, setting the class properties and upon
> leaving the form, are saving the values to the database through the
> class.save method. The problem is when someone clicks on the "X" to close
> the form down, the validate event does not fire.


Within scope, you can call the event handler using its name and
supplying any parameters e.g.

Option Explicit
Private Sub Text1_Validate(Cancel As Boolean)
' Validation code here
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Dim bCancel As Boolean
Text1_Validate bCancel
Cancel = bCancel
End Sub

An event handler is scoped as Private by default but there is nothing
(bar good software engineering principles <g>) preventing you from
changing it to Public.

Perhaps a better approach would be to have a separate Validate routine
that is called from all appropriate events e.g.

Option Explicit
Private Sub Text1_Validate(Cancel As Boolean)
Cancel = ValidateText1
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Cancel = ValidateText1
End Sub
Private Function ValidateText1() As Boolean
' Validation code here
End Function

Obviously, such an approach does not scale well e.g. you need a
dedicated routine for each control (and remember to call them all) or
an all-singing-all-dancing routine which could be a maintenance
headache. How you solve the perceived problem depends on the amount of
significance of the validation routines, the potential for code reuse,
coder skill level, personal style, etc but there are lot's of
possibilities (VBA6 has a CallByName method if your personal style is
'quick and dirty' <g>).

Jamie.

--

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Prevent Full String Selection after Change Event on Control Barry Tank Microsoft Access Form Coding 1 23rd Mar 2010 03:05 AM
Calling mousehover event for dynamic button control? Marc Microsoft VB .NET 3 1st Mar 2007 04:30 PM
Calling Event Handlers Inside Custom Control mike.gilmore@jccteam.com Microsoft ASP .NET 0 2nd Feb 2006 12:55 PM
Calling event procedure of control variable Mark A. Sam Microsoft Access Form Coding 3 8th Jun 2004 11:59 AM
calling a control's event procedure =?Utf-8?B?cG9seW5vbWlhbDVk?= Microsoft Dot NET Framework Forms 3 25th Mar 2004 01:01 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:14 PM.