Automatically Delete records from Recordset

  • Thread starter Booga_Boy via AccessMonster.com
  • Start date
B

Booga_Boy via AccessMonster.com

I wrote a macro to populate a text box on a form (continuous records) with
the contents of a directory by modifying the Access Help example of
"FileSearch." However, I want to populate the text box only with files that
*don't* already exist in the underlying query. I keep getting a run-time
error of "No current record" (No. 3021) when the macro gets to the "rst.
delete" line. Any way around this, or any way to modify the macro to
automatically delete a new record if it already exists? Code is below:

Query Name: qryGeneral
Field Name: file
Text Box: txtFile

Dim Length
Dim CurrentFile
Dim rst As Recordset
Dim strCriteria As String

Set fs = Application.FileSearch
Set rst = CurrentDb.OpenRecordset("qryGeneral")
strCriteria = "[file]=" & "'" & txtFile & "'"

With fs
.NewSearch
.SearchSubFolders = True
.MatchTextExactly = True
.LookIn = "C:\Documents and Settings\MyPath"
.FileName = "*.xls"
If .Execute = 0 Then
Exit Sub
End If
If .Execute(SortBy:=msoSortbyFileName, SortOrder:
=msoSortOrderAscending) > 0 Then
For I = 1 To .foundfiles.Count
CurrentFile = .foundfiles(I)
Length = Len(CurrentFile)
CurrentFile = Right(CurrentFile, Length - 53)
rst.AddNew
rst!file = CurrentFile
rst.Update
If DCount("file", "qryGeneral", strCriteria) > 0 Then
rst.Delete
End If
Next
End If
End With

rst.Close
Me.Requery

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top