?
Solved

Question: Excel to Access Table via ASP (Classic/IIS5.1/XP)

Posted on 2006-03-30
8
Medium Priority
?
310 Views
Last Modified: 2012-06-21
Ok im getting this error:

Error Type:
Microsoft OLE DB Provider for ODBC Drivers (0x80040E37)
[Microsoft][ODBC Microsoft Access Driver] Could not find output table 'tbl_tickets'.
/database/spreadsheet-import.asp, line 75

From the code below:
==============
Answer Caveat: Im a newbie! So I need to understand (fundamentally) what im doing wrong. So please be kind enough to point out every concern in the code with suggestions and/or recommendations for improvement (and why).
Most appreciated :->

Now the offending code:

<%@LANGUAGE="VBScript"%>
<%
OPTION EXPLICIT
RESPONSE.BUFFER = TRUE

%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>
 <title>Importing Records from Access into dbase</title>
</head>

<body>
<%
'excel set-up
'==========================
 dim Xpath       'path to excel file
 dim X             'excel connection object
 dim Rx       'excel recordset object
 dim xSql       'sql string to get data from excel spreadsheet

      Xpath       = Server.Mappath("/db/tbl_records.xls")
Set X             = Server.CreateObject("ADODB.CONNECTION")
Set Rx             = Server.CreateObject("ADODB.RECORDSET")


'dbase set-up
'===========================
dim objCon 'db connection string
set objCon = Server.CreateObject( "ADODB.Connection" )
      objCon.OPEN "DSN=dsnProc"
 
'create db command & recordset objects  
'========================================
dim objCmd, objRS
set      objCmd = Server.CreateObject( "ADODB.Command" )
set objCmd.ActiveConnection = objCon
set objRS  = Server.CreateObject( "ADODB.Recordset" )
 

      'Open the connection to the excel spreadsheet
      X.Open "Driver={Microsoft Excel Driver (*.xls)}; " &_
      "DBQ=" & Xpath

      'create SQL string
      xSql = "Select * from [Bus_0001$]"

      Rx.open xSql, X
      Rx.MoveFirst
      
      'create SQL string to insert into database looping through spreadsheet
      'then add each record
      
    Dim sql_Insert
     
     while not Rx.eof
      sql_Insert = "INSERT INTO tbl_tickets " &_
      "(source_name, patron_name, ticket_price, init_deposit, Deposit1, Deposit2, Deposit3, final_balance) " &_
      "VALUES (" &_
      "'" & Rx("source_name").value & "', " &_
      "'" & Rx("patron_name").value & "', " &_
      "" & Rx("ticket_price") & ", " &_
      "" & Rx("init_deposit") & ", " &_
      "" & Rx("Deposit1") & ", " &_
      "" & Rx("Deposit2") & ", " &_
      "" & Rx("Deposit3") & ", " &_
      "" & Rx("final_balance") & ");"
             objCmd.CommandText = sql_Insert
     set objRS = objCmd.Execute       

      'check string: Response.write(objCmd.CommandText & "<br>")
     Rx.moveNext
     Wend

      Rx.Close
Set Rx = NOTHING

      X.Close
Set X = NOTHING

      objRS.CLOSE
set objRS = NOTHING
      
%>
</body>

</html>
0
Comment
Question by:the_sleeper
  • 4
  • 4
8 Comments
 
LVL 1

Expert Comment

by:uncle_ho
ID: 16333907
If tbl_tickets does not already exist in the mdb, you cannot create it through an INSERT statement.  The easiest solution would be to create an empty table (tbl_tickets) with the appropriate structure in the mdb first.  Alternatively, it could be generated by a SELECT INTO statement or through ADOX (which is an extension of ADO used to manipulate database objects), but that is getting slightly more complicated.  

Also, using the adExecuteNoRecords option for the command object's execute statement will prevent a recordset being returned unnecessarily.  This eliminates some overhead costs.  E.g.:

objCmd.Execute , adExecuteNoRecords
0
 

Author Comment

by:the_sleeper
ID: 16334334
Greetings @uncle_ho

  @uncle_ho>>
