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

Adding a number to a date field 2

Status
Not open for further replies.

ghost807

IS-IT--Management
Jun 27, 2003
99
US
i can't seem to figure out how to add a number to a date field. i am working(still) on an app that will logoff the current user when time expires, but the time is based on the local time plus (dollarAmount/Multiplier)
i can get the app to recogonize both but i cannot add them together. i have even(i read in the SDK&Help)made this a fraction(for minutes) but i always get the same response. TYPE MISMATCH. Any help would be appreciated.
 
Option Explicit
Private Declare Function GetComputerName Lib "kernel32.dll" Alias _
"GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Dim AlarmOn As Boolean
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Const GW_HWNDFIRST = 0
Const GW_HWNDNEXT = 2


Private Sub Form_Load()
AlarmOn = True
LoadTaskList

End Sub

Sub OnButton_Click(Index As Integer)
If (Index = 0) Then
AlarmOn = True
Else
AlarmOn = False
End If
End Sub

Sub Timer1_Timer()
Dim Alarm1
Dim Dollars1
Dim Multiplier1
Dim Intern
Dim tme
Dim Count
Dim Test

tme = Format("HH:MM")
Alarm1 = Format("HH:MM:SS")
Alarm1 = AlarmSetting
tme = time1
Dollars1 = Dollars
Multiplier1 = Multi
___________________________________________________________
tme = (Time$ + (1 / (Dollars1 / Multiplier1))) '+Alarm1
'This is where i get the TYPE Mismatch
___________________________________________________________
Display.Caption = Format(Now, "HH:MM")

If (tme > AlarmSetting.Text And AlarmOn) Then
' Shell "C:\VBBB\alarm\shutdown.exe -l -f -t 30:", vbHide
MsgBox "The computer is going to Shutdown in 30 sec.", vbOKOnly
AlarmOn = False
Else
' Shell "C:\VBBB\ALARM\SHUTDOWN.EXE -C"
End If
End Sub

Private Sub Command1_Click()
Dim strCompName As String
Dim rVal As Long
Dim lngLen As Long
strCompName = Space(255)
lngLen = 255
rVal = GetComputerName(strCompName, lngLen)
MsgBox Left(strCompName, lngLen)

End Sub


Private Sub Tasks_Click()
LoadTaskList
End Sub

Sub LoadTaskList()
Dim CurrWnd As Long
Dim Length As Long
Dim TaskName As String
Dim Parent As Long
List1.Clear
CurrWnd = GetWindow(Form1.hwnd, GW_HWNDFIRST)
While CurrWnd <> 0
Parent = GetParent(CurrWnd)
Length = GetWindowTextLength(CurrWnd)
TaskName = Space$(Length + 1)
Length = GetWindowText(CurrWnd, TaskName, Length + 1)
TaskName = Left$(TaskName, Len(TaskName) - 1)

If Length > 0 Then
If TaskName <> Me.Caption Then
List1.AddItem TaskName
End If
End If
CurrWnd = GetWindow(CurrWnd, GW_HWNDNEXT)
DoEvents
Wend
End Sub
 
I don't understand the value dolar

Dim sumMin As Integer
sumMin = 20
Text1.Text = Format(Format(Text1.Text, &quot;0.000000&quot;) + (sumMin / 1440), &quot;c&quot;)


peterguhl@yahoo.de
 
sumMin = 20 'the minutes you 'll add
'Cadte give back a Double with you can add a
'value, then you must give a format to see the
'new time I use format &quot;C&quot; because also will be
'value the day change
Text1.Text = Format(CDate(Text1.Text) + (sumMin / 1440), &quot;c&quot;)


peterguhl@yahoo.de
 
Look up the DateAdd and DateDiff functions

________________________________________________________________
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.'
 
Sorry no matter what i do i keep getting type mismatch. i have even started a basic project to just try this.
all i have is a text1 box, the dollarAmount TextBox and the Multiplier TextBox. i just cannot seem to get this to work correctly.
 
The reason you have a mismatch is because you are trying to add two different data types, strings (Time$) and numbers (1 / (Dollars1 / Multiplier1)). You can change the string to a number by using the val function.....

val (time$) val (alarm1) etc...
 
Ok Guys i hope your still willing to help me.i have changed some major things. BUT, i'm still having a problem. if i try to use a whole number it will work correctly, but if i use a decimal point in the number then it seems to divide by the decimal point instead of adding to the time. i'm kinda lost now.
Here's the whole code for your viewing enjoyment.

