Click button1 on 20th loop - vb.net

On my form I have one button (button1). When I click this button it starts my app. My app is meant to loop forever. Is there a way for my app to click button1 on the 20th loop?
Here is an example of my app:
**Click button1**
webbrowser.navigate("")
looping:
'some code
'some code
goto looping:

You'd need a variable to use as a counter, and increment it each time through the loop. During each loop iteration you can test the value of the variable, and if it's equal to 20 then fire the click event on the button.

I'm not sure if this is what you want, but perhaps this will get you started.
Dim i as Integer =1
Do
If i = 20 Then
Call Button_Click(Nothing, Nothing)
ElseIf i<=20
i+=1
End If
If shouldExit Then 'some other condition to break out.
Exit Do
End If
Loop While True

Related

Show and Hide Text Box With A Timer

I need some help with a VBA for a textbox. This Macro will be embedded into a button.
I have this code below and what it does is that it show/hide the textbox password whenever the button is pressed. The textbox Input Mask property is set to password by default.
I am trying to edit it such that it will show and hide with only one click of the button. I need help putting a timer to the show password for 4 seconds before automatically hiding it again.. Is this possible?
Sub example()
If Text1.InputMask = "Password" Then
Text1.InputMask = ""
Else
Text1.InputMask = "Password"
End If
End Sub
I'm quite the novice at VBA so any help is much appreciated! :)
You dont really need a "timer" - You just need to tell VBA to do nothing for a period of time. Here I defined the time to be 1 second. Put a DoEvents in the loop so it doesn't appear to freeze.
Private Sub btnShowPassword_Click()
Dim dtWait As Date
If Text1.InputMask = "Password" Then
Text1.InputMask = ""
dtWait = DateAdd("s", 1, Now) ' 1 Second
Do Until Now > dtWait
DoEvents
Loop
Text1.InputMask = "Password"
End If
End Sub

Set a sub-form hidden field to visible, based on a check box status

C, Thank you for your input and encouragement! I have changed my form and script slightly, I am afraid I kept the if then statement as I am comfortable with the formatting. The script now works when the 'On Open'event runs.
Private Sub Form_Open(Cancel As Integer)
Me.ChkAlbumNotes.SetFocus
If Me.ChkAlbumNotes.Value = False Then
Me.lblAlbumNotes.Visible = False
Me.txtAlbumNotes.Visible = False
Me.btnAlbumNotes.Visible = True
Else
Me.lblAlbumNotes.Visible = True
Me.txtAlbumNotes.Visible = True
Me.btnAlbumNotes.Visible = False
End If
Me.TrackName.SetFocus
If Me.TrackName = " " Then
Me.btnAddRecord.SetFocus
Else
Me.btnNextRecord.SetFocus
End If
End Sub
This is fine when the form opens for the first time but I have a set of navigation buttons that are installed by the application as Macros. I cannot add my script to the On_Click event when the button is clicked, as On_Click is linked to the Macro. Is there a way to incorporate the script from the On_Load process to the pre-defined macro? Or can you suggest a neater way to achieve my requirements which are;
When the form opens,a check is made for the existence of a false value in the checkbox
if the check box is set to false, then the Notes Text Box and label are hidden and the notes button is visible.
If the check box has a true value, then the Notes text box and label are made visible and the button is hidden.
On completion of the test I check the field Track Name
if this is empty, I assume I am at the last record and give the Add New Record button the focus
If Track Name is not empty, then focus is set to Next Record button
when this button is clicked, the next record page opens and the process starts again.
Many Thanks
Mike
You should use the Form_Current event instead of Form_Open . This fires on starting the form (2 times) and everytime you move to another record.
Private Sub Form_Current()
Me.lblAlbumNotes.Visible = Me.ChkAlbumNotes.Value
Me.txtAlbumNotes.Visible = Me.ChkAlbumNotes.Value
Me.btnAlbumNotes.Visible = Not Me.ChkAlbumNotes.Value
If Me.TrackName = "" Then ' I suggest If Me.TrackName = " " being a typo and you want to check if empty ( that's why you should use vbNullString instead of "")
Me.btnAddRecord.SetFocus
Else
Me.btnNextRecord.SetFocus
End If
End Sub

Change the Excel.Button.Caption property inside of a loop?

I have the code below attached to a button in Excel. When the button is clicked, I would like to display a countdown by changing the Caption of the button to "5... 4... 3... 2... 1... 0" and waiting 1 second between each change. The "i" variable holds the countdown second.
The cell to which I'm writing "i" displays the countdown correctly, but the button caption only changes when it gets to "0". It doesn't display the intermediate values.
Why does the button text not change until after the loop is finished?
Sub Timer()
Const COUNTDOWN As Integer = 5
Dim testButton As Excel.Button
Set testButton = Application.ActiveSheet.Buttons("TestButton")
testButton.Select
For i = COUNTDOWN To 0 Step -1
ActiveCell.FormulaR1C1 = i
testButton.Caption = i
Call Application.Wait(Now + TimeValue("00:00:01"))
Next i
End Sub
Likely some repaint isn't happening, whatever the reason is (assuming Application.ScreenUpdating isn't turned off... which shouldn't be the case, since the cell value does get updated).
Add a DoEvents call after the Caption assignment.
For i = COUNTDOWN To 0 Step -1
ActiveCell.FormulaR1C1 = i
testButton.Caption = i
DoEvents '<~ let Excel process what just happened
Application.Wait Now + TimeValue("00:00:01")
Next i
That will also let Excel process any pending events and Windows messages.

