Solved

Trigger only once

Posted on 2001-08-09
11
125 Views
Last Modified: 2010-05-02
I want an Onchange event to call a function and trigger only once

private sub xyz_OnChange()
   cocojumbo() 'This should be executed only once and in later onchange event I do not want anything to happen
End sub

Pls help
0
Comment
Question by:santosh_14
11 Comments
 
LVL 11

Accepted Solution

by:
Otana earned 10 total points
Comment Utility
Use a variable at from level.

private HasRun as boolean

set it to false in your form_load

private sub xyz_OnChange()

If not HasRun then
  cocojumbo()
  HasRun=true
End If

End sub



0
 
LVL 11

Expert Comment

by:Otana
Comment Utility
Sorry that should be form level, not from...
0
 

Author Comment

by:santosh_14
Comment Utility
Thnks!!
Is there nothing in visual basic like:

set cocojumbo = dead

:-)

??

Will wait for today and if no better answer will give you the credit.
0
 

Author Comment

by:santosh_14
Comment Utility
Thnks!!
Is there nothing in visual basic like:

set cocojumbo = dead

:-)

??

Will wait for today and if no better answer will give you the credit.
0
 
LVL 11

Expert Comment

by:Otana
Comment Utility
Another option I can think off is adding a static variable to your function, which you set to true/false the first time your function runs, and test on that variable inside your function.

But I'm not entirely sure which default valeu a sttic boolean will get when you declare it.

public function CocoJumbo()
  dim static HasRun as boolean

  If HasRun = false Then
    ... ' Run code

    HasRun=true
  End If
end function
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Expert Comment

by:BozzoCage
Comment Utility
Otana, correct is
   static HasRun as boolean
and not
   dim static HasRun as boolean

>But I'm not entirely sure which default valeu a sttic boolean will get when you declare it.

Every variable has default value of it's type (Boolen->False, Integer->0, String->"") at creation time.
So static boolean var has default value False.
Otherwise your example is correct (and preferred for "triggers", since I doesn't require global variable.


santosh_14, just press F1 on Static, and you'll get an explanation. I think there is no better way of figuring out if (or how many times) function was already run than using static var.

-BozzoCage
0
 
LVL 11

Expert Comment

by:Otana
Comment Utility
BozzoCage

You're right about the static. Sorry, I haven't used statics in quite a while, and on my age you tend to forget these things... :)

Thnx
0
 
LVL 18

Expert Comment

by:deighton
Comment Utility
static b as boolean

if b then exit sub else b = true
0
 
LVL 44

Expert Comment

by:Arthur_Wood
Comment Utility
Like ANY Boolean variable, a STATIC Bool;ean get a default value of FALSE
0
 
LVL 6

Expert Comment

by:VK
Comment Utility
Hey Experts:

Why call xyz_OnChange() twice ?
If the event initiator is a Textbox-Change for example:

Sub Text1_Change
   call xyz_OnChange
   TextBox1.Locked = True
end sub

But perhaps your context doesn't allow this.

:-)
0
 

Author Comment

by:santosh_14
Comment Utility
Ok! Serves the purpose
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

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

15 Experts available now in Live!

Get 1:1 Help Now