Option Explicit
Dim AlarmOn As Boolean
Dim Counting
Dim Seconz
Dim CurrentTime As Date

Private Sub Form_Load()
AlarmOn = True
unsure
End Sub

Private Function unsure()
'This was done as a test but may move to onload instead
Counting = 0
Seconz = 0
CurrentTime = Format$(Time$, &quot;hh:mm:ss&quot;)
End Function

Private Function NextAlarmTime() As Date
Dim tme
Dim ALARM_INTERVAL
Dim time_now As Date
Dim h
Dim m
Dim seconds As Long
Dim next_m
currTime.Caption = Format$(Time$, &quot;hh:mm:ss&quot;)

tme = (Amount) / (Multiply)

ALARM_INTERVAL = tme
Test.Caption = tme

' Get the hour and minutes.
h = DatePart(&quot;h&quot;, CurrentTime)
m = DatePart(&quot;n&quot;, CurrentTime)
___________________________________________________________
' Calculate the next multiple of ALARM_INTERVAL.
next_m = (((m \ ALARM_INTERVAL) + 1) * ALARM_INTERVAL)
NextAlarmTime = DateAdd(&quot;n&quot;, h * 60 + next_m, Date)
This is now the problem. i cannot use a number that has a decimal (i.e. 1.5)
___________________________________________________________
End Function
Private Sub tmrAlarm_Timer()
Static next_alarm As Date
Dim secs_to_go As Single
Dim Testing
next_alarm = 0

lblNextAlarm.Caption = Format$(next_alarm, &quot;hh:mm:ss&quot;)
' See if we have initialized next_alarm.
If next_alarm = 0 Then
' Initialize next_alarm.
next_alarm = NextAlarmTime
lblNextAlarm.Caption = Format$(next_alarm, &quot;hh:mm:ss&quot;)
currTime.Caption = Format$(Time$, &quot;hh:mm:ss&quot;)
Data1.Recordset.MoveLast

End If

' See how many milliseconds until the next
' alarm time.
secs_to_go = DateDiff(&quot;s&quot;, Now, next_alarm)

' Stop the timer to clear previous alarms.
tmrAlarm.Enabled = False

' Set the timer's Interval.
If secs_to_go < 25 Then
If (Seconz = 0) Then
GoodBye.Caption = &quot;&quot;
' Fire the alarm.
Shell &quot;C:\VBBB\alarm\shutdown.exe -l -f -t 30:&quot;, vbHide
Shell &quot;c:\Alarm\end.bat&quot;, vbHide
GoodBye.Caption = &quot;The Computer is going to ShutDown in 30 Sec.&quot;
' Reinitialize next_alarm.
next_alarm = NextAlarmTime
tmrAlarm.Enabled = True
Seconz = 1
End If

ElseIf secs_to_go < 60 Then
If (Counting = 0) Then
GoodBye.Caption = &quot;The Computer is going to ShutDown in 60 Sec.&quot;
Counting = 1
End If
Else
End If

' Restart the timer.
tmrAlarm.Enabled = True
lblNextAlarm.Caption = Format$(next_alarm, &quot;hh:mm:ss&quot;)
currTime.Caption = Format$(Time$, &quot;hh:mm:ss&quot;)

End Sub

Thanx again guys.
 
The 'number' parameter of the DateAdd function will only accept a Long. If passed a decimal value it will round down. Thus sayeth VBHelp.

Multiply your number by 60 and use seconds as the interval.

________________________________________________________________
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.'
 
'Test this but you must complete it

Option Explicit
Dim AlarmTime As Date

'Form with one label Named currtime
'and one time-object named tmrAlarm

Private Sub shutdown()
Shell &quot;C:\VBBB\alarm\shutdown.exe -l -f -t 30:&quot;, vbHide
Shell &quot;c:\Alarm\end.bat&quot;, vbHide
End Sub

Private Sub Form_Load()
tmrAlarm.Interval = 1000 ' one second
NextAlarm
End Sub

Private Sub tmrAlarm_Timer()
currTime.Caption = Time()
Me.Caption = &quot;The Computer is going to ShutDown in &quot; & Format(Time() - AlarmTime, &quot;hh:mm:ss&quot;)
If Format(Time() - AlarmTime, &quot;hh:mm:ss&quot;) = CDate(0) Then shutdown
End Sub

