Gary Williams
05-23-2006, 04:24 PM
Hi All,
I need to insert many new records into a remote access database table. I found this script (globalUpdate.asp) on an asp code site, that works perfectly to insert a single record, comprising the data in the form, into a table.
What I need to do is expand this solution so that instead of inserting one record, insert a batch of records. That is, replace the form with a csv file of new records exported fron an excel file, and loop around the script (globalUpdate.asp) until all records have been inserted.
I have also found the final script 'open csv as recordset' which will create the necessary recordset for the new records to be inserted. How do you get globalUpdate.asp to loop around the csv record set, or is it the other way around?
Regards
Gary
************ code for Form ********************
<FORM ACTION="/scripts/globalUpdate.asp" METHOD=POST>
Name: <INPUT TYPE=TEXT NAME=Name SIZE=40>
Phone Number: <INPUT TYPE=TEXT Name=PhoneNumber SIZE=15>
Email Address: <INPUT TYPE=TEXT NAME=Email SIZE=25>
<INPUT TYPE=HIDDEN NAME=TableName VALUE="Contact">
<INPUT TYPE=SUBMIT>
</FORM>
**************code for globalUpdate.asp ******************
<%
Option Explicit
'Our ADO constants we'll need
Const adOpenForwardOnly = 0
Const adLockOptimistic = 3
Const adCmdTable = &H0002
'Create our connection object and open a connection to our database
Dim objConn
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.ConnectionString = "DSN=TestDB"
objConn.Open
'Create a recordset object
Dim objRS
Set objRS = Server.CreateObject("ADODB.Recordset")
'Open a table view for the table name specified by Request("TableName")
Dim strTableName
strTableName = Request("TableName")
objRS.Open strTableName, objConn, adOpenForwardOnly, adLockOptimistic, adCmdTable
'Add a new record...
objRS.AddNew
'Iterate through the fields of the table...
Dim fld
For Each fld in objRS.Fields
'If a value for the column name was passed in,
'set the column name equal to the value passed through the form...
if Len(Request(fld.Name)) > 0 then
fld.Value = Request(fld.Name)
end if
Next
'We're done, so update the record
objRS.Update
'Clean Up...
objRS.Close
Set objRS = Nothing
objConn.Close
Set objConn = Nothing
'Send the user to some confirmation page
Response.Redirect "RecordAdded.htm"
***************code for open csv as a recordset*********
Dim connCSV As New ADODB.Connection
Dim rsTest As New ADODB.Recordset
Dim adcomm As New ADODB.Command
Dim path As String
path = "C:\Testdir\" 'Here Test dir is the Directory where
' the text file is located. don't write the file name here.
'This is connection for a text file without Header
'connCSV.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
& path & ";Extended Properties='text;HDR=NO;FMT=Delimited'"
'This is connection for a text file with Header (i.e., columns
connCSV.Open "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" _
& path & ";Extensions=asc,csv,tab,txt;HDR=NO;Persist Security Info=False"
rsTest.Open "Select * From test.txt", _
connCSV, adOpenStatic, adLockReadOnly, adCmdText
Do While Not rsTest.EOF
MsgBox rsTest(0) 'You can select the required data
rsTest.movenext
Loop
**************************
I need to insert many new records into a remote access database table. I found this script (globalUpdate.asp) on an asp code site, that works perfectly to insert a single record, comprising the data in the form, into a table.
What I need to do is expand this solution so that instead of inserting one record, insert a batch of records. That is, replace the form with a csv file of new records exported fron an excel file, and loop around the script (globalUpdate.asp) until all records have been inserted.
I have also found the final script 'open csv as recordset' which will create the necessary recordset for the new records to be inserted. How do you get globalUpdate.asp to loop around the csv record set, or is it the other way around?
Regards
Gary
************ code for Form ********************
<FORM ACTION="/scripts/globalUpdate.asp" METHOD=POST>
Name: <INPUT TYPE=TEXT NAME=Name SIZE=40>
Phone Number: <INPUT TYPE=TEXT Name=PhoneNumber SIZE=15>
Email Address: <INPUT TYPE=TEXT NAME=Email SIZE=25>
<INPUT TYPE=HIDDEN NAME=TableName VALUE="Contact">
<INPUT TYPE=SUBMIT>
</FORM>
**************code for globalUpdate.asp ******************
<%
Option Explicit
'Our ADO constants we'll need
Const adOpenForwardOnly = 0
Const adLockOptimistic = 3
Const adCmdTable = &H0002
'Create our connection object and open a connection to our database
Dim objConn
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.ConnectionString = "DSN=TestDB"
objConn.Open
'Create a recordset object
Dim objRS
Set objRS = Server.CreateObject("ADODB.Recordset")
'Open a table view for the table name specified by Request("TableName")
Dim strTableName
strTableName = Request("TableName")
objRS.Open strTableName, objConn, adOpenForwardOnly, adLockOptimistic, adCmdTable
'Add a new record...
objRS.AddNew
'Iterate through the fields of the table...
Dim fld
For Each fld in objRS.Fields
'If a value for the column name was passed in,
'set the column name equal to the value passed through the form...
if Len(Request(fld.Name)) > 0 then
fld.Value = Request(fld.Name)
end if
Next
'We're done, so update the record
objRS.Update
'Clean Up...
objRS.Close
Set objRS = Nothing
objConn.Close
Set objConn = Nothing
'Send the user to some confirmation page
Response.Redirect "RecordAdded.htm"
***************code for open csv as a recordset*********
Dim connCSV As New ADODB.Connection
Dim rsTest As New ADODB.Recordset
Dim adcomm As New ADODB.Command
Dim path As String
path = "C:\Testdir\" 'Here Test dir is the Directory where
' the text file is located. don't write the file name here.
'This is connection for a text file without Header
'connCSV.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
& path & ";Extended Properties='text;HDR=NO;FMT=Delimited'"
'This is connection for a text file with Header (i.e., columns
connCSV.Open "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" _
& path & ";Extensions=asc,csv,tab,txt;HDR=NO;Persist Security Info=False"
rsTest.Open "Select * From test.txt", _
connCSV, adOpenStatic, adLockReadOnly, adCmdText
Do While Not rsTest.EOF
MsgBox rsTest(0) 'You can select the required data
rsTest.movenext
Loop
**************************