Create every 2 hour Timer and display message box - timer

I'm trying to create a program that will alarm every 2 hours and a message box will be display

Another option would be to set a module-level variable to the current time + two hours (Now + (2.0 / 24.0)), then have the Timer interrupt check to see if the current time is greater than this stored value.

You could use a Timer control to do this but you will not be able to directly set the Interval to 7200000. You could set your Interval to 60000 (and set Enabled property to True) and have a variable that counts the minutes that have gone by:
Dim iMinutesElapsed As Integer
Private Sub Timer1_Timer()
iMinutesElapsed = iMinutesElapsed + 1
If iMinutesElapsed = 120 Then
DoAlarm
iMinutesElapsed = 0
End If
End Sub
Private Sub DoAlarm()
MsgBox "2 hours have gone by."
End Sub
A better approach is to store a variable with the value of the Timer function and check how long it's been since you called Start. In this approach, set Timer1's Interval to 1000 and Enabled to False, the Start method will enable Timer1 for you:
Dim sngStart As Single
Public Sub Start()
sngStart = Timer
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
Dim sngElapsedTime As Single
sngElapsedTime = Timer - sngStart
If sngElapsedTime < 0 Then
' Clock passed midnight
sngElapsedTime = (86400 - sngStart) + Timer
End If
If sngElapsedTime > 7200 Then ' counting seconds with this code
sngStart = Timer ' Restart
DoAlarm
End If
End Sub
Finally, a solution that will be triggered every odd hour on minute 30 as described in the comments:
Private Sub Timer1_Timer()
If Hour(Now) Mod 2 = 1 And Minute(Now) = 30 Then
DoAlarm
End If
End Sub

You could also use the Task Scheduler to run a VBScript that displays your message box. You simply have to create a Task with a Trigger that repeats every 2 hours, indefinitely.
The VBScript can display the time:
MsgBox "It's now " & FormatDateTime(Now, vbShortTime)

Related

Save to access database at 8pm or specific time

i need to save to database at specific time at 8pm i cant seem to save it..
Public Sub updateDatabase(ByVal data As String) // update database using this function
With txtIn //textbox
'Dim con As OleDbConnection = New OleDbConnection("D:\POLI\SEM 5\PROJECT\Monitoring PH and Temperature\Monitoring PH and Temperature\PHTempdb.mdb")
'Dim sql2 As String = String.Empty
Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\POLI\SEM 5\PROJECT\Monitoring PH and Temperature\Monitoring PH and Temperature\PHTempdb.mdb") //database
'Dim dateTime.Now As String = Date
Dim date1 As Date = DateTime.Now
Dim str As String = date1.ToString("yyyy/MM/dd")
Dim insert As String = "INSERT INTO report (PHVALUE_TEMPERATURE, DATE_TIME) VALUES ('" & data & "', '" & DateTime.Now & "');"
Dim cmd As New OleDbCommand(insert, conn)
conn.Open()
If (DateTime.Now.Hour = 20 & DateTime.Now.Minute = 00) Then //save at 8 pm daily
cmd.ExecuteNonQuery() // execute
End If
System.Threading.Thread.Sleep(5000)
'.Clear()
'cmd.ExecuteNonQuery()
conn.Close()
' End If
'.Clear()
End With
End Sub
Private Sub SerialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles sp.DataReceived
Dim str As String = sp.ReadExisting()
Invoke(myDelegate, str)
updateDatabase(str)
End Sub
If you can save your database programmatically normally by simply running a procedure, then you can add a Timer Control to your form and then set it to run in the appropriate of milliseconds from now.
Given the scheduled datetime and the current datetime you can use DateDiff to calculate the number of seconds from "now", and multiply by 1000 to get milliseconds.
You can find detailed instructions on using the Timer Control here and here.
Notes
At the beginning of the procedure called by the timer event, set the Enabled property to False to stop the procedure from running again. (Setting the interval to 0 does not cause the timer to stop.)
Since the timer interval was set relative to current time, it can't be "re-used" but if you want to automatically schedule the procedure again for the following day, you can simply recalculate the number of milliseconds and reset timer at the end of execution of your procedure.
The maximum value for the Interval is 2,147,483,647 milliseconds so the farthest in the future you can set a schedule using this method is about 25 days.

Use Excel Loop (VBA) to update array ranges

End result desired: Use a macro to select a range of cells (D530:O530, then D531:O531, down to 1029) and "convert" the standard function into an array.
Rather than hitting F2 and CSE 500 times, I figured there should be an easy way to have Excel do the lifting.
I recorded a macro for the first couple lines (as an example) and attempted to then repeat that operation 500 more times.
Sub ArrayUpdateLoop()
'
' ArrayUpdateLoop Macro
'
Range("D530:O530").Select
For i = 1 To 500
Range("D530:O530").Select
ActiveCell.Offset(1, 0).Select
Selection.FormulaArray = "=Spreader(Rollouts,R[-503]C:R[-503]C[14])"
Next
End Sub
The above is my proposed/modified code using a loop, which seems to run, but doesn't actually perform the operation.
This is my first time using a loop to redo an operation X number of times, and I've not found much of the MS documentation/etc. terribly helpful.
Appreciate any feedback or help :-)
You need to offset by one row down in each loop:
Sub OffsetDown()
Dim i&, rng As Range
'// Start from the row above to line up with loop
Set rng = Range("D529:O529")
For i = 1 to To 500
Set rng = rng.Offset(1) '//Move one cell down
rng.FormulaArray = ...
Next
End Sub

Timer, Time code, Time format countdown

