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

Assigning current time to a text box

Status
Not open for further replies.

mans

Programmer
Mar 18, 2000
136
0
0
AU
Hello,

I am using VB6 SP5 on XP. I would like to display the current time in a text box at run time, when I attempt to an error message says: "Compile Error, Expected Variable or Procedure, Not Project" and it highlights the following statement, Text1.text = TIME (design time). I have been using this simple statement in '98 for many years, what is likely to have changed or what do I need to do.

Thank You
 
Did you check the statement AFTER the one that is highlighted?

Where is this code placed in the load or activate?
 
The easiest way I know of is to put a Timer on the form, then use this
Private Sub Timer1_Timer()
Text1.Text = Time()
End Sub
 
oopppsss forgot ..
Set the timer Interval property to 100
 
Does it have to be a TextBox? If you just want time displayed you might consider using a Statusbar control with the visible panel style set to sbrTime (5)
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Mans,

Assuming that all you want do is display the time of day, I don't undertand the tangents about a timer function.

Try either of these:

Text1.Text = Format$(Now(), "hh:mm AM/PM")
or
Text1.Text = Format$(Time(), "hh:mm AM/PM")

The Now() function is the only one I use because the other can be used to set the System Time on the user's PC.

BTW, a TextBox control carries an awful lot of overhead. Unless you want the user to be able write something in the control, I recommend a Lable with the BorderStyle set to 1 - Fixed Single and the BackColor set to &H00FFFFFF& (White). It will look like a TextBox, but consumes fewer system resorces and the user wont be misled into believing that he or she can change the time.
 
jppttawa ...
your code displays the time that the program loaded but does not update
After reading all posts I have to say that Johnwm is definatly on the right track. Use the proper control whenever possible, and in this case the status bar is the right control.
 
I answered the question asked, which is how the get the time of day without getting an error. I did not address refreshing the information. I guess I a nerve when I referred the Timer control as a tangent. Sorry, no insult intended.

But... Well, Timer controls are even more expensive then TextBoxes and they have been the source of many a programming bug. I learned back in VB3 days to get away from them whenever possible. Prior to VB5 introducing AddressOf for call-backs, I wrote DLL's in C to access the API and give back the time. Now it is much easier.

The following is borrowed from Balena's "Programming Visual Basic 6.0".

Create a BAS module with the following:

Code:
 'The following is based on Franceso Balena's CallBack example in
'"Programming Visual Basic 6.0" published by Microsoft Press
Option Explicit

Private Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Private Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long

Private timerID As Long

Public Sub StartTimer(ByVal timeout As Long)
    If timerID = 0 Then
        timerID = SetTimer(0, 0, timeout, AddressOf Timer_CBK)
    End If
End Sub

Public Sub StopTimer()
    If timerID Then
        KillTimer 0, timerID
        timerID = 0
    End If
End Sub

Private Sub Timer_CBK(ByVal hWnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal SysTime As Long)
    ' Just display the system time in a label control.
    Form1.lblTimer = Format$(Now(), "hh:mm:ss") ' SysTime
End Sub

Call StartTimer with long specifying the number of milliseconds to delay the call-back.
In Timer_CBK() set the targeted form and control to taste.
Use StopTimer to kill the process if and when it is no longer needed.
 
The Timer in VB5/6 is a much better-behaved beast than in days of yore, and is really only a thin wrapper SetTimer No particular need to avoid them, frankly.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top