|
rdouglass -> RE: Update access Table from an Excel table in ASP (4/30/2008 22:46:16)
|
OK, are you familiar with arrays? When you say UPDATE do you mean just UPDATE or UPDATE and INSERT where necessary? If you know there's always a "1 to 1" relationship, you can read the Excel file into an array and then loop thru the array building your update statememnts. It's kinda' like saying "get me started on building a car". It's hard to get you started without knowing what kind of vehile you want and what you have to work with. OK, having said that; this is probably the basics you're looking for. 1. Let's assume for simplicity sake that you're key is an Access PK field called ID and the matching ID is in the first column of the spreadsheet. 2. Let's also assume that we have 1 text field to update. In the Access DB, it'll be called 'myText' and it will be the second column in the Excel file. So we assume you already have your connection to the DB built but we're *not* going to use recordsets but rather 'getrows' which will put our Excel data into an array:
'First, read your Excel data into an array:
Set conntemp = Server.CreateObject("ADODB.Connection")
conntemp.Open "DRIVER=Microsoft Excel Driver (*.xls); DBQ=" & Server.MapPath("/fpdb/myFile.xls")
set rstemp=conntemp.execute("Select * From [$sheet1]")
IF Not rstemp.eof THEN
myArray = rstemp.getrows
End IF
rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
'Now set the Access DB connection and loop thru the Excel date
'creating UPDATEs as we go
Set conntemp = Server.CreateObject("ADODB.Connection")
conntemp.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("/fpdb/myDB.mdb")
FOR i = 0 TO ubound(myArray,2)
conntemp.execute("UPDATE myTable SET myText = '" & myArray(1,i) & "' WHERE ID = " & myArray(0,i))
NEXT
conntemp.close
set conntemp=nothing
Now I didn't do any error checking nor checked any syntax and that's strongly reccommended; it's right from memory so I'm sure there's probably at least 1 problem with it. But I think the concept should get through. 1. read the data into an array using getrows. 2. loop thru the array creating UPDATE statements 3. execute each update during the loop Does that make sense at all? If not, try brushing up on arrays. We're using a 2 dimensional array here.
|
|
|
|