Solved

MS Access - Delete all rows after a certain value for a given column

Posted on 2014-10-20
10
207 Views
Last Modified: 2014-10-25
I have  a table  (which is exported from Excel) .
 I know that the column by name F1 will have the  value starting with "Representation"  . All the rows from that row (including that row)  need to be deleted ( But I don't have the count of rows )

 Can you help me with a  query for this ?  

 Infact ,  This is part of a  Access macro . So  using  VBA also is an option

 Thanks
0
Comment
Question by:Sam OZ
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40393524
try this delete query


delete * From [Name Of Table]
where [F1] Like "Representation*"
0
 

Author Comment

by:Sam OZ
ID: 40393531
Thanks , But that is not what I am looking for . For example, If the 20th row has the column F1 Like "Representation*" , then all the rows from the row 20 should be deleted

 I get a feeling this need to be done with Recordset . Loop thru RS ..Look for the row . When found delete all the rows from  there as the loop goes thru
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40393575
for a record there is only ONE row.

the query I posted will delete the records with  "Representation" in the F1 column
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

Accepted Solution

by:
Sam OZ earned 0 total points
ID: 40393604
Experts , My approach solves the problem.  
 Loop thru RS ..Look for the row . When found  set a flag and , delete all the rows from  there as the loop goes thru
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40393647
can you post the codes that you used, so others may learn other ways of deleting records
0
 

Author Comment

by:Sam OZ
ID: 40393711
This is the code .

Private Function DeleteRows(dbobj As dao.Database)
   
   Dim MyRs As dao.Recordset
   Dim MyTableDef As TableDef
   Dim Sql As String
   Dim TableName As String
   Dim PropertyValue As String
   Dim bDeleteFlag As Boolean
   
   For Each MyTableDef In dbobj.TableDefs
     TableName = MyTableDef.Name
     bDeleteFlag = False
    ''Routine to check validity of name
     If IsValidTable(TableName) = False Then
       GoTo NextTable
     End If
     Sql = " select " & SPFPropertyColumnName & " from " & TableName
     Set MyRs = dbobj.OpenRecordset(Sql, dbOpenDynaset)
     While Not MyRs.EOF
        If Not IsNull(MyRs(PropertyColumnName)) Then
          PropertyValue = MyRs(PropertyColumnName)
        Else
         PropertyValue = ""
        End If
          '' Calling function to Check  the propertyvalue starts with Junkvalue
        If CheckStartsWith(UCase(PropertyValue), UCase(JunkValueStart)) = True Then
           bDeleteFlag = True
        End If
               
        If bDeleteFlag = True Then
           MyRs.Delete
        ElseIf PropertyValue = "" Then
           MyRs.Delete          
        End If
       
       
       MyRs.MoveNext
     Wend
     
     If Not MyRs Is Nothing Then
        Set MyRs = Nothing
     End If
     
NextTable:
   Next
   
   
     
 End Function
0
 
LVL 50

Expert Comment

by:Gustav Brock
ID: 40393823
> If the 20th row has the column F1 Like "Representation*" ,
> then all the rows from the row 20 should be deleted

I can't see where this is brought into play.
Your code seems, basically, to do the same as Rey's query.

/gustav
0
 

Author Comment

by:Sam OZ
ID: 40393833
If the rows are like

    =======   START OF ROWS ======================
  Myr1
  Myr2
  ...
 ...

 Myr18

 Representation : This is explaining the obvious
  This is a row after row20
   But the row 21 ,22,23  is blah blah
   So 20,21,22,23 should be deleted.

     =======   END OF ROWS ======================  

    Row 19 is a row with no value .
    The SQL will delete only row 20
    My code will delete row 19 (because it is empty)  , 20,21,22,23
     ( It is because after the Deleteflag is set to true all the  rows are deleted until the end)
0
 
LVL 50

Expert Comment

by:Gustav Brock
ID: 40393844
That is true. Missed that. Thanks.

/gustav
0
 

Author Closing Comment

by:Sam OZ
ID: 40403764
The solution is my previous comment . Comment from the other expert didn't help, unfortunately
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question