473,473 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Getting rid of Duplicates in a Field

2 New Member
Hi I'm new to VBA and Access and was wondering if anyone knew of a way to go through my field to replace the duplicated string separated by a new line and carriage return:

Field Ex:
A
A
A
B
B
C
C
C
C

With:
A
B
C

I really appreciate any help.
Sep 28 '20 #1
14 4830
twinnyfo
3,653 Recognized Expert Moderator Specialist
CankleSteve,

Welcome to Bytes!

I'm not sure I understand your question. Is your first list of characters the contents of one field in one record or is this a series of records from one field in a table?

The answer to this will certainly determine how we approach this solution.

Standing by to hepp!

Thanks!
Sep 29 '20 #2
CankleSteve
2 New Member
Twinnyfo,

They are a list of one field in one record. So the record's field would read like the long above list. I would like help figuring out how I can "scan" through them and take out any duplicates.

Note: they seem to all be in alphabetical order, so in no cases will it go like A /n/r A /n/r B /n/r A for example.
Sep 29 '20 #3
twinnyfo
3,653 Recognized Expert Moderator Specialist
So, I will "describe" the solution, without coding it out, but we can always work toward a final solution if needed.

I would create a user-defined function that takes the value of the field ans assigns that value to an array, using the Split() function, with the separating value of Chr(13)--which is the carriage return. Once it is in an array, assign the first character to a string variable. Cycle through the values in the array and when a value other than the current value is found, add a carriage return and that character to your string variable. After going through the entire array, return the value of that string.

Again, this is a description, but at the moment I am unable to write out code for this, but I'm glad to continue efforts on this.

If you have no idea what I am talking about, then we will have to go through things step by step. That's OK - everyone has to begin learning VBA at some point.

Hope this hepps!
Sep 29 '20 #4
ADezii
8,834 Recognized Expert Expert
Although not an easy task, I'll try to step into twinnyfo's shoes for a minute, and hopefully I'll be on the right track.
  1. I have no access to Access (no pun intended) right now, so I will simulate a Field Value in Excel, then pass it to a Function to be analyzed.
    Expand|Select|Wrap|Line Numbers
    1. Dim strTest As String
    2.  
    3. 'Simulate Field Value
    4. strTest = "A" & vbCrLf & "A" & vbCrLf & "A" & vbCrLf & "B" & vbCrLf & _
    5.           "B" & vbCrLf & "B" & vbCrLf & "C" & vbCrLf & "C" & vbCrLf & _
    6.           "C" & vbCrLf & "C"
    7.  
    8. 'Debug.Print strTest         'Make sure I got it right!
    9.  
    10. Debug.Print fAnalyzeString(strTest)
  2. As twinnyfo has suggested, I'll split the String into an Array consisting of individual Elements with a CrLf being the Delimiter.
  3. I will use a variable and extract the 1st character from the Array.
  4. Now, check this character against every element in the Array, and if there is no match, start building the Variable String. You are actually checking the rightmost character of the Build String against elements of the Array.
  5. Repeat this process until every character in the Build String has been checked against every element in the Array.
  6. Function Definition:
    Expand|Select|Wrap|Line Numbers
    1. Public Function fAnalyzeString(strString As String) As String
    2. Dim varString As Variant
    3. Dim strChars As String
    4. Dim intCtr1 As Integer
    5. Dim intCtr2 As Integer
    6.  
    7. varString = Split(strString, vbCrLf)
    8.  
    9. strChars = varString(0)
    10.  
    11. For intCtr1 = 1 To Len(strChars)
    12.   For intCtr2 = LBound(varString) To UBound(varString)
    13.     If Right$(strChars, 1) <> varString(intCtr2) Then
    14.       strChars = strChars & vbCrLf & varString(intCtr2)
    15.     End If
    16.   Next
    17. Next
    18.  
    19. fAnalyzeString = strChars
    20. End Function
  7. OUTPUT:
    Expand|Select|Wrap|Line Numbers
    1. A
    2. B
    3. C
P.S. - Looking at my explanation, I do realize that it may not be the best in the world. Perhaps twinnyfo can make it much clearer.
Sep 29 '20 #5
isladogs
457 Recognized Expert Moderator Contributor
There are many ways of dealing with this situation. For example:

Create a new empty table with the same fields but index your field (the one with dupes) and set as no dupes allowed.

Now create an append query to copy all data into the new table.
Because you have set a unique index, only one record will be appended for each duplicate

Hope that helps
Sep 29 '20 #6
twinnyfo
3,653 Recognized Expert Moderator Specialist
@ADezii
I have not checked your math, but your approach is spot on to the direction I was going. It's a great outline to start with.

@isladogs
If you read the rest of this thread carefully, you will notice that your solution does not address the issue. Note, that this was my first question to OP, and we determined that the field itself has these duplicate values, not a series of fields.

@CankleSteve
I hope this has given you something to work with, or at least begin work on a solution for your table.

Standing by with more hepp if you need it....
Sep 29 '20 #7
isladogs
457 Recognized Expert Moderator Contributor
@twinnyfo
I've re-read the thread carefully but your response wasn't clear!
I think you meant a single record in a field has these duplicate values not a series of records. If that is the case, my suggestion won't work.
Sep 29 '20 #8
twinnyfo
3,653 Recognized Expert Moderator Specialist
Isladogs,

