The .csv file you enclosed is not a .csv file. CSV stands for comma separated value, these are actually semi colon delimited (
. And each row starts with a " and ends with a " basically indicating to access that the entire row is one big text string. Is this actually how the data comes to you or are there no " marks in your incoming file?
If you remove those " marks you can create an import spec that will properly import the data (either name it something new or overwrite the spec I gave you). If your file does actually come with each line enclosed in " marks you may have to write an addition to the script I gave you that opens the file and removes the " marks it finds then saves it.
Secondly, your field on the end Estimated cost at the end of the year has commas in odd places for instance, these are the two values you have in your last field. Neither one of these is a number format, am I to assume the , (comma) mark is actually supposed to be a . (period)
12584,22042857
115,93741936
Third, you've modified and butchered the SQL statements. Some of your data has a ' (apostrophe) mark in it so you're going to have to modify the SQL statements further. Here is the code I'm running on the button now and it correctly adds all the information.
Code:
Dim sFileName As String
Dim db As Database
Dim rst As Recordset
Dim sSQL As String
Dim sMonthLabel1 As String
Dim sMonthLabel2 As String
Dim sMonthLabel3 As String
Dim sMonthLabel4 As String
If IsNull(lstFileName) Then
MsgBox "Kies voor het importeren een bestand"
Else
Set db = CurrentDb
DoCmd.SetWarnings False
DoCmd.RunSQL ("DELETE * FROM tblRawImport")
DoCmd.SetWarnings True
sFileName = CurrentProject.Path & "\" & Me.lstFileName
Debug.Print sFileName
DoCmd.TransferText acImport, "ImportSpecNew", "tblRawImport", sFileName, False
sSQL = "SELECT * FROM tblRawImport"
Set rst = db.OpenRecordset(sSQL)
rst.MoveFirst
sMonthLabel1 = rst.Fields("FieldA")
sMonthLabel2 = rst.Fields("FieldB")
sMonthLabel3 = rst.Fields("FieldC")
sMonthLabel4 = rst.Fields("FieldD")
rst.MoveNext
Do While rst.EOF <> True
sSQL = "INSERT INTO tblImportedData (Company, Debtor, [Cost Type], MonthLabel, MonthAmount) VALUES "
sSQL = sSQL & "(""" & rst.Fields("company") & """, """ & rst.Fields("debtor") & """, """ & rst.Fields("cost type") & """, """ & sMonthLabel1 & """," & rst.Fields(3) & ")"
Debug.Print sSQL
db.Execute sSQL
sSQL = "INSERT INTO tblImportedData (Company, Debtor, [Cost Type], MonthLabel, MonthAmount) VALUES "
sSQL = sSQL & "(""" & rst.Fields("company") & """, """ & rst.Fields("debtor") & """, """ & rst.Fields("cost type") & """, """ & sMonthLabel2 & """," & rst.Fields(4) & ")"
Debug.Print sSQL
db.Execute sSQL
sSQL = "INSERT INTO tblImportedData (Company, Debtor, [Cost Type], MonthLabel, MonthAmount) VALUES "
sSQL = sSQL & "(""" & rst.Fields("company") & """, """ & rst.Fields("debtor") & """, """ & rst.Fields("cost type") & """, """ & sMonthLabel3 & """," & rst.Fields(5) & ")"
Debug.Print sSQL
db.Execute sSQL
sSQL = "INSERT INTO tblImportedData (Company, Debtor, [Cost Type], MonthLabel, MonthAmount) VALUES "
sSQL = sSQL & "(""" & rst.Fields("company") & """, """ & rst.Fields("debtor") & """, """ & rst.Fields("cost type") & """, """ & sMonthLabel4 & """," & rst.Fields(6) & ")"
Debug.Print sSQL
db.Execute sSQL
rst.MoveNext
Loop
Set db = Nothing
End If
NOTE I am NOT adding the last field to the table because, from the sound of it, it is a budget of sorts which should be on a separate table.
Lastly, you *STILL* have not said whether your data changes month to month
Take the example file you sent.
It covers months 2011-04 through 2011-07. the file you receive for 2011-08 will have the months 2011-05 through 2011-08. Will the fields 2011-05 through 2011-07 have the exact same numbers as the previous month or will they change. If they change do you want to overwrite the existing data? if they do not change do you want to only add the new month?
For instance
Code:
2011-04 2011-05 2011-06 2011-07
1000 1500 2000 2500
Let's say this is your data for the current file
Next month will you get this:
Code:
2011-05 2011-06 2011-07 2011-08
1500 2000 2500 3000
where the three first months are identical to the previous months last three months or can those values change month to month.
The method I've given you adds the data regardless of what happens. but you are going to have to modify the program based on what is actually the case with your system. If the data for the three overlapping months you have is going to be identical the easiest thing to do is to set your primary key to be the company, debtor, cost type and month label. This will prevent duplicates and will only add the new field (or you can modify the code to only add the most recent months' worth of data after your original import)