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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Count Up Timer 1

Status
Not open for further replies.
Jan 13, 2008
167
US
HEy guys I can't get my mind around a program i'd like to create.

I just got my first job work on some servers and I want a downtime timer. I want a VBA program that can keep different timers that count up.

I have 3 servers so three different labels that can be reset if the servers go down.

So what would the code be behind the server that adds a day if it goes a full 24 hours?

I haven't started it yet I'm just exploring different avenues!
 
>I want a VBA program ...

In that case, you might want to explore forum707.
 
Your requirements are very vague. Are your timers supposed to automatically detect if a server goes down, or is the start and stop done manually?

Also, wouldn't two datetime fields, StartupTime and ShutDownTime for example, be all that you need to calculate the time span? What does a "timer" do for you?

 
harebrain and Joe give me the idea to see what you have so far...

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
i don't have anything. I'm just wanting to have a day counter. That resets with a button that stores in a file so if my computer goes down when i start it back up it will continue on it's path.
 
This is an interesting exercise in problem definition. :)

Matt, you need to define your problem more accurately. To illustrate this, I'll ask some questions, which perhaps you can answer.

<that adds a day if it goes a full 24 hours
I'm unaware of any days that do not go a full 24 hours. Please clarify.

<I'm just wanting to have a day counter.
What sort of days do you want to count? Surely not all of them, so what are the criteria that need to be met in order to determine that a day is countable?

<That resets with a button that stores in a file
Why do you want to store a button in a file? If that's not what you're saying, precisely what do you want to store in a file?

<if my computer goes down when i start it back up
So, you only want to keep track of the times that the computer goes down after you start it back up, presumably after first shutting it down manually. What's the underlying reason that you would do this, or are you trying to say something else?

Let's start there and see what you come up with.

HTH

Bob
 
i can see how this is confusing.

1. Well that's just saying that as a day passes I want the Days to increase by one.

ex. Days = Days + 1

2. I want to count the number of days that the servers dont have down time. So for instance I restart a server click start and it just runs and if the server crashes then it resets but saves the document like every 5 minutes or whatever time i set. So if it crashes I can read the log and it'll say "24 days, 3 hours, 15 minutes, 32 seconds of no down time"

3. I guess I don't need a button if all i need to do is start the counter and it reset if the server crashes

4. no not of when my computer goes down, i need to be able to "reset" the counter if the servers go down, but this wont be necessary if i have it saving to a file and resetting upon opening
 
How do you plan to write the log noting the time AS THE SERVER CRASHES? Or is the script supposed to run on a client computer that polls the servers and writes the log there?

As the first response to your original question pointed out, this isn't the VBA forum, it's for Visual Basic version 5 and 6.

Lee
 
i figure that if the server crashes the program goes down with it and the last known time in the file will be the closest estimate to how long the server was up and kicking.

here is the code i have so far:
Code:
Seconds.Text = Seconds.Text + 1
If Seconds.Text = 60 Then
    Minutes.Text = Minutes.Text + 1
    Seconds.Text = 0
    If Minutes.Text = 60 Then
        Hours.Text = Hours.Text + 1
        Minutes.Text = 0
        If Hours.Text = 24 Then
            Days.Text = Days.Text + 1
        End If
    End If
End If

very very simple, not 100% accurate, however when you are going for estimates this is good.

i'm probably going to do something like this as the string to save the file

Code:
savedstring = days.text & " Days - " & hours.text & " Hours " & minutes.text & " Minutes " & seconds.text & " Seconds"

however i'm not that good at saving files yet.

Any help in that location?
 
Got It. Using simple simple coding

Code:
Private Sub Timer1_Timer()
Dim SavedString As String
Seconds.Text = Seconds.Text + 1
If Seconds.Text = 60 Then
    Minutes.Text = Minutes.Text + 1
    Seconds.Text = 0
    If Minutes.Text = 60 Then
        Hours.Text = Hours.Text + 1
        Minutes.Text = 0
        If Hours.Text = 24 Then
            Days.Text = Days.Text + 1
            Hours.Text = 0
        End If
    End If
End If
SavedString = Days.Text & " Days - " & Hours.Text & " Hours " & Minutes.Text & " Minutes " & Seconds.Text & " Seconds"
FileNum = FreeFile
Open "C:\Server.txt" For Output As FileNum
Print #FileNum, SavedString
Close FileNum
End Sub

