I need to say two things before I start. One, "Thank You" to anyone who takes the time to read this and respond. Two, I am very much a newbie so bear with my stupid questions.
I have an Access database that is growing beyond what I am comfortable having in Access. I also look at this a way to become more comfortable with SQL Server. One of the tasks that my Access database does is import spreadsheets that we get back from remote users. We send them a spreadsheet with their current forecast. These remote users modify their forecast and then send back the Excel spreadsheets. These sheets do not have a constant name, instead I keep a log of imported files and compare to that each time a file attempts to be imported. The following is my VBA code that does all of this...
Public Sub import_spreadsheets()
Dim rst As Recordset, sql As String
Dim strDirPath, MyName As String
DoCmd.SetWarnings False
On Error GoTo cant_import
'strDirPath = "C:\My Documents\Access\Import Files\"
strDirPath = "\\Uinet4a\Vol1\Shared\Product Supply\Access Projects\Prod Supply Database\Imp_Files\"
write_log "Import Spreadsheets Process", "Start"
'Delete existing temp table
write_log "Delete Temp Table", "Start"
CurrentProject.Connection.Execute "DELETE * FROM tblTemp", , adExecuteNoRecords
write_log "Delete Temp Table", "End"
' Display the names in C:\ that represent files.
MyName = clean_comments(Dir(strDirPath, vbNormal)) ' Retrieve the first entry.
Do While MyName <> "" ' Start the loop.
'If Left(MyName, 4) = "imp_" And Right(MyName, 4) = ".xls" Then
If Right(MyName, 4) = ".xls" Then
'Test to see if the file has already been imported
sql = "SELECT * FROM tbl_imported_files " & _
"WHERE (((file_name)='" & MyName & "'))"
Set rst = New Recordset
rst.Open sql, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
If rst.BOF = True And rst.EOF = True Then 'this files has yet to be imported
'Write filename to table
rst.AddNew
rst!file_name = MyName
rst!trans_date = Date
rst.Update
write_log "Import file - " & MyName, "Start"
'Table Long File Name Sheet Range
'DoCmd.TransferSpreadsheet acImport, 8, "tblTemp", strDirPath & MyName, True, "Forecast!Excel_Master"
DoCmd.TransferSpreadsheet acImport, 8, "tblTemp", strDirPath & MyName, True, "all_data"
write_log "Import file - " & MyName, "End"
End If
rst.Close
End If
MyName = Dir ' Get next entry.
Loop
write_log "Import Spreadsheets Process", "End"
Exit Sub
cant_import:
Select Case Err.Number
Case 3011, 2391 'Can't Import Sheet
write_log "Failed Spreadsheet Import " & MyName, "Error"
DoCmd.SendObject , , , "me@XXX.com", "my_boss@XXX.com", "", "Import Error", "Spreadsheet " & MyName & " Failed", False, """"
Resume Next
Case Else
write_log "MAJOR spreadsheet Import Error", "Error"
End Select
End Sub
Can anyone tell me how I can me how I can do the same thing from within SQL Server? I can set up a DTS Package to import a single spreadsheet, but I can't figure out how to pass it a name as a variable. It might just be that I am looking at the problem the wrong way. Sometimes I get stuck in the "Well in Access I would ...." frame of mind.
Thank You,
Scott
I have an Access database that is growing beyond what I am comfortable having in Access. I also look at this a way to become more comfortable with SQL Server. One of the tasks that my Access database does is import spreadsheets that we get back from remote users. We send them a spreadsheet with their current forecast. These remote users modify their forecast and then send back the Excel spreadsheets. These sheets do not have a constant name, instead I keep a log of imported files and compare to that each time a file attempts to be imported. The following is my VBA code that does all of this...
Public Sub import_spreadsheets()
Dim rst As Recordset, sql As String
Dim strDirPath, MyName As String
DoCmd.SetWarnings False
On Error GoTo cant_import
'strDirPath = "C:\My Documents\Access\Import Files\"
strDirPath = "\\Uinet4a\Vol1\Shared\Product Supply\Access Projects\Prod Supply Database\Imp_Files\"
write_log "Import Spreadsheets Process", "Start"
'Delete existing temp table
write_log "Delete Temp Table", "Start"
CurrentProject.Connection.Execute "DELETE * FROM tblTemp", , adExecuteNoRecords
write_log "Delete Temp Table", "End"
' Display the names in C:\ that represent files.
MyName = clean_comments(Dir(strDirPath, vbNormal)) ' Retrieve the first entry.
Do While MyName <> "" ' Start the loop.
'If Left(MyName, 4) = "imp_" And Right(MyName, 4) = ".xls" Then
If Right(MyName, 4) = ".xls" Then
'Test to see if the file has already been imported
sql = "SELECT * FROM tbl_imported_files " & _
"WHERE (((file_name)='" & MyName & "'))"
Set rst = New Recordset
rst.Open sql, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
If rst.BOF = True And rst.EOF = True Then 'this files has yet to be imported
'Write filename to table
rst.AddNew
rst!file_name = MyName
rst!trans_date = Date
rst.Update
write_log "Import file - " & MyName, "Start"
'Table Long File Name Sheet Range
'DoCmd.TransferSpreadsheet acImport, 8, "tblTemp", strDirPath & MyName, True, "Forecast!Excel_Master"
DoCmd.TransferSpreadsheet acImport, 8, "tblTemp", strDirPath & MyName, True, "all_data"
write_log "Import file - " & MyName, "End"
End If
rst.Close
End If
MyName = Dir ' Get next entry.
Loop
write_log "Import Spreadsheets Process", "End"
Exit Sub
cant_import:
Select Case Err.Number
Case 3011, 2391 'Can't Import Sheet
write_log "Failed Spreadsheet Import " & MyName, "Error"
DoCmd.SendObject , , , "me@XXX.com", "my_boss@XXX.com", "", "Import Error", "Spreadsheet " & MyName & " Failed", False, """"
Resume Next
Case Else
write_log "MAJOR spreadsheet Import Error", "Error"
End Select
End Sub
Can anyone tell me how I can me how I can do the same thing from within SQL Server? I can set up a DTS Package to import a single spreadsheet, but I can't figure out how to pass it a name as a variable. It might just be that I am looking at the problem the wrong way. Sometimes I get stuck in the "Well in Access I would ...." frame of mind.
Thank You,
Scott