Call a sub after this one has finished

I have a userform looping through a range with 2 settings; manual and automatic.
When I have an option button on my form set to manual, and click a next command button, I check the next cell in the range, change the contents of the form accordingly, then wait for the next button press.
However if I find the option button is set to automatic then instead of finishing up my code and waiting for the next button press, I have to call the next button press programmatically. That means that the previous subs each calling the next code slowly build up in the call stack, and I worry this will have some memory implications if I'm looping over a large range.
In code:
Private Sub nextItem()
Dim willShow As Boolean
returnResults 'return details from the form to the sheet
clearMemory 'clear out previous items on form
itemNo = itemNo + 1 'iterate over the range
SetupParts 'place new items on form
'do what next
Select Case displaySetting 'this variable holds the result from my option button in a custom enum "dispMode"
Case dispMode.Manual 'always show form
willShow = True
Case dispMode.SemiAutomatic 'show form based on condition
willShow = (Data.Count = 0) 'if SetupParts returns no data, display form, otherwise keep hidden
Case dispMode.Automatic 'don't show form
willShow = False
End Select 'there are actually a few more options here, but I've simplified
If willShow = False Then
If Me.Visible = True Then 'if needs to hide, and currently visible, then hide the form
Me.Hide
nextItem 'this is the problem, I call this code again, so the call stack grows
Else
'form is already hidden, do nothing
End If
ElseIf Me.Visible = False Then 'willShow must be True
Me.Show 'then wait for button click
End If
End Sub
Private Sub commandBtnNext_Click()
nextItem
End Sub
To avoid this problem, is there any way of getting nextItem to run immediately after the previous call of nextItem has run; ie. to tell a sub to run immediately after this one has finished (without introducing time delays). Or maybe this isn't an issue; if so, please explain why.
UPDATE
There is also a SemiAutomatic check to see which mode to use based on the contents of the userform. This is fine when calling recursively, but I can't see how to incorporate it into a looping approach.

Pause VBA macro, allow user to make a selection, and restart where it left off

I want to allow the user to make a selection, run some code, pause for another selection, run more code?
I work with documents with large number of tables that eventually convert to HTML. Sometimes the formatting on two like tables doesn't convert the same. Knowing how the converter works, I'd like to copy all of the formatting data from one table and "paste" it onto another one.
I've the idea of a userform to have the user select something, hit a copy button, select something else and hit a paste button.
The timer function allows you to do this. It may not be the best way to code, but it is the answer to your problem:
'1st code here
Start = Timer
Do While Timer < Start + 10
DoEvents
Loop
'do 2nd code here
DoEvents allows the user to select text, etc. After 10 seconds, the code resumes at "2nd code."
You can use global a global variable:
Public myVar as Variant
Sub fillmyVar()
myVar = Selection
End Sub
Sub doSth()
' use myVar and the new selected text
End Sub
Using the answer from Aaron and incorporating it with a ToggleButton in the Userform you can successfully pause the code. With this you can then work in an additional selection to change the operation.
I originally did not use Global or Public Variables but soon learnt that its easier for passing data between Subs and Userforms
Userform:
Public Sub ToggleButton1_AfterUpdate()
'Program is Paused / Selected to Pause
If ProgBar.ToggleButton1.Value = True Then
'Changing the Text of the Toggle button once the program is selected to Pause
'If program paused then button will display continue
ProgBar.ToggleButton1.Caption = "Continue"
'For Sending to Loop Code
ProgramStatus = "0"
Call LoopCode.PrgStat(ProgramStatus)
End If
'Program is running / Selected to run
If ProgBar.ToggleButton1.Value = False Then
'Changing the Text of the Toggle button once the program is selected to continue
'If program running then button will display pause
ProgBar.ToggleButton1.Caption = "Pause"
'For Sending to Loop Code
ProgramStatus = "1"
Call LoopCode.PrgStat(ProgramStatus)
End If
End Sub
In your Module
Public Status As String
Public Sub PrgStat(ByVal ProgStatus As String)
Status = ProgStatus
End Sub
Sub SomeSub()
Do While
' Some Loop Code Running
If Status = "1" Then
'Toggle Not Pressed
End If
If Status = "0" Then
'Toggle Button Pressed
Do While Status = "0"
'Program will stop here until the togglebutton on the
'userform is pressed again which changes Status = 1
'This is where you can make another selection on the
'userform
DoEvents
Loop
End If
Loop
End Sub