I want to stop users from being able to save a duplicate record in my Access database. I thought of using RecordsAffected, but I can't figure out how to get it to work.

SelectSearchString = "SELECT * FROM Breakdown WHERE BFrom = " & txtFrom.Text ' & " AND BTo = " & txtTo.Text
Dim Con As New OleDbConnection(BCalc_ConnectionString)
Dim Search As New OleDbCommand(SelectSearchString, Con)

That's how I've started. I want to be able to use the result of the RecordsAffected as a parameter in an IF statement so that

IF Search.records affected = 0 THEN Insert records
          ELSE Display msgbox telling user that duplicate records aren't allowed.

Any help would be appreciated

Hi,

Use DataReader which is very fast. Check This Code:

Dim cn As OleDbConnection
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\MyDB.mdb;")
cn.Open()
cmd = New OleDbCommand("SELECT * FROM Breakdown WHERE BFrom = '" & txtFrom.Text & "'", cn)
dr = cmd.ExecuteReader
If dr.Read()
  MsgBox "Matching Records Found"
  'write whatevr code u want here
Else
  MsgBox "Data not found"
End If
dr.Close()
cn.Close()

Regards
Veena

commented: Good suggestion +3

Thanks! I had the right idea - I had started thinking about using a DataReader, but I couldn't figure out the condition for the IF statement, the

IF dr.read() THEN

was exactly what I needed!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.