Private Sub NextAlarm()
Dim Alarminterval
Alarminterval = 60 'here you have to calculate your
'Alarmsequenz in seconds -> for example
MsgBox (Alarminterval / 86400)
AlarmTime = Format(Time() + CDate((Alarminterval / 86400)), &quot;hh:mm:ss&quot;)
End Sub


peterguhl@yahoo.de
 
Try something like this....

Private Sub Command1_Click()
Dim dollars1
Dim multiplier1
dollars1 = 5
multiplier1 = 3
Debug.Print (Right(Format(Now, &quot;short time&quot;), 2)) + ((Left(Format(Now, &quot;short time&quot;), 2)) * 60) + (1 / (dollars1 / multiplier1))
End Sub
 
ok here's what i ended up with. i had to keep refreshing the data incase it gets changed.
i also had to fill the text boxes before the calculations otherwise i would get a type mismatch.
i had to put in some counters to get things to only run once per session. and also the ability to logoff the user if they didn't have a valid time in the database.


Option Explicit
Dim AlarmOn As Boolean
Dim AlarmTime As Date
Dim CurrentTime As Date
Dim Counting
Dim Seconz
Dim Quitting


Private Sub Form_Load()
AlarmOn = True
unsure
tmrAlarm.Interval = 1000 ' one second
NextAlarm
End Sub

Private Function unsure()
Counting = 0
Seconz = 0
Quitting = 0
CurrentTime = Format(Now, &quot;hh:mm:ss&quot;)
End Function

Private Function NextAlarm() As Date
On Error GoTo Shutdown
Dim tme As Double
Dim Alarminterval As Double
Dim A As Double
Dim M As Double
Data1.Refresh
A = Amount.Text
M = Multiply.Text
tme = A / M
Alarminterval = tme
' MsgBox (Alarminterval / 1440)
AlarmTime = Format(CurrentTime + CDate((Alarminterval / 1400)), &quot;hh:mm:ss&quot;)

Always_NextAlarm:
GoTo Exit_NextAlarm
Shutdown:
Shutdown
Exit_NextAlarm:
End Function


Private Sub tmrAlarm_Timer()
Dim secs_to_go As Single
'Dim Testing
currTime.Caption = Format(Time(), &quot;hh:mm:ss&quot;)
lblNextAlarm.Caption = Format(AlarmTime - Time(), &quot;hh:mm:ss&quot;) '&quot;The Computer is going to ShutDown in &quot; &
' See how many milliseconds until the next
' alarm time.
secs_to_go = (((AlarmTime - Time()) * 1440))
Test.Caption = ((secs_to_go) * 60)
If secs_to_go < 0.033 Then
If (Quitting = 0) Then
Beep
GoodBye.Caption = &quot;&quot;
GoodBye.Caption = &quot;The Computer is Now Shutting Down.&quot;
Data1.Recordset.Delete
Quitting = 1
Shutdown
End If
End If
If secs_to_go < 0.5 Then
If (Seconz = 0) Then
Me.Caption = &quot;Goodbye, I'll Miss You.&quot;
GoodBye.Caption = &quot;&quot;
' Fire the alarm.
' *** Do something here ***
Beep
GoodBye.Caption = &quot;The Computer is going to ShutDown in 30 Sec.&quot;
' Reinitialize next_alarm.
'next_alarm = NextAlarmTime
tmrAlarm.Enabled = True
Seconz = 1
End If

ElseIf secs_to_go < 1 Then
If (Counting = 0) Then
Beep
'tmrAlarm.Interval = secs_to_go * 1000
GoodBye.Caption = &quot;The Computer is going to ShutDown in 60 Sec.&quot;
Counting = 1
End If
Else
' tmrAlarm.Interval = 500
End If

' Restart the timer.
tmrAlarm.Enabled = True
NextAlarm
'If Format(Time() - AlarmTime, &quot;hh:mm:ss&quot;) = CDate(0) Then ShutDown
End Sub

Private Sub Shutdown()
Shell &quot;C:\VBBB\alarm\shutdown.exe -l -f -t 30:&quot;, vbHide
Shell &quot;c:\Alarm\end.bat&quot;, vbHide
End
End Sub


Thanx for your help guys.
Heres a star for both of you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top