473,387 Members | 1,603 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,387 software developers and data experts.

Find duplicate record and pass its values to the boxes for update

hefaz
24
Hello every one.
i am trying to catch duplicate values from a data. in the picture when i want to add new record, then if the record already exists then go to that record and fill all the boxes, then i will press the update button,but if not, then add new value. field is indexed.
Capture.JPG![Capture.JPG]
so how to code that? i need it very much.
here is the code:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdAdd_Click()
  2. If T6.Text = Data.Recordset.Fields("Name").Value Then
  3.  T8.Text = Data.Recordset.Fields("Amount").Value
  4.  T9.Text = Data.Recordset.Fields("Price").Value
  5.  T10.Text = Data.Recordset.Fields("Bought").Value
  6.  T11.Text = Data.Recordset.Fields("Date").Value
  7.  Else
  8.  MsgBox ("new record")
  9.  ' what i need here to insert? to add a new record
  10. End Sub

Attached Images
File Type: jpg Capture.jpg (39.4 KB, 253 views)
Nov 9 '15 #1
26 2467
strive4peace
39 Expert 32bit
Recordset.AddNew
assign values
then
Recordset.Update
Nov 10 '15 #2
hefaz
24
but now it says, this is avoided because of associated object, what is that?
Nov 10 '15 #3
strive4peace
39 Expert 32bit
you probably need Data. before Recordset

Also, I apologize for suggesting an answer ... am new to posting here. I gave you a different answer but then realized you were asking from Visual Basic not Access and couldn't figure out how to delete my post so I did my best at giving you an answer that might work. Perhaps someone else will jump in with something better.

thanks
Nov 10 '15 #4
hefaz
24
no problem at all, thank you very much, however I tried data I hope someone will help me
Nov 14 '15 #5
zmbd
5,501 Expert Mod 4TB
I don't usually handle VB stuff directly; however, from what I've seen S4P is on the right track.

I am guessing the data is in a database, so, what I would guess you should do is build an SQL based on the values entered. Use that SQL to establish a record-set object and check for the presence of a matching record. If found, then either use that Object to edit directly therein or use the Object to jump to that record.

You do not mention where your datasource is located (Access, Oracle, MYSQL, SQL Server, etc...); however, because most of my work is with Access, I do have a link to a basic (pun intended :-D ) method to open a record set in Access using VB - it is for Offic97; however, the method should still be valid: Visual Basic Example to Open a RecordSet Using DAO

Hopefully that points you in the right direction.
Nov 15 '15 #6
strive4peace
39 Expert 32bit
thanks, Z

so it would seem that hefaz needs to define the SQL to find a specific record, and assign a recordset variable.

Assume this would be something like:

Expand|Select|Wrap|Line Numbers
  1.   Dim ws As Workspace 'this may not be necessary
  2.   Dim db As dao.Database
  3.   Dim strSQL As String
  4.   Dim rs As dao.Recordset
  5.  
  6.   'Create a default workspace Object
  7.   'this may not be necessary
  8.   'Set ws = ... 'not sure what this would be ... example is DBEngine.Workspaces(0) 'but this is what Access uses
  9.  
  10.   'Open a Database 
  11.   'Set db = ws.OpenDatabase("PathAndFilenameOfDatabase")
  12.   'might just need this:
  13.   Set db = OpenDatabase("PathAndFilenameOfDatabase")
  14.  
  15.   'The SQL statement -- change Tablename to whatever it is
  16.   strSQL = "SELECT T.*" _
  17.     & " FROM [Tablename] as T " _
  18.     & " WHERE T.Name = '" & T6.Text & "'"
  19.  
  20.    'Create a Dynaset Type Recordset
  21.    Set rs = db.OpenRecordset(strSQL,dbOpenDynaset)
  22.  
  23.    with rs
  24.       if .eof then 'no record found
  25.          .AddNew
  26.          !Name = T6.Text
  27.       else 'record found
  28.          .Edit
  29.       end if
  30.       !Amount = T8.text
  31.       !Price = T9.Text
  32.       !Bought = T10.Text
  33.       !Date = T11.Text
  34.       .Update
  35.       .close 'close recordset
  36.    end with 'rs
  37.  
  38.    set rs = nothing 'release recordset variable
  39.    db.quit 'or maybe db.close
  40.    set db = nothing 'release db variable
  41.    'set ws = nothing 'if ws is needed