[1]  If tbl_tickets does not already exist in the mdb, you cannot create it through an INSERT statement.
[2] The easiest solution would be to create an empty table (tbl_tickets) with the appropriate structure in the mdb first.

 1. Yes. tables does exist.
 2. Thats what I did first. by "appropriate structure", i assume you mean each Field in the DB matches a cell in the Spreadsheet. If so. Yes. Table Structure is as follows:

tbl_tickets
-----------
source_id autonum pk
source_name
0
 

Author Comment

by:the_sleeper
ID: 16334434
Sorry hit the wrong key...restarting response.

 @uncle_ho [1]  If tbl_tickets does not already exist in the mdb, you cannot create it through an INSERT statement.
 @uncle_ho [2] The easiest solution would be to create an empty table (tbl_tickets) with the appropriate structure in the mdb first.

 1. Yes. tables does exist.
 2. Thats what I did first. by "appropriate structure", i assume you mean each Field in the DB matches a cell in the Spreadsheet. If so. Yes. Table Structure is as follows:

bl_tickets
-----------
source_id          autonum pk
source_name     text
patron_name     text
ticket_price        Currency  
init_deposit        Currency  
Deposit1            Currency  
Deposit2            Currency  
Deposit3            Currency  
final_balance     Currency  

 @uncle_ho [3] Also, using the adExecuteNoRecords option for the command object's execute statement will prevent a recordset being returned unnecessarily.  This eliminates some overhead costs.  

 [3] Ok. Read up on it. Cool. Only problem is...(from code)...

             objCmd.CommandText = sql_Insert
     set objRS = objCmd.Execute, adExecuteNoRecords      

Generates the following error below. Can u provide code example? Thanks.

Error Type:
Microsoft VBScript compilation (0x800A0401)
Expected end of statement
/database/spreadsheet-import.asp, line 70, column 26
set objRS = objCmd.Execute, adExecuteNoRecords
-------------------------^

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Accepted Solution

by:
uncle_ho earned 2000 total points
ID: 16334580
Can you verify the DSN is setup correctly and is pointing to the right mdb?  It seems to be able to open up the connection without any problems, but if the mdb is present and has the table already in it, I don't know why else you would be receiving that error.

Re: adExecuteNoRecords
You can eliminate the entire left part of the line that calls the Execute command (i.e. take out "set objRS = ").  Since you are not returning any value, you can't use that method call to create a recordset.  I forgot to mention you will probably need to define adExecuteNoRecords at the top of your code:

Const adExecuteNoRecords = 128
0
 

Author Comment

by:the_sleeper
ID: 16334878
OK, now we're getting somewhere.

1. You were right. Went back and checked my dsn. ....Wrong DSN.   Thanks!
2. Understood on the Const adExecuteNoRecords = 128. Added it to code and works fine. Thanks for the tip!

BUT....Now I get this error:

 "Operation must use an updateable query."

Pointing to this line:  objCmd.Execute, adExecuteNoRecords

Which I modified based on this instruction:

@uncle_ho  >> You can eliminate the entire left part of the line that calls the Execute command
                        (i.e. take out "set objRS = ").

Apparantly, I still dont quite get this. Please explain.
0
 
LVL 1

Expert Comment

by:uncle_ho
ID: 16335050
I think this might be an issue with the permissions on the mdb, or the directory in which the mdb resides.  Check out this MSDN KB article:

http://support.microsoft.com/kb/q175168/

You can also try explicitly setting the mode when you open the connenction object.  I don't think this is causing the error you see, but it's not a bad habit to get into specifying mode anyway:

'Include with other variable definitions
Const adModeReadWrite = 3

'Setting up connection object
dim objCon 'db connection string
set objCon = Server.CreateObject( "ADODB.Connection" )
objCon.Mode = adModeReadWrite     'NEW LINE
objCon.OPEN "DSN=dsnProc"
0
 

Author Comment

by:the_sleeper
ID: 16335191
Dude. that was it. Working like a charm now.

Ok... moving all db's to one folder. this is for the birds.

Thanks for your excellent help and patience.

Points coming...
0
 
LVL 1

Expert Comment

by:uncle_ho
ID: 16335350
Glad it worked -- thanks for the points.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Screencast - Getting to Know the Pipeline

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question