Results 1 to 4 of 4
  1. #1
    uframos is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Mar 2013
    Posts
    2

    Blank duplicated values

    Hi to all!

    I'm trying to execute a query that will delete(Blank) values in a certain column (in the example below col1), but I'm getting no positive result. Can you give me a hand?

    Original :

    col1
    col2
    Text1 2Text1
    Text2 2Text2
    Text2 2Text3
    Text2 2Text4
    Text3 2Text5
    Text3 2Text6
    Text4 2Text7
    Text4 2Text8
    Text5 2Te



    Goal:


    col1
    col2
    Text1 2Text1
    Text2 2Text2
    2Text3
    2Text4
    Text3 2Text5
    2Text6
    Text4 2Text7
    2Text8
    Text5 2Text9

    Thanks in advance!

    uframos

  2. #2
    Rawb is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2009
    Location
    Somewhere
    Posts
    875
    I'm not sure how you want to use this, but if you're presenting it to the user, I'd just make a Report that displays those two Fields and then set "Hide Duplicates" to "Yes" on col1.

    If you actually need to delete the data from the Table(s), then we'll need all of the Fields in the Table(s) effected before we can build an appropriate Query for you.

  3. #3
    uframos is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Mar 2013
    Posts
    2
    Quote Originally Posted by Rawb View Post
    I'm not sure how you want to use this, but if you're presenting it to the user, I'd just make a Report that displays those two Fields and then set "Hide Duplicates" to "Yes" on col1.

    If you actually need to delete the data from the Table(s), then we'll need all of the Fields in the Table(s) effected before we can build an appropriate Query for you.
    Yes I need to delete the data from the table. I will send you 2 screenshots of the real table (tbl_ListagemFretes). The second secreenshot is goal I want to achieve with the query.

    Click image for larger version. 

Name:	tbl1.png 
Views:	1 
Size:	26.4 KB 
ID:	11688

    Click image for larger version. 

Name:	tbl2.png 
Views:	2 
Size:	20.3 KB 
ID:	11689

    The column "Frete" is the one I want to change.

    uframos

  4. #4
    Rawb is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2009
    Location
    Somewhere
    Posts
    875
    Well, that's going to be very difficult to do as a straight SQL Query (or collection of Queries). The easiest way to handle it in this situation, is to use VBA code to loop through the Records and, whenever it finds a duplicate entry, delete the data in that Field.

    Before I get to that though, I'm gonna clamber up on my soapbox and preach a little bit.

    <soapbox>
    From what I can see of your data, you're treating the Tables as a collection of Spreadsheets (instead of actual Tables in a relational database). This is fairly typical of people that are just getting into relational databases and, as long as the applications are small and simple, doesn't hurt things too much.

    That said, I'd strongly recommend learning about normalization and implementing it in your database(s). It makes things much easier to handle as the database gets larger and more complex, it saves space, and it decreases the possibility of orphaning data/Records when editing information.
    </soapbox>

    OK, sorry about that, on with the VBA! I've tried to add comments to the code so you can see what I'm doing, but feel free to ask if you have any questions.

    Code:
    Public Sub Remove_Duplicates()
      ' This Sub loads the Table tbl_ListagemFretes into a Recordset and "fixes"
      ' any multiple consecutive values of the Field Frete by replacing the
      ' consecutive duplicates with empty strings. This Sub does not take any
      ' arguments and does not return any values.
    
      On Error GoTo Error_Remove_Duplicates
    
      Dim db1 As DAO.Database
      Dim work As DAO.Workspace
    
      Dim rst As DAO.Recordset
    
      Dim boolWork As Boolean
      Dim strCompare As String
    
      Set db1 = CurrentDb()
      Set work = DBEngine(0)
    
      ' There really should be an ORDER BY Clause here, but since I don't know the
      ' full design of the Table, you will need to come up with that part on your
      ' own
      Set rst = db1.OpenRecordset("SELECT * FROM tbl_ListagemFretes", dbOpenDynaset, dbSeeChanges) ' There really should be an ORDER BY clause in here!
    
      ' Only do the following if there's at least one (1) Record in the Table
      If Not rst.RecordCount = 0 Then
        strCompare = "" ' Initial value to compare "Frete" against
    
        ' Wrap everything in a Transaction so any database changes can be undone in
        ' the event of an error
        work.BeginTrans
        boolWork = True ' Transaction flag variable
    
        ' Loop through the entire Table
        Do While Not rst.EOF
          ' Compare the previous Record's Frete value (recorded in strCompare) with
          ' the current Record's value
          If rst("Frete") = strCompare Then
            ' If they match
    
            ' Delete the value of Frete for the current Record, do not change
            ' strCompare
            With rst
              .Edit
              !Frete = ""
              .Update
            End With
          Else
            ' If they don't match
    
            ' Change strCompare to match the new value of Frete, do not delete the
            ' value in the current Record
            strCompare = rst("Frete")
          End If
    
          ' Move to the next Record
          rst.MoveNext
        Loop
    
        ' If we haven't encountered any errors, save the changes to the database
        work.CommitTrans
        boolWork = False
      End If
    
    Function_Closing:
      ' Clean up our Object variables
    
      On Error Resume Next ' Disable error checking to prevent an infinite loop!
    
      rst.Close
      Set rst = Nothing
    
      Set work = Nothing
    
      Set db1 = Nothing
    
      On Error GoTo 0 ' ALWAYS turn error checking back on when you're done!
    
      Exit Sub ' Exit the Sub/Function
    
    Error_Remove_Duplicates:
      ' If we encounter an error
    
      ' If we're in the middle of our Transaction
      If boolWork = True Then
        ' Unset our Transaction flag variable to prevent an infinite loop!
        boolWork = False
    
        ' Undo all our changes to prevent data corruption or unanticipated changes
        work.Rollback
      End If
    
      ' Display the error message
      MsgBox Err.Number & ": " & Err.Description
    
      ' Jump to the end of the Sub/Function
      Resume Function_Closing
    End Sub

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 9
    Last Post: 01-22-2014, 01:27 PM
  2. Blank Values in Combo Box Selection
    By CementCarver in forum Access
    Replies: 1
    Last Post: 02-28-2013, 01:11 PM
  3. Combo Box showing Blank Values
    By glen in forum Access
    Replies: 5
    Last Post: 11-24-2012, 09:44 AM
  4. Duplicated values in query
    By truent in forum Queries
    Replies: 6
    Last Post: 03-03-2012, 01:45 PM
  5. Replies: 4
    Last Post: 10-28-2011, 12:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums