i have created a function to input data from four textboxes into a table. how do i prevent duplicate records of the same mth/Yr into the table? editing of the data in the table with reference to the mt/Yr is allowed, but how to prevent duplicate records? -
Public Sub PutInMonthlyRecords()
-
-
Dim sql As String
-
Dim Db As DAO.Database
-
Dim rs As DAO.Recordset
-
Dim f As Form
-
Dim MthYr As String
-
-
Set f = Forms!frmQpi
-
-
MthYr = f("txtMthYr")
-
-
sql = "SELECT * FROM [tblQpiMonthly] WHERE (([tblQpiMonthly].[Input MthYr] = 'MthYr' ));"
-
-
Set Db = CurrentDb()
-
Set rs = Db.OpenRecordset(sql)
-
-
-
If rs.RecordCount = 0 Then
-
If (f!txtTotalPF) = 0 Then
-
Exit Sub
-
End If
-
-
rs.AddNew
-
rs![Input MthYr] = f("txtMthYr")
-
rs![Monthly TotalPF] = f("txtTotalPF")
-
rs![Monthly Rejection] = f("txtTotalAvoid")
-
rs![Monthly TotalAvoid] = f("txtRejection")
-
-
rs.Update
-
-
Else
-
If (f!txtTotalPF) = 0 Then
-
rs.Delete
-
-
Exit Sub
-
End If
-
-
-
rs.Delete
-
rs.AddNew
-
rs![Input MthYr] = f("txtMthYr")
-
rs![Monthly TotalPF] = f("txtTotalPF")
-
rs![Monthly Rejection] = f("txtTotalAvoid")
-
rs![Monthly TotalAvoid] = f("txtRejection")
-
-
rs.Update
-
-
End If
-
-
-
Db.Close
-
-
11 5261
i have created a function to input data from four textboxes into a table. how do i prevent duplicate records of the same mth/Yr into the table? editing of the data in the table with reference to the mt/Yr is allowed, but how to prevent duplicate records? -
Public Sub PutInMonthlyRecords()
-
-
Dim sql As String
-
Dim Db As DAO.Database
-
Dim rs As DAO.Recordset
-
Dim f As Form
-
Dim MthYr As String
-
-
Set f = Forms!frmQpi
-
-
MthYr = f("txtMthYr")
-
-
sql = "SELECT * FROM [tblQpiMonthly] WHERE (([tblQpiMonthly].[Input MthYr] = 'MthYr' ));"
-
-
Set Db = CurrentDb()
-
Set rs = Db.OpenRecordset(sql)
-
-
-
If rs.RecordCount = 0 Then
-
If (f!txtTotalPF) = 0 Then
-
Exit Sub
-
End If
-
-
rs.AddNew
-
rs![Input MthYr] = f("txtMthYr")
-
rs![Monthly TotalPF] = f("txtTotalPF")
-
rs![Monthly Rejection] = f("txtTotalAvoid")
-
rs![Monthly TotalAvoid] = f("txtRejection")
-
-
rs.Update
-
-
Else
-
If (f!txtTotalPF) = 0 Then
-
rs.Delete
-
-
Exit Sub
-
End If
-
-
-
rs.Delete
-
rs.AddNew
-
rs![Input MthYr] = f("txtMthYr")
-
rs![Monthly TotalPF] = f("txtTotalPF")
-
rs![Monthly Rejection] = f("txtTotalAvoid")
-
rs![Monthly TotalAvoid] = f("txtRejection")
-
-
rs.Update
-
-
End If
-
-
-
Db.Close
-
-
Since you are allowing editing at the Table level, I feel as though your best option may be : in tblOptMonthly, set the Indexed property of the [Input MthY] Field to Indexed = Yes(No Duplicates). In the PutInMonthlyRecords() Routine, trap the specific Duplicate Record Error that will now be generated if you try to add a Duplicate Record on the [Input MthY] Field via VCBA code, and Exit the Sub-Routine. The Record should not be Appended since rs.Update will not be successful in this scenario.
You are now covered in both situations:
1) Editing a Record and creating a Duplicate Record based on the [Input MthY] Field.
2) Adding a Record via the Sub-Routinre and creating a Duplicate Record on the [Input MthY] Field.
NeoPa 32,534
Expert Mod 16PB
Nice answer ADezii.
Ariel,
If you feel that any part of this resolution is too complicated for you to implement - don't worry, just post back explaining which parts you find complicated and ADezii (or someone else even if he's too busy) will be able to help you through it.
Error handling is a very important part of VBA code but some people avoid using it. Don't let that frighten you off this solution.
Best of luck.
MODERATOR.
Nice answer ADezii.
Ariel,
If you feel that any part of this resolution is too complicated for you to implement - don't worry, just post back explaining which parts you find complicated and ADezii (or someone else even if he's too busy) will be able to help you through it.
Error handling is a very important part of VBA code but some people avoid using it. Don't let that frighten you off this solution.
Best of luck.
MODERATOR.
thank you. i am new to programming...i don't understand a single thing. any similiar sample programs where i can get ideas from?
NeoPa 32,534
Expert Mod 16PB
Ariel,
Please reread my earlier post. The wording was not random.
Unless I/we have a particular point to help with, we are looking at saying that the answer is not good enaough and starting again.
That is certainly not the case here, and you have a choice to use it or not. We can help you but I am certainly not prepared to look elsewhere until I'm sure you've given the answer a fair try. As I say, we can help with that. I am certainly not offering to do work for you, simply to help you do the work yourself.
MODERATOR.
From the original post... - Set f = Forms!frmQpi
-
MthYr = f("txtMthYr")
-
sql = "SELECT * FROM [tblQpiMonthly] WHERE (([tblQpiMonthly].[Input MthYr] = 'MthYr' ));"
-
Can someone please explain this to me? Some of the syntax used in this routine is unfamiliar to me, so don't think that I'm telling you anything is wrong - I just didn't quite follow it.
The main problem I have is with the underlined sections. The way I read it, this is supposed to be matching on a month and year value found in textbox txtMthYr, but is in fact searching for the literal string "MthYr".
Am I misreading this?
It took me a while to realise something. In hindsight, if the WHERE clause is wrong, this might never find a match, and hence give rise to the whole issue of creating duplicates.
here is the function which i re-edited. the codes that were not affecting the program have been deleted. -
Public Sub PutInMonthlyRecords()
-
-
Dim sql As String
-
Dim Db As DAO.Database
-
Dim rs As DAO.Recordset
-
Dim f As Form
-
-
-
Set f = Forms!frmQpi
-
Set Db = CurrentDb()
-
-
-
sql = "SELECT * FROM [tblQpiMonthly];"
-
-
Set rs = Db.OpenRecordset(sql)
-
-
If (f!txtTotalPF) = 0 Then
-
Exit Sub
-
End If
-
-
If (f!txtTotalPF) = 0 Then
-
Exit Sub
-
End If
-
-
If (f!txtTotalPF) = 0 Then
-
Exit Sub
-
End If
-
-
rs.AddNew
-
-
rs![Input MthYr] = f("txtMthYr")
-
rs![Monthly TotalPF] = f("txtTotalPF")
-
rs![Monthly Rejection] = f("txtTotalAvoid")
-
rs![Monthly TotalAvoid] = f("txtRejection")
-
-
rs.Update
-
-
-
Db.Close
-
-
End Sub
-
Basically, when the record is not found in the table, it will add the new record. if the record is found in the table, it should edit the record instead of adding the same record into the table.
the form looks this way (see attachment0
the form looks this way (see attachment)
NeoPa 32,534
Expert Mod 16PB
Ariel,
You need to look again at ADezii's post (#2) and try to work your way through it. We can help with specific questions but not in the form of "I'm lost - please tell me the whole thing again in easier words."
You should do what you can then, when you get stuck, post exactly what your problems is and we can help you through it - with reference to post #2 that we can all see.
Does this make sense to you?
If you abort from an answer because it's a little complicated to understand, and try a different tack, then we could be here forever as you keep chopping and changing. This is unfair on our experts so I don't want to see it happen.
If you can go through it as I've described above, then we can help you.
MODERATOR.
here is the code for the statement from this: -
rs.AddNew
-
-
rs![Input MthYr] = f("txtMthYr")
-
rs![Monthly TotalPF] = f("txtTotalPF")
-
rs![Monthly Rejection] = f("txtTotalAvoid")
-
rs![Monthly TotalAvoid] = f("txtRejection")
-
-
rs.Update
-
to this: -
While Not rs.EOF
-
If Not rs.NoMatch Then
-
Exit Sub
-
-
rs.AddNew
-
-
rs![Input MthYr] = f("txtMthYr")
-
rs![Monthly TotalPF] = f("txtTotalPF")
-
rs![Monthly Rejection] = f("txtTotalAvoid")
-
rs![Monthly TotalAvoid] = f("txtRejection")
-
-
rs.Update
-
Next
-
rs.MoveNext
-
Wend
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Catherine Jo Morgan |
last post by:
Can I set it up so that a certain combination of fields can't contain the
same entries, on another record? e.g. a combination of
FirstName/LastName/address? Or FirstName/LastName/phone? Or...
|
by: ms |
last post by:
Access 2000:
I am trying to delete duplicate records imported to a staging table leaving one
of the duplicates to be imported into the live table. A unique record is based
on a composite key of 3...
|
by: Carroll |
last post by:
I'm looking for a way in SQL to find duplicate records in a single
table, that are the same based on 3 columns, regardless of what is in
the other columns in the duplicate records. I would like to...
|
by: B.N.Prabhu |
last post by:
Hi,
I have a DataTable with several rows. Its having 20 Columns. when i click
the Insert button then i have to check the Database Rows. Whether these new
rows are already available in the...
|
by: nethravathy |
last post by:
Hi,
The following table namely elcbtripselect contains 5147 records.I want to know wether this table contains duplicate records or not.
I tried with following query
1)SELECT...
|
by: teser3 |
last post by:
I have my PHP inserting into Oracle 9i.
But how do I prevent duplicate record entries?
I only have 3 fields in the insert in the action page:
CODE
<?php
$c=OCILogon("scott", "tiger",...
|
by: jbrumbau |
last post by:
Hello,
I have been successfully using a database I've created for several months to populate an equipment list for a project we've been working on. However, the form has recently stopped working...
|
by: nomvula |
last post by:
hi guys
i need some help to duplicate records on my form datasheet:
here's the example of my form results:
ClientLookup DateCaptured ForecastDate Description ForecastQuantity Forecast Actual
UJ...
|
by: xraive |
last post by:
I have a problem with this. Currently I am trying Allen's code and i am not successful.
Current Design
Table1 (Main Form)
TravelID (PK)
ApprovedBY
EntreredBy
BudgetCode
ExpenseCode
|
by: Rina0 |
last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
| |