Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

repeating code within an on_click sub.

Status
Not open for further replies.

JulieS

MIS
Nov 23, 2000
48
CA
I'm so new to visual basic that i'm not even sure if i'm in the right forum, but here's my question:

Every time i click a button, i want my code within the sub button_onClick() to repeat. so, if i had a msgbox in this sub, every time i clicked the button, the message would appear. I'm not sure how to go about doing this (something with a do until loop maybe).

Thanks,
julie
 
Julie -

How many times do you want it to repeat? If it's just once per click, your code in the form should look like:
[tt]
Option Explicit

Private Sub Command1_Click()
MsgBox "Hello World"
End Sub
[/tt]

The Option Explicit is put there automatically when you set the "Require Variable Declaration" checkbox in VB's options. You see, VB doesn't actually require your variable to be declared before you use them -- it assumes they're of Variant datatype. If you don't have that option checked, and you mis-spell a variable, you end up with two variables, and debugging becomes much harder!

Chip H.
 
Julie -

remember, windows applications are event driven. that is to say that a particlar event triggers an action. in your case the event is clicking the button - hence chip's indication of using the [tt]commandButton[/tt]'s built in [tt]On_Click[/tt] event.

if you double click the button in the development environment then you will notice you are taken to the code page and the default event for that control (in this case a commandButton) is presented.

if effect windows will coninually check each control for events and when a particular event is triggered it will run the code you have defined (as shown by chip above). - all you have to worry about is ensuring all the possible events that a user may place upon an object are included in your code. Mark Saunders :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top