G
Guest
I have tried, unsuccessfully, to revise the code below so that when I enter a
date in the StartDate field, an input box will come up and ask me how many
days I want to add or subtract, then it will populate the FutureDate field.
I have tried putting this in the AfterUpdate for the StartDate field.
Anyway, obviously I am doing something wrong. I think it is the problem is
with the code: Selection.InsertBefore Format((Date1 + myValue), Mask). Can
someone help me? My aspiration is to then have another input box come up and
ask me how many months to add or subtract from the date and then a third
input box come up and ask me how many years to I want to add or subtract.
Private Sub StartDate_AfterUpdate()
Dim Message As String
Dim Mask As String
Dim Title As String
Dim Default As String
Dim Date1 As String
Dim MyText As String
Dim Var1 As String
Dim Var2 As String
Dim Var3 As String
Dim Var4 As String
Dim Var5 As String
Dim Var6 As String
Dim Var7 As String
Dim Var8 As String
Dim Date2 As String
Dim Date3 As String
Dim myValue As Variant
Dim StartDate As String
StartDate = Me.ActiveControl
Date = Me.StartDate
''Date mask below includes non-breaking spaces (Chr(160))
Mask = "mm/" & Chr(160) & "dd/" & Chr(160) & "yy" ' Set Date format
Default = "14" ' Set default.
Title = "Plus or minus date starting with " & Format(Date, Mask)
Date1 = Format(Date, Mask)
Var1 = "Enter number of days by which to vary above date. " _
& "The number entered will be added to "
Var2 = Format(Date + Default, Mask) ' Today plus default (14)
Var3 = Format(Date - Default, Mask) ' Today minus default (14)
Var4 = ". The default ("
Var5 = ") will produce the date "
Var6 = ". Minus (-"
Var7 = ". Entering '0' (zero) will insert "
Var8 = " (today). Click cancel to quit."
MyText = Var1 & Date1 & Var4 & Default & Var5 & Var2 & Var6 _
& Default & Var5 & Var3 & Var7 & Date1 & Var8
'
' Display InputBox and get number of days
GetInput:
myValue = InputBox(MyText, Title, Default)
'
If myValue = "" Then
End 'quit subroutine
End If
'
On Error GoTo Oops 'just in case
Selection.InsertBefore Format((Date1 + myValue), Mask)
Selection.Collapse (wdCollapseEnd)
End 'End subroutine
'
Oops: ' error handler in case user types something other than a number
'
MsgBox prompt:="Sorry, only a number will work, please try again.", _
Buttons:=vbExclamation, _
Title:="A number is needed here."
GoTo GetInput
End Sub
Thanks in advance.
date in the StartDate field, an input box will come up and ask me how many
days I want to add or subtract, then it will populate the FutureDate field.
I have tried putting this in the AfterUpdate for the StartDate field.
Anyway, obviously I am doing something wrong. I think it is the problem is
with the code: Selection.InsertBefore Format((Date1 + myValue), Mask). Can
someone help me? My aspiration is to then have another input box come up and
ask me how many months to add or subtract from the date and then a third
input box come up and ask me how many years to I want to add or subtract.
Private Sub StartDate_AfterUpdate()
Dim Message As String
Dim Mask As String
Dim Title As String
Dim Default As String
Dim Date1 As String
Dim MyText As String
Dim Var1 As String
Dim Var2 As String
Dim Var3 As String
Dim Var4 As String
Dim Var5 As String
Dim Var6 As String
Dim Var7 As String
Dim Var8 As String
Dim Date2 As String
Dim Date3 As String
Dim myValue As Variant
Dim StartDate As String
StartDate = Me.ActiveControl
Date = Me.StartDate
''Date mask below includes non-breaking spaces (Chr(160))
Mask = "mm/" & Chr(160) & "dd/" & Chr(160) & "yy" ' Set Date format
Default = "14" ' Set default.
Title = "Plus or minus date starting with " & Format(Date, Mask)
Date1 = Format(Date, Mask)
Var1 = "Enter number of days by which to vary above date. " _
& "The number entered will be added to "
Var2 = Format(Date + Default, Mask) ' Today plus default (14)
Var3 = Format(Date - Default, Mask) ' Today minus default (14)
Var4 = ". The default ("
Var5 = ") will produce the date "
Var6 = ". Minus (-"
Var7 = ". Entering '0' (zero) will insert "
Var8 = " (today). Click cancel to quit."
MyText = Var1 & Date1 & Var4 & Default & Var5 & Var2 & Var6 _
& Default & Var5 & Var3 & Var7 & Date1 & Var8
'
' Display InputBox and get number of days
GetInput:
myValue = InputBox(MyText, Title, Default)
'
If myValue = "" Then
End 'quit subroutine
End If
'
On Error GoTo Oops 'just in case
Selection.InsertBefore Format((Date1 + myValue), Mask)
Selection.Collapse (wdCollapseEnd)
End 'End subroutine
'
Oops: ' error handler in case user types something other than a number
'
MsgBox prompt:="Sorry, only a number will work, please try again.", _
Buttons:=vbExclamation, _
Title:="A number is needed here."
GoTo GetInput
End Sub
Thanks in advance.