the thing this code does not to is ensure there are values in each control, which really should also be done before assigning them

hefaz , if you get it figured out, would you mind sharing what you did? thank you
Nov 15 '15 #7
hefaz
24
thank you both zmbd and dear strivepeace.
actually I don't know sql I am using access database, which doesn't have a code like that to define, I think I can't describe my problem, here I will try to explore it again.
in the first post I have a form (the picture) I want to enter some texts to the boxes, so if the record (name) which is denid duplicate in database, already exists, than give me a message and fill automatically all the boxes with that record for updating, and if it does not exist than create a new record. my English is weak, I hope you understood. thank you again.
Nov 15 '15 #8
strive4peace
39 Expert 32bit
hi hefaz,

you're welcome

you are using Access? Well, then we can help you better! You posted this in the Visual Basic forum.

The easiest way to change information in a table using a form is to bind it to the RecordSource.

Go to the design view of the form.

Turn on the Property Sheet if it is not showing by pressing Alt-Enter.

the Property Sheet shows you more information about what is selected.

Click where the rulers intersect in the upper left to select the form object.

On the property sheet, click on the Data tab and then in the RecordSource property. Then click on the drop-down arrow and select the table you are using.

Once you have identified the table, you can then bind each of the controls.

Click on the first control.

On the data tab, click in the ControlSource property, then drop the list and choose the appropriate field.

Do this for each control.

Once you get that far, post back and we can help you more.

Your form looks very nice. What is your native language?
Nov 15 '15 #9
strive4peace
39 Expert 32bit
hi hefaz,

it occurs to me that I perhaps got so excited when I saw Access that I misinterpreted what you meant. If you would be kind enough to ZIP your project and a sample database, we can look at it and attempt to give you better help.
Nov 15 '15 #10
hefaz
24
thank you man, my native language is Pashto(Afghanistan)
well, I think I again missed that, sorry for that, I am using access database with visual basic the form is created in visual basic 6.0 ( now I am really confused, how to describe my problem?)(hahaha we know that gathering requirements is hard step in software engineering.).
Dear s4p. I am just using access as back-end my front end is visual basic, just make a form for me, that can catch a duplicate record and pass its all values to the textboxes in the form, if the duplicate record doesn't exist than create that record as a new record. thanks for all the patience friends.
Nov 15 '15 #11
zmbd
5,501 Expert Mod 4TB
Hefaz,
S4P's post should point you in the correct direction.

You need to make the connection to the database:
Visual Basic Concepts> Visual Studio 6.0> Create a Connection to a Microsoft Access Database File

There is a link there to take you to a tutorial for interacting with the database engine. It's not the best; however, it should provide the basics.

+ Access SQL is a tad different than your standard flavor if SQL; however, it will be something you have to master to be able to complete your project.
Nov 15 '15 #12
hefaz
24
thanks zmbd,
every thing is ok with the database all the links are created, data is also connected, I just want to catch duplicate record and pass its value to boxes,(BANG, I AM CONFUSED, YOU ALSO GOT CONFUSED.).
Nov 15 '15 #13
strive4peace
39 Expert 32bit
Hi hefaz,

firstly, Name is NOT a good unique identifier -- there must be something better. Use an AutoNumber in the database structure to uniquely identify records. Also, since your database is for medications, I am assuming there will be a CustomerNumber or a PatientNumber that would also be unique.

If you need further help, we need to see your form and a sample database. Could you zip them and attach them? Or post them and link us to them?

Thank you

warm regards,
crystal

~ have an awesome day ~
Nov 15 '15 #14
zmbd
5,501 Expert Mod 4TB
K,
+ Building on S4P's information: Tokens and key words to avoid:
Problem names and reserved words in Access
Your best bet is to always use a modified version.
So instead of [Name] for a field it is advisable to Use [FName] or [First_Name] etc....

+ Look at lines 16 to 18 of S4P code.
This sets up the query when executed in line 21 to return a record set that either contains nothing, or contains the record meeting the values in your form.

+ Lines 23 thru 36 handle the find-and-edit of the matching record or creates a new record with the entered values.

Personally, I would have looked at the record count as I've found the End of File flag to not always be reliable; however the count will either return 1 or 0 if there are any records or no records respectively.

In any case, the EOF should work for most cases.

+ As S4P rightly points out, the code needs to be modified to handle null/no-entry values in your form. I add to this that the code also needs to be setup for error trapping too.

At this point, I would disagree with needing to see your database file in and of itself; however, if you will list the table names and fields along these lines:

table1_name
[field1_name] as primary key as long (or autonumber)
[field2_name] as text(20)
[field3_name] as foreign key to table... as long
etc...

for each of your tables that would help.


Please understand, many of us cannot or will not d/l attachments. This has absolutely nothing to do with who you are... In my case, such d/l are prohibited by my ITSec staff. For many others, it is part of "safe computing/best practices" not to d/l such files. A practical example as to why can be found here http://bytes.com/topic/access/answer...l-ms-products. this unfortunate Member opened a file from a trusted source and was having all sorts of issues!
Nov 15 '15 #15
strive4peace
39 Expert 32bit
thanks, Z -- thought since he attached the image, he could attach a file too ... still, and always, learning. I would like to see his project to figure it out myself too :)
Nov 15 '15 #16
hefaz
24
ok guys I will just zip the database along with the form and buttons, Just give it a look.
Nov 15 '15 #17
hefaz
24
when I want to enter records to the boxes, then I click check button to check if the record already exists, which's indexed value(no duplicates)is the name field. if it exists than give me that message in the code (second pic)and give its values to the boxes than I click update button to update that record. and if not than create a new record,
well this works, but when I enter a records that is already In the database, than give me an error message (third pic) now correct that for me. I have included the zipped folder.
That would be your pleasure if you guys give me your email, so that I would be able to send the complete project.
(capture.jpg)



CheckCode.JPG



Error.jpg

Attached Images
File Type: jpg Capture.jpg (28.7 KB, 257 views)
File Type: jpg check code.JPG (45.3 KB, 269 views)
File Type: jpg error.jpg (26.6 KB, 257 views)
Attached Files
File Type: zip Database+vb6form.zip (40.9 KB, 93 views)
Nov 15 '15 #18
zmbd
5,501 Expert Mod 4TB
+ Usually we ask our membership not to use private emails for topics online unless the information is sensitive in nature and we involve the site administration.

+ The code posted in the image.
This is nothing like what we've suggested/advised and as it stands will not work as there is no check between the database and the form. You must use some method, even if you have to emulate the DLookup() (see tutorial here on how to do that in VB: How to Implement the DLookup Function in Visual Basic ), to look the value up first.

Thus, User enters data, Clicks on [Check] Button, Code runs the look up via either the emulated Domain function or as we have suggested via record-set, if the code finds the value then retrieve and populate the controls on the form and enables the [Update] button, if the code doesn't find the value then create a new record and save the form data... maybe provide a bit of user feed back to that effect.
Nov 15 '15 #19
hefaz
24
exactly zmbd.
your third paragraph is my problem, now is there any way to do what I want? or is that a nonsense idea of mine? please tell me if the code s4p wrote will work for this problem. or write your own and final idea, because I know I have created a headache for you guys, nothing else.

sorry I am new here and still don't understand the basics of the site. p i.e privacy etc.

wow, thanks for the nice suggestion, about dlookup, I will try it, I think that is my problem.

sorry I failed to do that, if someone create just a sample for me that would be better, otherwise I can't.
Nov 16 '15 #20
zmbd
5,501 Expert Mod 4TB
+ Usually we do not write the entire code, instead, we try to help guide our membership to look at all of the options available and decide the best route for themselves.

