I've found out how to import a CSV into my Access database via another question. But in the answer there is no information on how to target specific columns upon import. How can I be sure that the correct columns from the CSV get placed into the correct columns in my database?
I need to be able to edit/add-to the column values as they get imported using classic ASP. Examples:
- The 4th column in the CSV is a userid, I need to be able to add "@domain.com" to the end as it enters the database for an email column.
- The 6th column is a classification: if it's value is "Teacher" then, as it gets imported, I need to change it to "Classroom".
I'm working with some old code & DB's that I didn't create. Believe me, I'd be using something else if I could, so no pot-shots for using MS Access, please.
I guess this whole thing boils down to this:
- if the previous question's answer really allows me to treat the CSV the same as a db table, then how can I select the columns as in a real db table?
// Update:
I've set everything up how I believe it should be, but I'm receiving an error:
ADODB.Recordset error '800a0bb9'
Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.
/datazone_bkup/Rollover/importTeachers.asp, line 12
line 12: CSV.open "SELECT * FROM teachers.csv", conn
My full code is as follows:
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &_
Server.MapPath("\path\to\file") & ";Extended Properties='text;HDR=no;FMT=Delimited';"
Set connCSV = Server.CreateObject("ADODB.Connection")
connCSV.Open strConn
Set CSV = Server.CreateObject("ADODB.recordset")
CSV.open "SELECT * FROM teachers.csv", conn
do until CSV.eof
UserName = CSV.Fields(4)
LastName = CSV.Fields(1)
FirstName = CSV.Fields(2)
MiddleName = CSV.Fields(3)
School = CSV.Fields(5)
if CSV.Fields(6) = "Teacher" then
SecLevel = "Classroom"
end if
Active = "Yes"
TeacherNumber = rsCSV.Fields(0)
rsCSV.movenext
sql = "INSERT INTO tblTeacher (UserName, LastName, FirstName, MiddleName, School, SecLevel, Active, TeacherNumber) " &_
"VALUES (" &_
"'" & UserName & "','" & LastName & "','" & FirstName & "','" & MiddleName & "'," &_
"'" & School & "','" & SecLevel & "','" & Active & "','" & TeacherNumber & "'" &_
")"
on error resume next
conn.execute(sql)
loop
@HansUp, when I tried using the !
as in rsDB!UserName
, I received a Syntax Error
. So I went back to setting variables for the CSV column values.
Again, thanks for any help.