PDA

View Full Version : [SOLVED:] Input box Data entered need code to state you’ve entered Wrong data try again



estatefinds
03-06-2023, 04:18 PM
Sub Rectangle25_Click()
Dim FirstDate As Date ' Declare variables.
Dim IntervalType As String
Dim Number As Integer
Dim Msg As String
IntervalType = "m" ' "m" specifies months as interval.
FirstDate = InputBox("Enter a date for Example: 03/05/2023")
Number = InputBox("Enter number of months to add for Example: -1 -2 -3 for how many months prior to the date entered, and 1 2 3 for how many months forward")
Msg = "New date: " & DateAdd(IntervalType, Number, FirstDate)
MsgBox Msg
End Sub

Paul_Hossler
03-06-2023, 05:33 PM
I'd add more checks, like making sure the year in in the right range (2022 - 2030 for ex) and that the number of months is reasonable



Option Explicit


Sub Rectangle25_Click()
Dim FirstDate As Variant ' <<<<<<<<<<<<<<<<<
Dim IntervalType As String
Dim Number As Variant ' <<<<<<<<<<<<<<<<<<
Dim Msg As String


IntervalType = "m" ' "m" specifies months as interval.


FirstDate = InputBox("Enter a date for Example: 03/05/2023")

Do While Not IsDate(FirstDate)
MsgBox "Try again"
FirstDate = InputBox("Enter a date for Example: 03/05/2023")
Loop

Number = InputBox("Enter number of months to add for Example: -1 -2 -3 for how many months prior to the date entered, and 1 2 3 for how many months forward")
Do While Not IsNumeric(Number)
MsgBox "Try again"
Number = InputBox("Enter number of months to add for Example: -1 -2 -3 for how many months prior to the date entered, and 1 2 3 for how many months forward")
Loop

Msg = "New date: " & DateAdd(IntervalType, Number, FirstDate)


MsgBox Msg
End Sub

estatefinds
03-06-2023, 05:50 PM
Thank you!:yes

Aussiebear
03-06-2023, 11:16 PM
If the issue has been solved, would you kindly mark it as so by using the Thread Tools options please?

estatefinds
03-07-2023, 05:24 PM
Thank you!:yes