Help Please--record finding in access db
hi :SICK:
i have MS access database with a "itmname" field
and a form with a textbox and a button
i want a code to check the database before add new record if
name in textbox already exist in database raise a message like
"name already exists" else add name on textbox in database.
im begginer and please write codes in vb6
thanks :WAVE:
[386 byte] By [
x-pc] at [2007-11-11 10:21:14]

# 1 Re: Help Please--record finding in access db
Add a reference in your project (Project -> References...) to Microsoft DAO Object Library. Then do something like this:
If Not SaveItemName(txtItemName.Text) Then
' Name already exists
End If
Function SaveItemName(ByVal ItemName As String) As Boolean
Dim db As Database
Dim rs As Recordset
Dim result As Boolean
Set db = OpenDatabase("d:\path\filename.mdb")
' Get number of records whose itmname field
' matches ItemName parameter value
Set rs = db.OpenRecordset("SELECT COUNT(*) FROM TableName WHERE itmname = '" & ItemName & "'")
' If count > 0, name already exists
If rs(0) > 0 Then
result = False
Else
' If name not found, insert new ItemName
db.Execute "INSERT INTO TableName (itmname) VALUES ('" & ItemName & "')"
result = True
End If
' Clean up
rs.Close
db.Close
' Return success or failure
SaveItemName = result
End Function