Hi,
I'm trying to use this code to import data from an excel spreadsheet directly into a table in my database. I've copied and pasted most of this code so I'm sure I'm missing something. As well, the OpenAsTextStream(strPath, ForReading, 0)....does this not work for excel files????
Can you please direct me to the right solution.
Here is the code:
txtPath.SetFocus
strPath = txtPath.Text
Set oXL = CreateObject("Excel.Application"
Set fFolder = fso.GetFolder(strPath)
Set oWB = oXL.Workbooks.Open(fFile.Path)
Set oWS = oWB.Worksheets(1)
If oWS.Cells(9, 1) <> "Date" Or _
oWS.Cells(9, 2) <> "Invoice #" Or _
oWS.Cells(9, 3) <> "Description" Or _
oWS.Cells(9, 5) <> "Card" Or _
oWS.Cells(9, 6) <> "Site Address" Or _
oWS.Cells(9, 9) <> "Grade" Then Exit Sub
Else: Do
strsql = "SELECT * FROM tbl_Transactions"
Set db = CurrentDb
Set rs = db.OpenRecordset(strsql, dbOpenDynaset)
Set fs = CreateObject("Scripting.FileSystemObject"
Set f = fs.OpenAsTextStream(strPath, ForReading, 0)
rs.AddNew
rs!Date = Format(oWS.Cells(12, 1), "mm/dd/yyyy"
rs!Invoice# = oWS.Cells(12, 2)
rs!Description = oWS.Cells(12, 3)
Loop
f.Close
rs.Close
Set db = Nothing
Set rs = Nothing
Set f = Nothing
Set fs = Nothing
End If
oWB.Close False
DoEvents
Me.Refresh
oXL.Quit
Thanks in advance.
TN
I'm trying to use this code to import data from an excel spreadsheet directly into a table in my database. I've copied and pasted most of this code so I'm sure I'm missing something. As well, the OpenAsTextStream(strPath, ForReading, 0)....does this not work for excel files????
Can you please direct me to the right solution.
Here is the code:
txtPath.SetFocus
strPath = txtPath.Text
Set oXL = CreateObject("Excel.Application"
Set fFolder = fso.GetFolder(strPath)
Set oWB = oXL.Workbooks.Open(fFile.Path)
Set oWS = oWB.Worksheets(1)
If oWS.Cells(9, 1) <> "Date" Or _
oWS.Cells(9, 2) <> "Invoice #" Or _
oWS.Cells(9, 3) <> "Description" Or _
oWS.Cells(9, 5) <> "Card" Or _
oWS.Cells(9, 6) <> "Site Address" Or _
oWS.Cells(9, 9) <> "Grade" Then Exit Sub
Else: Do
strsql = "SELECT * FROM tbl_Transactions"
Set db = CurrentDb
Set rs = db.OpenRecordset(strsql, dbOpenDynaset)
Set fs = CreateObject("Scripting.FileSystemObject"
Set f = fs.OpenAsTextStream(strPath, ForReading, 0)
rs.AddNew
rs!Date = Format(oWS.Cells(12, 1), "mm/dd/yyyy"
rs!Invoice# = oWS.Cells(12, 2)
rs!Description = oWS.Cells(12, 3)
Loop
f.Close
rs.Close
Set db = Nothing
Set rs = Nothing
Set f = Nothing
Set fs = Nothing
End If
oWB.Close False
DoEvents
Me.Refresh
oXL.Quit
Thanks in advance.
TN