The reasoning is two fold:
++ Primarily, because there are usually multiple methods to arrive at a solution and no one approach is a panacea
++ Because, when one finds the solution oneself the methods learned become more valuable in that one usually learns how to apply the tools in more situations.
Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime.

- Maimonides
+ The code S4P has provided is a framework that you will need to modify to fit your situation.
Nov 16 '15 #21
hefaz
24
yeah, that gave me really a good sense and felling, thank you from the advice. that is right, I will try hard to reach to my problem's solution, and will post the answer back here. thank you both for the contribution.
THOSE WHO STRUGGLE MAY FAIL, BUT THOSE WHO DON'T STRUGGLE HAVE ALREADY FAILED.
Nov 16 '15 #22
zmbd
5,501 Expert Mod 4TB
hefaz,
Your thread has definitely highlighted a weakness in my understanding of VB6 as it relates to Access. To that end I started looking around for more information about how to handle Access in VB.

I've found the following tutorial, it mentions using ADO as the primary means of manipulating Access database objects; however, with the advent of the "ACEDAO" (currently Access 2007 and newer) - I think it advisable to learn how to handle the "DAO" methods described within the tutorial. Using the native engine language to handle data seems to be the best route. I use ADO methods when push/pull data to a SQL-Server or MYSQL because the ADO translates better to those servers.

In any case here's the link - keep in mind that this tutorial references older libraries (3.51) however the newest version is ACE 12.0 ((Database.Version Property (DAO) )) So some things such as user level security are no longer available: http://www.vb6.us/guides/vb6-database-guide

In any-case, this is the one I am staring with, I found three others that appear to be newer and I'll be looking thru too - but they don't appear to be for "the faint of heart." Thus, this one is just as a primer for me as I do not develop in VB, even though it references older material it may provide some useful insights for your project.

Best of Luck with your project.
Nov 16 '15 #23
hefaz
24
Thank you man. Rated you as best moderator with 5 stars.
Nov 17 '15 #24
hefaz
24
still working on the problem guys...
Feb 18 '16 #25
zmbd
5,501 Expert Mod 4TB
hefaz,
Have you a revised code?
What further steps have you taken?
Feb 19 '16 #26
hefaz
24
no zmbd sir,
i removed that feauture form it. it was really disappointed.
May 15 '16 #27

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

Similar topics

8
by: Mark | last post by:
When my form goes to a new record, I have a procedure that copies the last record added to the form's underlying table into the form. The intent is that a series of new records may have the same...
4
by: MP | last post by:
I need a code in the Before Update event procedure, which prevents to enter duplicate record in a form. I have the main formX, which takes values from the table named tblX and a subformY, which...
0
by: Cyberwolf | last post by:
OK, how to explain this. I have a table that will feed into another table using an append query. What I want to do is look at the table I am appending the record to to check for a duplicate...
0
by: Antonio | last post by:
Hello, everyone. Something strange is happening when I edit a record in the datagrid. I have a cust_id field that, when the grid is in edit mode, users can change. I don't really want to display...
2
by: john saul | last post by:
I have a form that i use for customers to input information that i will use to locate a specific part. This part will have a specific name that will consist of a concatination of one letter from...
4
by: mcca0081 | last post by:
hi - i'm trying to delete one of the duplicate records based on the most current date. here's the code for my access 2000 db. any help would be appreciated!!! - thank you kindly Sub...
3
by: rajeshkrsingh | last post by:
Hi friends, Step1- create table duplicate ( intId int, varName varchar(50) ) insert into duplicate(intId,varName) values(1,'rajesh') insert into duplicate(intId,varName) values(2,'raj12')...
6
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",...
1
by: shr04 | last post by:
i m doing search for specific record. i want to update that record so when i find the record i double click on it and after doublecliking the row all the values in the grid are trasfer to update page...
2
by: prabhu kannan | last post by:
pls help me to find the duplicate record in vb6 i'm using access 2007. and my code is.. If (rs2.Fields(0) = copnme_txt.Text) Then reset Else rs2.AddNew rs2.Fields(0) = copnme_txt.Text...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.