Fields don’t have records, records have fields. Your solution is based upon one field among multiple records. The question is about one field in one record that has a “stacked” value with duplicate values in that “stack”.

As a side note, although your method may have worked under a different scenario, it is usually not wise to force the loss of a record via index violations. This would require adequate and accurate error handling to keep the DB from halting, not to mention the loss of all the data in the other fields of each record rejected.

Hope this makes sense and hope it hepps you understand this issue a bit better.
Sep 30 '20 #9
isladogs
457 Recognized Expert Moderator Contributor
Twinnyfo
Yes, my answer was also poorly worded regarding fields and records.
Thank you but I already understood the issue without your additional explanation
I'm also fully aware of potential side effects of deleting any data.
Not sure why you keep saying 'hepp' when you mean help.
Sep 30 '20 #10
NeoPa
32,556 Recognized Expert Moderator MVP
"Not sure why you keep saying 'hepp' when you mean help."
Not sure how old you are C but if you remember back a few years (OK. Decades. I mean decades alright :-( ) when The Beverly Hillbillies was showing on terrestrial TV over here then imagine TwinnyFo (24 in same accent) speaking in a similar dialect.

He tells me it isn't from that show but another I (probably we) are less familiar with but it's about stock car racing in that same region I believe.

Anyway, all I can really say is that Twinny, as you'll see from his post count, is one of our most prolific and helpful expert mods.

All the best - and I hope life out West is treating you well. You can imagine how much fun it is to be close to Town right now but we're managing.
Dec 22 '20 #11
isladogs
457 Recognized Expert Moderator Contributor
Hi NeoPa
I'd forgotten all about my slightly irritated responses in this thread. Anyway, thanks for the explanation as well as the derivation of twinnyfo's name.

I believe I'm quite a few years older than you but I have to confess that I never saw the Beverley Hillbillies back in the day.

I do count myself lucky to be living in the middle of nowhere (Exmoor). Even without COVID, I'd find it very hard to live in a town again after so many years away. I'd noticed you hadn't been around for a while. Good luck and stay safe
Dec 22 '20 #12
NeoPa
32,556 Recognized Expert Moderator MVP
IslaDogs:
I believe I'm quite a few years older than you but I have to confess that I never saw the Beverley Hillbillies back in the day.
I wouldn't have known, there again I maybe haven't actually seen you much. When we spoke it was just on the microphone mainly.

I just checked when it stopped showing. It was March 1971. As a point of reference that was about a month after we completed conversion of the currency to decimal!
Dec 23 '20 #13
twinnyfo
3,653 Recognized Expert Moderator Specialist
Henry,

I believe Post #5 has an adequate outline/solution. OP has not responded to let us know if this approach is acceptable in his situation.
Dec 23 '20 #14
NeoPa
32,556 Recognized Expert Moderator MVP
Both posts #4 & #5 describe solutions that appear adequate to me. Are they for you?

If so then you have your answer. If not, please feel free to post a question explaining what you need to know. Your question would have to be in its own separate thread of course.
Dec 23 '20 #15

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

Similar topics

0
by: Child | last post by:
i have an editable datagrid, which screws up my date every time. Can you please advise? I am retrieving the edited data using the following code. I am certain is the ctype causing the problem. ...
0
by: Rob G | last post by:
Hi There, I am using a Drop Down List box to select a Person's name and then based on that selection a CheckBoxList fills with their information. I pull the data set with an in-line SQL...
1
by: John | last post by:
Hi Is there an asp.net equivalent of the following code in the context of a form? for i = 1 to Request.Form.Count strMsgInfo = strMsgInfo & Request.Form.Key(i) & ": " & Request.Form.Item(i)...
0
by: Sagaert Johan | last post by:
hi 2 questions This code is in the mda.Updated event ; why does the ExecuteScalar returns a decimal while in the table definition the keyfield is declared as int ? SqlCommand Cmd = new...
4
by: Aneri | last post by:
I want to show a same field from database twice each with different conditions in datagrid.I use Oracle as back-end. I use self join as below: sql2 = "select m1.int_no(select int_no from...
1
by: lizarraga | last post by:
I have a table this table sep 2007 = 1.7890 oct 2007 = 1.8952 nov 2007 = 1.8970 in the field (date) sep 2007 the problem is and and other field get 1.7890
4
by: Bob Alston | last post by:
My client insists that the printed copy of text entered into a memo field have EXACTLY the same layout as it appears on the screen. I found I had to eliminate the vertical scroll bar. Then I...
0
by: sparks | last post by:
I was using this to swap out a form with subforms on it by just making a copy and tying each to copy of the same table. subformV1 tblVisit1 subformV2 tblVisit2 etc Select Case TabCtl0.Value...
4
by: JimCarlson | last post by:
I have a form where the user selects the type of time off they wish to request. It is defined as follows: Control Source: Time Off Type (data type is Text and field size =1) Row Source Type: ...
2
by: timleonard | last post by:
How do you reference the Table:Field to use in a file path? I have been trying the code below I would like to reference a Tablel:Field to use in the path instead of hard coding the file name,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.