i have some other timer stuff and things like that but theres the gist... now just make it an EXE and have it startup as the servers do... Money
 
Let's use some simpler coding. First, read in a start value. This value might be the last time the server crashed, for example. That would give you a running total of the time the server has been running without crashing. It's better to run the timer every millisecond so that the seconds will run smoothly, however, let's say you only want to save the file every second to reduce overhead.
Code:
Dim StartValue as Date

Private Sub Form_Load()
'(Read the startvalue from a log file)
End Sub

Private Sub Timer1_Timer()
static TimesRun as integer
TimesRun = TimesRun + 1
lblSeconds.Caption = CStr(DateDiff("s", StartValue, Now))
lblMinutes.Caption = CStr(DateDiff("n", StartValue, Now))
lblHours.Caption = CStr(DateDiff("h", StartValue, Now))
lblDays.Caption = CStr(DateDiff("d", StartValue, Now))
If TimesRun < 1000 then 
    Exit Sub
End If
TimesRun = 0
FileNum = FreeFile
Open "C:\Server.txt" For Output As FileNum
Print #FileNum, txtDays.Text & " Days - " & txtHours.Text & " Hours " & txtMinutes.Text & " Minutes " & txtSeconds.Text & " Seconds"
Close FileNum
End Sub

See if you like that, and feel free to post back with questions.

Bob
 
here is what i have for saving the file. I did it this way to if the server crashes i have either 4 to 7 days to get to it.
Code:
If Days.Text = 0 And Hours.Text = 0 And Minutes.Text = 0 And Seconds.Text = 5 Then
    Started = "Started: " & Date & " - " & Time
Else
End If
If Days.Text = 0 And Days.Text <= 4 Then
    SavedString = Days.Text & " Days - " & Hours.Text & " Hours " & Minutes.Text & " Minutes " & Seconds.Text & " Seconds" & vbCrLf & vbCrLf & Started & vbCrLf & vbCrLf & "Ended: " & Date & " - " & Time
    FileNum = FreeFile
    Open "C:\ServerL4.txt" For Output As FileNum
    Print #FileNum, SavedString
    Close FileNum
ElseIf Days.Text > 4 And Days.Text <= 7 Then
    SavedString = Days.Text & " Days - " & Hours.Text & " Hours " & Minutes.Text & " Minutes " & Seconds.Text & " Seconds" & vbCrLf & vbCrLf & Started & vbCrLf & vbCrLf & "Ended: " & Date & " - " & Time
    FileNum = FreeFile
    Open "C:\ServerG4L7.txt" For Output As FileNum
    Print #FileNum, SavedString
    Close FileNum
ElseIf Days.Text > 7 Then
    SavedString = Days.Text & " Days - " & Hours.Text & " Hours " & Minutes.Text & " Minutes " & Seconds.Text & " Seconds" & vbCrLf & vbCrLf & Started & vbCrLf & vbCrLf & "Ended: " & Date & " - " & Time
    FileNum = FreeFile
    Open "C:\ServerG7.txt" For Output As FileNum
    Print #FileNum, SavedString
    Close FileNum
End If

however that "Started" disappears after 5 seconds. I need it to store it permanently so i can look at it and see when the computer was up and when it went down. Does anyone know how to store the "current" date and time to a string that doesn't update.
 
What do you mean by string that doesn't update? The simplest answer to your question would be mystring = now. To keep it from updating, just don't update it.

But I don't believe I'm getting at what you're asking, becuase that's too obvious to need a question. So, please clarify.

Now, I'm sorry, but your code is a nightmare for whomever comes after you to maintain it. (DON'T tell me that nobody will, you don't know that! :)) You could simplify it a great deal just by pulling out the repetitive bits, so:
Code:
dim FileName as String
Select Case Days.Text
   Case 0 To 4
      FileName = "C:\ServerL4.txt"
   Case 4 To 7
      FileName = "C:\ServerG4L7.txt"
   Case Else
      FileName = "C:\ServerG7.txt"
End Select
SavedString = Days.Text & " Days - " & Hours.Text & " Hours " & Minutes.Text & " Minutes " & Seconds.Text & " Seconds" & vbCrLf & vbCrLf & Started & vbCrLf & vbCrLf & "Ended: " & Date & " - " & Time
FileNum = FreeFile
Open FileName For Output As FileNum
Print #FileNum, SavedString
Close FileNum

