You need to add this code in the TextChanged Event.
In the event write your code
VB
Private Sub txtCode_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtCode.TextChanged
Dim sqlQRY As String = "SELECT COUNT(*) AS numRows from STOCK WHERE CODE = @Code "
MyCn.Open()
Dim queryResult As Integer
Dim com As New OleDbCommand(sqlQRY, MyCn)
com.Parameters.AddWithValue("@Code", Me.txtCode.Text.Trim())
queryResult = com.ExecuteScalar()
MyCn.Close()
If queryResult > 0 Then
MessageBox.Show("Data Already Exists!", "REMINDER ", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
End Sub
C#
private void txtCode_TextChanged(System.Object sender, System.EventArgs e)
{
string sqlQRY = "SELECT COUNT(*) AS numRows from STOCK WHERE CODE = @Code ";
MyCn.Open();
int queryResult = 0;
OleDbCommand com = new OleDbCommand(sqlQRY, MyCn);
com.Parameters.AddWithValue("@Code", this.txtCode.Text.Trim());
queryResult = com.ExecuteScalar();
MyCn.Close();
if (queryResult > 0) {
MessageBox.Show("Data Already Exists!", "REMINDER ", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
}