Link to home
Start Free TrialLog in
Avatar of Sanjay
SanjayFlag for United States of America

asked on

a column is not importing date data into a linked sql table

I am importing data from an excel file (Stock_Status_Report.xls) into a linked SQL table in my MS access application.  The linked table is called dbo_Stock_Status_Report.  This SQL table has a field called "Next Delivery Date".  The excel file also has the same field name.  The docmd.transferspreadsheet method imports all of the data except for the date data in the column Next Delivery Date with no errors.  I have no idea why this is happening.  COuld really use expert help.


Sub Excel_Import_Stock()

Dim table1 As String, table2 As String, table3 As String
Dim db As DAO.Database
Dim tbl As DAO.TableDef
Dim indx As DAO.Index
Dim PrimaryKey As String
Dim qry As String

Dim xlObj As Excel.Application
Dim shtName As String
Dim xlPath As String, xlFile As String
Dim filArr(), j

DoCmd.SetWarnings False

table1 = "dbo_Stock_Status_Report"
table2 = "dbo_On_Hand_Value"

SaveEmailAttachments_Stock    'SAVES OHM E-MAIL EXCEL ATTACHMENTS TO DESKTOP

Excel_Rename_Columns

filArr = Array("Stock_Status_Report.xls", "On_Hand_Value.xls")
xlPath = GetUSERPROFILE() & "\Desktop"
Set xlObj = CreateObject("Excel.Application")

'import the excel file
For j = LBound(filArr) To UBound(filArr)
    xlObj.Workbooks.Open xlPath & "\" & filArr(j)
    shtName = xlObj.Worksheets(1).Name
    If filArr(j) = "Stock_Status_Report.xls" Then
        DoCmd.RunSQL "DELETE * FROM " & table1
        DoCmd.TransferSpreadsheet acImport, , table1, xlPath & "\" & filArr(j), True, shtName & "!"
'    ElseIf filArr(j) = "On_Hand_Value.xls" Then
'        DoCmd.RunSQL "DELETE * FROM " & table2
'        DoCmd.TransferSpreadsheet acImport, , table2, xlPath & "\" & filArr(j), True, shtName & "!"
    End If
Next

For j = LBound(filArr) To UBound(filArr)
    xlObj.Workbooks.Close
    Set xlObj = Nothing
    On Error Resume Next
    Kill xlPath & "\" & filArr(j)    'Delete the Excel files
Next

xlObj.Workbooks.Close
Set xlObj = Nothing

TerminateExcelProcess

'x_DropStockImportErrorsTables

'Add_Current_Date_Time

DoCmd.SetWarnings True

MsgBox "Stock data import complete."

End Sub





Sub Excel_Rename_Columns()

Dim xlObj As Excel.Application
Dim xlPath As String, shtName As String
Dim filArr(), j
Dim i As Integer

DoCmd.SetWarnings False

filArr = Array("Stock_Status_Report.xls")
xlPath = "C:\Users\sanjayg\Desktop"
Set xlObj = CreateObject("Excel.Application")

For j = LBound(filArr) To UBound(filArr)
    xlObj.Workbooks.Open xlPath & "\" & filArr(j)
    shtName = xlObj.Worksheets(1).Name
    For i = 1 To xlObj.Worksheets(shtName).UsedRange.Columns.Count
        If xlObj.Worksheets(shtName).Cells(1, i).Value = "Description" Then
            xlObj.Worksheets(shtName).Cells(1, i).Value = "Desc"
        ElseIf xlObj.Worksheets(shtName).Cells(1, i).Value = "Annual Dep. Usage" Then
            xlObj.Worksheets(shtName).Cells(1, i).Value = "Annual Dep Usage"
        ElseIf xlObj.Worksheets(shtName).Cells(1, i).Value = "Annual Indep. Usage" Then
             xlObj.Worksheets(shtName).Cells(1, i).Value = "Annual Indep Usage"
        End If
    Next
   
    xlObj.Worksheets(shtName).Columns("H:H").NumberFormat = "m/d/yyyy"
   
Next

xlObj.ActiveWorkbook.Save
'xlObj.Workbooks.Close
Set xlObj = Nothing

TerminateExcelProcess

'DoCmd.SetWarnings True

End Sub
Avatar of Sanjay
Sanjay
Flag of United States of America image

ASKER

This worked.  Had to save the file first as fileformat = 56.




Sub Excel_Rename_Columns()

Dim xlObj As Excel.Application
Dim xlPath As String, shtName As String
Dim filArr(), j
Dim i As Integer

DoCmd.SetWarnings False

filArr = Array("Stock_Status_Report.xls")
xlPath = "C:\Users\sanjayg\Desktop"
Set xlObj = CreateObject("Excel.Application")

For j = LBound(filArr) To UBound(filArr)
    xlObj.Application.DisplayAlerts = False
    xlObj.Workbooks.Open xlPath & "\" & filArr(j)
    xlObj.ActiveWorkbook.SaveAs xlPath & "\" & filArr(j), FileFormat:=56
Next

Set xlObj = Nothing

TerminateExcelProcess

filArr = Array("Stock_Status_Report.xls")
xlPath = "C:\Users\sanjayg\Desktop"
Set xlObj = CreateObject("Excel.Application")

For j = LBound(filArr) To UBound(filArr)
    xlObj.Workbooks.Open xlPath & "\" & filArr(j)
    shtName = xlObj.Worksheets(1).Name
    For i = 1 To xlObj.Worksheets(shtName).UsedRange.Columns.Count
        If xlObj.Worksheets(shtName).Cells(1, i).Value = "Description" Then
            xlObj.Worksheets(shtName).Cells(1, i).Value = "Desc"
        ElseIf xlObj.Worksheets(shtName).Cells(1, i).Value = "Annual Dep. Usage" Then
            xlObj.Worksheets(shtName).Cells(1, i).Value = "Annual Dep Usage"
        ElseIf xlObj.Worksheets(shtName).Cells(1, i).Value = "Annual Indep. Usage" Then
             xlObj.Worksheets(shtName).Cells(1, i).Value = "Annual Indep Usage"
        End If
    Next
   
    xlObj.Worksheets(shtName).Columns("H:H").NumberFormat = "m/d/yyyy"
   
Next

xlObj.ActiveWorkbook.Save
'xlObj.ActiveWorkbook.SaveAs xlPath & "\" & filArr(j), FileFormat:=56
'xlObj.Workbooks.Close
Set xlObj = Nothing

TerminateExcelProcess

'DoCmd.SetWarnings True

End Sub
Avatar of Scott McDaniel (EE MVE )
I would assume this is because of the format of the Dates in the Excel sheet.

Try moving the data into a local Access table first, and then see what that column looks like.

In general, it's better to import to a local Access table first, and then use standard SQL to move the data over to SQL Server. This allows you to have much more control over the import, and allows you to detect problems with the data before it gets to the live tables.
ASKER CERTIFIED SOLUTION
Avatar of Sanjay
Sanjay
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.