473,399 Members | 3,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

insert CSV into access

Hi,

I use the following code to insert the data from my CSV into an access
database. But now I want the script to UPDATE the courses id they
already exist (so when the COURSENUMBER is known in the table),
otherwise he has to add just a new record.

How can I do this?

Thanks!
Joost

<%
'create instance of the Connection object
Set objRS = Server.CreateObject("ADODB.Recordset")
SQLstmt = "SELECT * FROM blaat"
objRS.Open SQLstmt, Conn , adOpenStatic , adLockOptimistic
'declare our variables for the file handling
Dim objFSO , strURL , objFile
'create an instance of the file system object
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
'this is the csv file
strURL = Server.MapPath("test.csv")
'open the file
Set objFile = objFSO.OpenTextFile(strURL)
'while we are not at the end of the file
Do While Not objFile.AtEndOfStream
'store the contents of the file in strText
strText = objFile.readLine
'split the strText
arrText = split(strText, ";", 6)
objRS.AddNew
objRS("coursenumber") = arrText(0)
objRS("place") = arrText(1)
objRS("time") = arrText(2)
objRS("day1") = arrText(3)
objRS("day2") = arrText(4)
objRS("day3") = arrText(5)
objRS.Update
Loop
'close and destroy objects
objRS.Close
objFile.Close
Set objRS = nothing
Set Conn = nothing
Set objFile = Nothing
Set objFSO = Nothing
Response.Write("status ok")
%>
Jul 19 '05 #1
1 4746
> Set objRS = Server.CreateObject("ADODB.Recordset")
SQLstmt = "SELECT * FROM blaat"
Stop doing this, for starters. Why do you need to select the entire table
back to the ASP page, just to add rows?
Do While Not objFile.AtEndOfStream
Stop doing this, also. How about:

arrLines = split(objFile.ReadAll(), vbCrLf)
for i = 0 to ubound(arrLines)
arrText = split(arrLines(0), ";", 6)
...
next
objRS.AddNew
objRS("coursenumber") = arrText(0)
objRS("place") = arrText(1)
objRS("time") = arrText(2)
objRS("day1") = arrText(3)
objRS("day2") = arrText(4)
objRS("day3") = arrText(5)
objRS.Update


Ugh. As you're going through the loop, assuming CourseNumber is, in fact, a
number:

sql = "SELECT courseNumber FROM blaat " & _
" WHERE courseNumber=" & arrText(0)

set rs = conn.execute(sql)

if rs.eof then
sql = "INSERT blaat(coursenumber, ...) " & _
"VALUES(" & courseNumber & ", ...)"
else
sql = "UPDATE blaat SET ... " & _
"WHERE courseNumber = " & arrText(0)
end if
conn.execute sql, , 129
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: deancoo | last post by:
I have gotten into the habit of often using copy along with an insert iterator. There are scenarios where I process quite a lot of data this way. Can someone give me a general feel as to how much...
0
by: ImraneA | last post by:
Hi there I had pleasure of upsizing Access v97 db to Access v2K/SQL 2K. Wish to provide some knowledge gained back to community - hopefully help others. 1.Question how do you test stored...
8
by: Bri | last post by:
Greetings, I'm having a very strange problem in an AC97 MDB with ODBC Linked tables to SQL Server 7. The table has an Identity field and a Timestamp field. The problem is that when a new record...
8
by: Carl | last post by:
Hi, I hope someone can share some of their professional advice and help me out with my embarissing problem concerning an Access INSERT query. I have never attempted to create a table with...
2
by: Geoffrey KRETZ | last post by:
Hello, I'm wondering if the following behaviour is the correct one for PostGreSQL (7.4 on UNIX). I've a table temp_tab with 5 fields (f1,f2,f3,...),and I'm a launching the following request :...
3
by: MP | last post by:
Hi Posted this several hours ago to another ng but it never showed up thought i'd try here. using vb6, ado, .mdb, jet4.0, no access given table tblJob with field JobNumber text(10) 'The...
6
by: rn5a | last post by:
During registration, users are supposed to enter the following details: First Name, Last Name, EMail, UserName, Password, Confirm Password, Address, City, State, Country, Zip & Phone Number. I am...
1
by: Zuggy | last post by:
I'm trying to create a registration/login script using Access 2003. I'm using ADOdb to connect through ODBC. <?php // Connects to your Database include('adodb/adodb.inc.php'); # load code...
5
by: DonnaL | last post by:
I'm using Access 2000, but this question likely pertains to any version of Access. Simply put, is there a programmatic way of inserting a new Query in whatever master system table stores these...
2
by: yeap | last post by:
Hi All, I can't insert java variable into ms access database. I'm using odbc connection to ms access. Below are my coding. try { ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.