I read the data from mysql database with command
vrijeme = myData.Item(2) ' translation vrijeme = time
ime = "Dobrodosli " & myData.Item(0) & "" ' translation ime = name
GameForma.Show()
After that i store the vrijeme to the textbox with
txtvrijeme.text = vrijeme
Then i add timer control on form and want to countdown from the time in textbox to 0 ..
The time in textbox is in format 00:00:00
Anyway's ill set the entire project in .rar with connection strings and all needed stuffs if anyone want to help
Dropbox :https://www.dropbox.com/s/maxmxeak0cr353q/PocetnaVerzija.rar
The solution would be . Read the data from value
hours = txtTimefromDB.Text.Substring(0, 2)
minutes = txtTimefromDB.Text.Substring(3, 2)
sec = txtTimefromDB.Text.Substring(6, 2)
Start decrementing
Private Sub tmrCountdown_Tick(sender As Object, e As EventArgs) Handles tmrCountdown.Tick
sec = sec - 1
If sec = 0 Then
minutes = minutes - 1
sec = 60
ElseIf minutes = 0 Then
hours = hours - 1
minutes = 60
End If
txtTimefromDB.Text = "" & hours & ":" & minutes & ":" & sec
End Sub

Checking if an index in a array in empty VB6

I have been getting into some object-oriented features of VB6. I've done lots of OOP with Java, and I'm trying to get this to work:
I have a array of Card objects, I want to check if an object in the index in the array has been created.
Dim cardsPlayer1(1 To 10) As Card
I created objects like this:
Set cardsPlayer1(index) = New Card
If tried using this to test if I have assigned an object to an index yet:
For counter = 1 To 10
If (cardsPlayer1(counter) Is Nothing) Then
Set cardsPlayer1(counter) = New Card
End If
Next counter
But it gave me a true value everytime and creating a new object to the whole array.
Here's the relevant code:
'Jordan Mathewson
'May 31, 2013
Dim cardsPlayer1(1 To 10) As Card
Dim cardsPlayer2(1 To 10) As Card
Private Sub cmdStartGame_Click()
Call addCard(1)
End Sub
'Called to add a card to one of the player's stacks
Private Sub addCard(player As Integer)
Dim counter As Integer
'To add a card to player1..
If (player = 1) Then
For counter = 1 To 10
If (cardsPlayer1(counter) Is Nothing) Then
Print "Object created." '<- Printed 10 times.
Set cardsPlayer1(counter) = New Card
End If
Next counter
'To add a card to player2..
Else
For counter = 1 To 10
If (cardsPlayer2(counter) Is Nothing) Then
Set cardsPlayer2(counter) = New Card
End If
Next counter
End If
Call refreshForm
End Sub
If I understand you correctly, the addCard sub should add one card but it adds all of them, when only called once. This isn't because it can't tell which array element is empty. It's just because it doesn't stop after successfully adding one.
For counter = 1 To 10
If (cardsPlayer1(counter) Is Nothing) Then
Set cardsPlayer1(counter) = New Card
Exit For ' <-- Add this
End If
Next counter
Without the Exit For, it will keep looping through the array and initialize the rest of it.
I suspect you might have a scoping issue. This gives me the expected results:
Sub Test()
Dim objectsArray(1 To 5) As TestObject
If objectsArray(1) Is Nothing Then
MsgBox "objectsArray(1) Is Nothing" ' <----- displayed
End If
Set objectsArray(1) = New TestObject
If objectsArray(1) Is Nothing Then
MsgBox "objectsArray(1) Is Nothing"
Else
MsgBox "Not objectsArray(1) Is Nothing" ' <----- displayed
End If
End Sub
Where do you declare objectsArray; where do you create the object; where is the loop? (Are these code snippets in different modules/class modules/functions?)

Circulate playing audio with MediaElement in WPF

I am developing a video player with WPF.(in VB)
I have already created a MediaElement ,ListBox, "Next" button,
then start playing through reading ListBox,
and use "Next" to skip to next audio/video.
In "MediaEnded" event, i just copy all code in "Next" button.
Now, problem is coming,
Assume the Listbox has four audios(.mp3), "test1.mp3", "test2.mp3", ......
now playing is "test1.mp3", i push "Next" button, then now playing is "test2.mp3".
However, when i just let "test1.mp3" play completed, my player will not play "test2.mp3",
it plays "test3.mp3" or others in random.
This situation like "MediaEnded" event was processed for many times.
Private Sub MediaElement1_MediaEnded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MediaElement1.MediaEnded
nextmedia()
End Sub
Private Sub nextmedia()
Try
'pi is play index, start from 1, 0 is non playing
If pi <> 0 Then
If pi = ListBox_temp.Items.Count Then
Dim filename As String = ListBox_temp.Items.Item(0).ToString
MediaElement1.Source = New Uri(filename)
pi = 1
Else
Dim filename As String = ListBox_temp.Items.Item(pi).ToString
MediaElement1.Source = New Uri(filename)
pi = pi + 1
End If
End If
Catch ex As Exception
End Try
Window1.Title = "Video Sampler - " + CStr(pi) + ". " + CStr(ListBox1.Items.Item(pi - 1))
End Sub
Who can help me....
I haven't tested it, but try the following.
Try
pi = If(pi < ListBox_temp.Items.Count - 1, pi + 1, 0)
Dim filename As String = ListBox_temp.Items.Item(pi).ToString
MediaElement1.Source = New Uri(filename)
Catch ex As Exception
End Try
This increments pi unless the last ListBoxItem is selected, in which case it sets it to zero.

Resources