954,582 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Record Already Exists

Can any body tell me how can i check that record already exists?

Naveed_786
Posting Whiz in Training
268 posts since Jul 2010
Reputation Points: 20
Solved Threads: 11
 

Use the same select statement type -

SELECT * FROM student WHERE StudentName LIKE " & "'" & Nav & "%'"


This will return all the names that has Nav in its text.

AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

You can use this code to check wether a query result returns a macthing record or not

Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset

'NOTE: you can use AndreRet's given example above this post...

rs.Open "YOUR SELECT STATEMENT HERE", cn, adOpenKeyset, adLockOptimistc, adCmdText
If rs.BOF and rs.EOF = True then 'if there are no results from the query, then do this (this means that there are no records)

Else 'when the query have return a number of matching records...do this

End if
PoisonedHeart
Junior Poster in Training
57 posts since Jul 2009
Reputation Points: 14
Solved Threads: 14
 
rs.Open "YOUR SELECT STATEMENT HERE", cn, adOpenKeyset, adLockOptimistc, adCmdText

As in -

rs.Open "SELECT * FROM student WHERE StudentName LIKE " & "'" & Nav & "%'", cn, adOpenStatic, adLockOptimistic


.:)

AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

if you want to know if the record is already exist you need to query from your database if that record is already been there example.

RecordSet.Open "Select * from studentTable Where StudenID = '" & txtStudentID.Text & "'", cn, adOpenStatic, adLockOptimistic

and then check if recordcount of recordset is more than 0 example
if recordset.recordcount > 0 then
msgbox "Record already exit.",vbexclamation
exit sub
end if

Hope this simple tutorial really help your prob. :D

devLady
Newbie Poster
9 posts since May 2010
Reputation Points: 10
Solved Threads: 1
 

ooopss sorry, it must be
if recordset.recordcount > 0 then
msgbox "Record already exist.",vbexclamation
exit sub
end if

devLady
Newbie Poster
9 posts since May 2010
Reputation Points: 10
Solved Threads: 1
 

I am using this code on keypress event of txtstudentid please help me how would i check here that record already exists. when i use this it populates message when i click on the textbox

Private Sub txtstudentid_KeyPress(KeyAscii As Integer)
if rs.recordcount > 0 then
msgbox "Record already exist.",vbexclamation
exit sub
If KeyAscii = 13 Then
txtstudentname.SetFocus
If txtstudentid.Text = nul Then
MsgBox "Field Must Be Entered", vbOKOnly + vbExclamation, "EXCELLENT ACADMY"
txtstudentid.SetFocus
End If
End If
End if
End Sub
Naveed_786
Posting Whiz in Training
268 posts since Jul 2010
Reputation Points: 20
Solved Threads: 11
 
'Declare the ADODB objects
Dim cnMyCon As ADODB.Connection
Dim rsMyRec As ADODB.RecordSet

Set a reference to the ADODB that will be used in this context
Set cnMyCon = New ADODB.Connection
Set rsMyRec = New ADODB.RecordSet

'Do your access database connection here.
cnMyCon.Open = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= WhereYourPathOfDatabaseIS\YourDatabaseName.MDB;Persist Security Info=False"

If txtstudentid.Text = nul Then
   MsgBox "Field Must Be Entered", vbOKOnly + vbExclamation, "EXCELLENT ACADMY"
   txtstudentid.SetFocus

   Exit Sub
      Else    
   rsMyRec.Open "SELECT * FROM YourTableName WHERE TheStudentIdNumber = " & "'" & txtstudentid.Text & "'"

   If rsMyRec.BOF = True Or rsMyRec.EOF = True Then
      'Add the new record here because it does not exist
         Else
      msgbox "Record already exist.",vbexclamation
      
      cnMyCon.Close
      rsMyRec.Close
      exit sub
   End If
End IF
AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

Hi dear,

I have resolved the problem by using this code

On Error Resume Next
If KeyAscii = 13 Then
txtstudentname.SetFocus
rs.MoveFirst
While rs.EOF = False
If Val(txtstudentid.Text) = rs.Fields!StudentID Then
MsgBox "RECORD ALREADY EXISTS!", vbOKOnly + vbInformation, "EXCELLENT ACADMY"
txtstudentid.SetFocus
Exit Sub
Else
If txtstudentid = nul Then
MsgBox "Please enter StudentID!", vbOKOnly + vbInformation, "EXCELLENT ACADMY"
txtstudentid.SetFocus
Exit Sub
End If
End If
rs.MoveNext
Wend
End If


Thanks alot for your help.

Naveed_786
Posting Whiz in Training
268 posts since Jul 2010
Reputation Points: 20
Solved Threads: 11
 

It was only a pleasure.:)

I see that you had your txtStudentId set to your datacontrol. Nicely done to compare from there.

Please mark this thread as solved for us, thanks.

Happy coding.:)

AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You