Deleting Date/Text from table with vba

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • slenish
    Contributor
    • Feb 2010
    • 283

    #1

    Deleting Date/Text from table with vba

    Hello,

    I am having a problem with a table where I want to delete the information in two rows of the table. What i have is an Excel file that im importing to a table. The current code im using deletes leading zeros from the first row that is imported which is what i need it to do, but at the very end of the report there are two additional rows that are being imported that I want to delete one is a date row another is a text row. I cant figure out how to adjust the code im using to delete these.

    Any help would be great!

    thanks so much

    code im using
    Code:
    Public Function TrimZeroes()
    
    Dim rec As DAO.Recordset
    Dim lngLoop As Long
    Dim lngCount As Long
    Dim strData As String
    Dim strTemp As String
    Dim intStartRow As Integer
    
    Set rec = CurrentDb.OpenRecordset("TableName")
    rec.MoveLast
    rec.MoveFirst
    
    
    lngCount = rec.RecordCount
    For lngLoop = 1 To lngCount
    strData = rec("ColumnA")
    strTemp = Left(strData, 1)
    Do While strTemp = "0"
    strData = Mid(strData, 2)
    strTemp = Left(strData, 1)
    Loop
    rec.Edit
    rec("ColumnA") = strData
    rec.Update
    rec.MoveNext
    Next lngLoop
    Set rec = Nothing
    
    End Function
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Try the following, on Test Data of course! (LOL):
    Code:
    Public Function TrimZeroes()
    Dim rec As DAO.Recordset
    Dim lngLoop As Long
    Dim lngCount As Long
    Dim strData As String
    Dim strTemp As String
    Dim intStartRow As Integer
      
    Set rec = CurrentDb.OpenRecordset("TableName")
    
    rec.MoveLast: rec.MoveFirst
      
    lngCount = rec.RecordCount
    
    For lngLoop = 1 To lngCount
      strData = rec("ColumnA")
      strTemp = Left(strData, 1)
        Do While strTemp = "0"
          strData = Mid(strData, 2)
          strTemp = Left(strData, 1)
        Loop
        
        If lngCount - lngLoop < 2 Then      'Next-to-Last, or Last Record
          rec.Delete        'DELETE Record
        Else
          rec.Edit
            rec("ColumnA") = strData
          rec.Update
        End If
          
          rec.MoveNext
    Next lngLoop
    
    rec.Close
    Set rec = Nothing
    End Function

    Comment

    • slenish
      Contributor
      • Feb 2010
      • 283

      #3
      Hi ADezii,

      I have said it before and im saying it again...you are the Man!!!

      Thank you so much for your help on this was driving me crazy!!

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        You are quite welcome, slenish.

        Comment

        Working...