Consulting

Results 1 to 5 of 5

Thread: Input box Data entered need code to state you’ve entered Wrong data try again

  1. #1
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location

    Input box Data entered need code to state you’ve entered Wrong data try again

    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
    Last edited by Aussiebear; 03-06-2023 at 05:36 PM. Reason: Adjusted the Code tags

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    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
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location
    Thank you!

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,057
    Location
    If the issue has been solved, would you kindly mark it as so by using the Thread Tools options please?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  5. #5
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location
    Thank you!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •