chanman525
IS-IT--Management
Hey all. I have a database with some code already built in to import files from .txt files into the database. I'm trying to errorproof it so that if this is run twice in one day, it won't import the same information twice. I would like to use Dlookup to see if the date already exists in the "End Date" field, then don't run the import. But of course, I'm terrible at writing code so I'm looking for help. Here is my code....
Function import()
On Error GoTo import_Err
Dim receive As Database
Dim add_date As Field
Dim input_date As TableDef
Dim rec As Recordset
Dim path As String
Dim file_name As String
Dim import_filename As String
Dim month As String
Dim strFileExists As String
Dim year As String
Dim currentmonth As String
Dim test As String
Set receive = CurrentDb()
Set rec = receive.OpenRecordset("input_date")
path = "g:\prorep\tranhist\Receive\"
file_name = (rec!add_date & ".txt")
import_filename = path & file_name
strFileExists = dir(import_filename)
If strFileExists <> "" Then
year = Left$(file_name, 4)
month = Mid$(file_name, 5, 2)
currentmonth = year & month
If test = DLookup("[End Date]", "currentmonth", "[End Date]" = Date) Then
Quit acQuitPrompt
Else
import_filename = path & file_name
DoCmd.TransferText acImportDelim, "receive spec", currentmonth, import_filename, False, ""
End If
' Delete input_date table
Set rec = receive.OpenRecordset("input_date")
Do
rec.Delete
rec.MoveNext
Loop Until rec.EOF
import_Exit:
Exit Function
import_Err:
MsgBox Error$
Resume import_Exit
End If
End Function
A lot of the code was already there, basically my line is the Dlookup, but the code is going right through it. Can someone help me out?
Function import()
On Error GoTo import_Err
Dim receive As Database
Dim add_date As Field
Dim input_date As TableDef
Dim rec As Recordset
Dim path As String
Dim file_name As String
Dim import_filename As String
Dim month As String
Dim strFileExists As String
Dim year As String
Dim currentmonth As String
Dim test As String
Set receive = CurrentDb()
Set rec = receive.OpenRecordset("input_date")
path = "g:\prorep\tranhist\Receive\"
file_name = (rec!add_date & ".txt")
import_filename = path & file_name
strFileExists = dir(import_filename)
If strFileExists <> "" Then
year = Left$(file_name, 4)
month = Mid$(file_name, 5, 2)
currentmonth = year & month
If test = DLookup("[End Date]", "currentmonth", "[End Date]" = Date) Then
Quit acQuitPrompt
Else
import_filename = path & file_name
DoCmd.TransferText acImportDelim, "receive spec", currentmonth, import_filename, False, ""
End If
' Delete input_date table
Set rec = receive.OpenRecordset("input_date")
Do
rec.Delete
rec.MoveNext
Loop Until rec.EOF
import_Exit:
Exit Function
import_Err:
MsgBox Error$
Resume import_Exit
End If
End Function
A lot of the code was already there, basically my line is the Dlookup, but the code is going right through it. Can someone help me out?