473,516 Members | 2,898 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4749
> 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
2275
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 of a performance hit I'm taking using this technique versus using 'copy' to copy directly into a container with elements in place? Thanks, d
0
4472
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 procedure from SQL Server vs MS Access point of view ?
8
9212
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 is entered, either from a form or from the table view of the table, when the record gets saved it immediately displays #DELETED# in all of the...
8
6275
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 one-to-one relationship but on this occasion I must keep username/password details within a seperate table. Here's the basic specs and database schema:...
2
3169
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 : INSERT INTO temp_tab VALUES (1,2,3)
3
2301
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 example I had to go by 'INSERT INTO tblCustomers (CustomerID, , )
6
3440
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 using MS-Access 2000 database table for this app. Note that the datatype of all the fields mentioned above are Text. Apart from the above columns,...
1
3174
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 common to ADOdb $db = &ADONewConnection('access'); # create a connection $db->PConnect('evdb'); # connect to MS-Access, evdb DSN $db->debug =...
5
4446
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 (maybe QueryDefs?), so I wouldn't have to use the IDE and open Access to do it? Here's more information if that helps: I can open Access itself and...
2
6098
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 { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // set the connection for database path String Database_Path = "E:/Segi/Project/Coding/Car_Park_Storage.mdb";
0
7273
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7182
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7134
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7547
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5709
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4768
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
1615
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
823
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
482
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.