Solved

Trigger only once

Posted on 2001-08-09
11
127 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
ID: 6371409
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
ID: 6371411
Sorry that should be form level, not from...
0
 

Author Comment

by:santosh_14
ID: 6371422
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:santosh_14
ID: 6371424
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
ID: 6371440
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
 
LVL 2

Expert Comment

by:BozzoCage
ID: 6371534
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
ID: 6371556
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
ID: 6371730
static b as boolean

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

Expert Comment

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

Expert Comment

by:VK
ID: 6380770
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
ID: 6427269
Ok! Serves the purpose
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB error "Type mismatch" 2 53
VB 6.0 printer how to align 6 62
passing parameter in sql procedure 9 61
Notepad++ how to remove delimiter : from beggning of the line? 3 101
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…

808 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