Furthermore, it would be much better for you to use standard naming conventions for your controls. Text boxes should begin with txt and labels with lbl, for example your "Seconds" text box ought to be "txtSeconds". Finally, why are you using text boxes anyway, rather than labels? There's nobody going to write in them, and furthermore, if they do, they could mess up your code since your code depends on their contents.
 
<Microsoft, you say.

Putting words in my mouth, eh? Microsoft I do NOT say! :)

As you know, I could provide numerous pages advocating Hungarian notation, and most of them not from Microsoft. Here's one that happens to be from Microsoft: Granted, this page is 4 years old and granted, 4 is a number, not a very big one of course, but a number all the same.

Having now looked at the page you provided, I now take keyboard in hands again after having spent 15 minutes in profound meditation to recover from the shock--the shock engendered by realizing that you have supported your point of view using a web page which screams ".Net Framework Developer Center" from the top. [surprise] [Et tu, strongm?]

Ok, all kidding aside. Personally, I adhere to the notation for controls, and not for anything else. I wouldn't do this in .Net, and in fact it has fallen out of favor in .Net (the vast number of classes make it impractical), as the page you share suggests. I still think it useful to let people coming after me know that a control is a control, but perhaps this advice is very conservative.

On the other hand, the fact that Microsoft has reversed its position about what is best practice in the matter is suspicious to me, given the extent of their machinations to suppress VB6 in favor of .Net. It seems to me that if the use of Hungarian notation has fallen out of favor in the .Net community, that Microsoft would cheerfully reverse its position thereon and advocate the .Net habits as best practice overall.

I do, however, value your opinions. Please feel free to share your personal opinion on the matter, for it will certainly carry more weight than does Microsoft's with me.
 
i usually do do the txt"name" lbl"name" cmd"name" etc. Sorry for not doing it in this program just whipping something together.

I fixed the time updating. I had it in the timer loop so I just put it in form load and dimmed it as a declared string. This made it work great.

So now my program does the following

1-4 days - ServerL4.txt
5-7 days - ServerG4L7.txt
8+ - SeverG7.txt

It stores the Current run time. The start Date and Time and The time the server crashes.

All in all it works good.

Thanks for all the help guys you rock!
 
You're welcome. However, I'd like to mention that it's important to trim out the fat from your programs. If you get in the habit of looking for more concise ways to do things, you learn more, your programs run more efficiently, and most importantly it's easier for others to figure out what you're doing. I suggest that you keep all that in mind as you progress.

In other words, it's not enough to learn the moves, you must also learn how to play. :)
 
>this page is 4 years old

It's somewhat older than that. That's just when it was last reviewed for correctness. It's about 10 years old, to my knowledge. And the page I pointed you to is actually 'only' 6 to 7 years old. So it is Microsoft's updated position. And no, I haven't gone to the dark side in relation to .NET, it's just that's where Microsoft's current programming standards are laid out (plus they'd have problems giving that advice for the Win32 API, since that is already fully documented using Systems Hungarian)

And since Microsoft (well, their chief architect at the time, Charles Simonyi) invented (back in the days of DOS) and publicised (somewhat later) Hungarian notation - although probably the biggest influence was Charles Petzold on Windows programmers - I'm happy to accept that they've figured out that it is somewhat less useful nowadays in the days of intelligent IDEs.

I still happen to (generally) use HN myself out of habit, but I don't evangalise that others should use it (I'll happily argue that development houses and individuals should develop and use standardsm, but I don't really care what that standard is. In other words I don't have any disagreement with your assertion that 'Furthermore, it would be much better for you to use standard naming conventions for your controls', I just suggest that Hungarian isn't the only option.
 
Personally I still like to use "txt" and "cbo" and such when reading my code. Sure, Visual Studio will tell me it's a textbox if I hover the mouse over it, but that's kind of like reading with your finger on the page.

 
Yea, I programmed in high school and i'm an MIS major (sophomore) so I am in a MIS job 3 years before i'm suppose to be looking. So the advice is priceless and I appreciate all of it. I'm just glad all you guys are here to step it up and kick me in line!

Thanks again and I'll work on the code!

- Matt

"Never Give a Sword to a Man Who Can't Dance
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top