473,383 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Edit a Record, Check for duplication prior updating to the table

6
Hi All

I have been looking around for help, and finally post this problem.

I created a form to edit a record. Before i can click save button on the edit form i need to check if the data that have been edit exists in the table. But if i m calling the same record and updating the same record without changing anything it should allowed to save.

Example: I m creating a table to save all types of courses (course_id, course_code, course_description)

On the Edit Course form, when click Save button, in the code screen i check for
(1) Is null (incase the user try to erase the value in course_code and try to save a blank code.
(2) Next i need to check if the new code is exists in the table, if exits do not allow to save reminder that duplicate code.
(3) If the code is same, which mean if i change the course description, it should allow to save.

My Code:

Private Sub btnSave_Click()
On Error GoTo Err_btnSave_Click

Dim criteria As String
criteria = "[course_code] = '" & Me.course_code.Value & "'"
' Comments: Checking all the fields are mandatory during creation of Course.
If IsNull(Me![course_code]) Then
MsgBox "Mandatory Field: Course Code."
ElseIf DCount("course_code", "s_courses", _
criteria) > 0 Then
'Undo duplicate entry
Me.Undo
'Message box warning of duplication
MsgBox "Duplicate code."
ElseIf IsNull(Me![course_name]) Then
MsgBox "Mandatory Field: Course Name."
Else
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
DoCmd.Close
End If

Exit_btnSave_Click:
Exit Sub

Err_btnSave_Click:
MsgBox Err.description
Resume Exit_btnSave_Click

End Sub




Please help.
Gurmet
Aug 27 '08 #1
1 2001
gurmet
6
Hi Everybody
As i have dateline to complete this, i have been searching and chit chat with my friend and found a solution. Just to share with others on how this was resolved:

(1) Create a query to check

SELECT s_courses.course_id
FROM s_courses
WHERE s_courses.course_code=Forms!s_courses_edit!course_ code And s_courses.course_id<>Forms!s_courses_edit!course_i d;


(2) Create a additional function to include the above query in the code to check if the existing count

Private Function CheckExistingCourse() As Integer
Dim db As Database
Dim rs As Recordset
Dim strSQL As String
Dim existingCourseCount As Integer
Dim queryDefs As Variant

Set db = CurrentDb()
Set queryDefs = db.queryDefs("course_edit_q1")
queryDefs(0) = course_code
queryDefs(1) = course_id

Set rs = queryDefs.OpenRecordset(dbOpenSnapshot)
On Error Resume Next
rs.MoveLast
existingCourseCount = rs.RecordCount
rs.Close
Set db = Nothing
CheckExistingCourse = existingCourseCount
End Function


(3) Modify the save function.

Private Sub btnSave_Click()
On Error GoTo Err_btnSave_Click

' Using private function to check
existingCourseCount = CheckExistingCourse

' Validate input during editing a record before updating
If IsNull(Me![course_code]) Then
MsgBox "Mandatory Field: Course Code."
ElseIf IsNull(Me![course_name]) Then
MsgBox "Mandatory Field: Course Name."
ElseIf existingCourseCount > 0 Then
MsgBox "The course code already exits!!"
Else
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
DoCmd.Close
End If

Exit_btnSave_Click:
Exit Sub

Err_btnSave_Click:
MsgBox Err.description
Resume Exit_btnSave_Click

End Sub

At last, this is resolved, and hope it helps others.

Regards
Gurmet
Aug 27 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

25
by: dixie | last post by:
I have some code that adds new records into a table for each ID in a list box when a button on a form is clicked. This works fine. My problem now is that I wish to be able to edit all the records...
3
by: pmud | last post by:
Hi, I have an ASP.NET application using C# code. I am using a datagrid to display records from a database based on a user input, i.e a user enters a compnay name in text box & when he clicks a...
0
by: tone | last post by:
i have just changed a field so that it automatically grabs x/y coord for a site from an arcmap doc. problem is that as it does this it creates a new record for each site instead of updating the...
8
by: =?Utf-8?B?bWlrZWc=?= | last post by:
Hi, I am building a small Help Desk application for my company and need to be able to edit "open" help desk issues. I use a simple datagrid to display each issue (6 per page) , with an Edit...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.