Actually I'm doing a project to maintain the company info, here i wanted to write a code to check whether the company id already exists or not. that is if the id 123 is already present in the access database then it shuld show the msg that it already exist. Please can any one help me in this.

Recommended Answers

All 3 Replies

make your company id primery key in database.
Now add the follwing code to your save button

Try
'Initialize
factory,
connectionstring,
adapter,
command , etc.
doInsert()
write insert commond here

Catch ex As
Exception

If ex.Message.
IndexOf("Company_ID") Then
MsgBox
("Duplicate data
entry found")
Else
MsgBox("An
unexpected error
occured: " &
ex.Message)
End If
End Try

Do a query to select the record with that ID. If it returns a record then you know that it exists.

Dim myID As String = "123"
query = "SELECT * FROM myTable WHERE company_ID = '" & myID & "'"

or even

query = "SELECT COUNT(*) FROM myTable WHERE company_ID = '" & myID & "'"

To return only the number of records found. How you execute the query depends on the method used to access the database (SQL, ADO, OLEDB).

thank you for helping me in this

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.