title: How to fix this Overflow Exception was unhandled / Arithmetic operation resulted in an overflow ( d = d +1 part of the code )
body: <p>I'm getting an <code>OverflowException</code> in my code below.</p>
  
  <blockquote>
    <p>Arithmetic operation resulted in an overflow.</p>
  </blockquote>
  
  <pre><code>Private Sub txtSearch_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtSearch.KeyPress
      Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
      d = 0
      c = 0
      Dim strTitle As String
      Dim b As Short
      Dim length As Short
      length = Len(txtSearch.Text)
  
      If KeyAscii = 13 Then
          rf.MoveFirst()
          If txtSearch.Text = "" Then
              MsgBox("Search box empty.", MsgBoxStyle.Information)
              txtSearch.Focus()
          Else
              Do Until rf.EOF
                  strTitle = rf.Fields("Title").Value
                  For b = 1 To Len(strTitle) - length
                      If Mid(strTitle, b, length) = txtSearch.Text Then
                          d = d + 1
                          Exit For
                      End If
                  Next
                  rf.MoveNext()
              Loop
              lblTotal.Text = "" &amp; d
              If d = 0 Then
                  MsgBox("Keyword not found.", MsgBoxStyle.Critical)
                  txtSearch.Text = ""
                  txtSearch.Focus()
              Else
                  rf.MoveFirst()
                  Do Until rf.EOF
                      strTitle = rf.Fields("Title").Value
                      For b = 1 To Len(strTitle) - length
                          If Mid(strTitle, b, length) = txtSearch.Text Then
                              d = d + 1 '&lt;-- Error occurs here.
                              Exit For
                          End If
                      Next
                  Loop
              End If
          End If
      End If
      eventArgs.KeyChar = Chr(KeyAscii)
      If KeyAscii = 0 Then
          eventArgs.Handled = True
      End If
  End Sub
  </code></pre>