title: Insert Date in TextBox - VBA
body: <p>I know that we can use function <strong><em>Date</em></strong> in forms for the insertion of the date. But for some dates (such as Hijri Shamsi and Hijri lunar history, etc.), this is impossible and difficult. So I wrote a code that works with the text box. But I think the code that I wrote can be simpler. Do you have a solution to make it simpler?
  For example: checking the slash or preventing of Double message display for the moon and day error.</p>
  
  <p><em>Thanks in advance for the friends who respond.</em></p>
  
  <pre><code>Private Sub TextBox1_Change()
      'To check the slash in the correct place
      If Mid(TextBox1, 1) = "/" Or Mid(TextBox1, 2) = "/" Or Mid(TextBox1, 3) = "/" Or Mid(TextBox1, 4) = "/" Or Mid(TextBox1, 6) = "/" Or Mid(TextBox1, 7) = "/" Or Mid(TextBox1, 9) = "/" Or Mid(TextBox1, 10) = "/" Then
          MsgBox "Please insert date with format: YYYYMMDD or YYYY/MM/DD"
          SendKeys ("{BACKSPACE}")
      End If
      'Insert the slash automatically
      If TextBox1.TextLength = 8 Then
          Me.TextBox1.Value = Format(Me.TextBox1.Value, "0000/00/00")
      End If
  
      'Year Error!
      If Mid(TextBox1, 4) = 0 Then
          MsgBox "Year Error!" &amp; vbNewLine &amp; "Please insert date with format: YYYYMMDD or YYYY/MM/DD"
          With TextBox1
              .SelStart = 0
              .SelLength = Len(.Text)
          End With
          Exit Sub
      End If
      'Month Error!
      If TextBox1.TextLength = 10 Then
          If Mid(TextBox1.Value, 6, 2) = 0 Or Mid(TextBox1.Value, 6, 2) &gt; 12 Then
              MsgBox "Month Error!" &amp; vbNewLine &amp; "Please insert date with format: YYYYMMDD or YYYY/MM/DD"
              With TextBox1
                  .SelStart = 5
                  .SelLength = 2
                  '.SelText = ""
              End With
              Exit Sub
          End If
      End If
      'Day Error!
      If TextBox1.TextLength = 10 Then
          If Mid(TextBox1.Value, 9, 2) = 0 Or Mid(TextBox1.Value, 9, 2) &gt; 31 Then
              MsgBox "Day Error!" &amp; vbNewLine &amp; "Please insert date with format: YYYYMMDD or YYYY/MM/DD"
              With TextBox1
                  .SelStart = 8
                  .SelLength = 2
              End With
              Exit Sub
          End If
      End If
  End Sub
  
  Private Sub textbox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
      'Accept only number and slash
      If Not Chr(KeyAscii) Like "[0-9,/ ]" Then
          KeyAscii = 0
          MsgBox "Only Numbers Allowed!" &amp; vbNewLine &amp; "Please insert date with format: YYYYMMDD or YYYY/MM/DD"
          With TextBox1
              .SetFocus
              Exit Sub
          End With
      End If
  End Sub
  </code></pre>