473,418 Members | 2,142 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,418 software developers and data experts.

SQL Statement and Table connection issues

15
Hello all,

I am using an SQL select statement to store the result into a variable. This variable then needs to get into a table. I am having issues with it. Kindly help. I am providing below the code.

'First table variables
Dim sql_insert, con, data_source

'Second table variables
Dim sql_insert2, con2, data_source2

'Opens connection for writing to Purchases
data_source = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source = "&_
Server.MapPath("orders.mdb")

'Opens connection for writing to Suppliers
data_source2 = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source = "&_
Server.MapPath("orders.mdb")

'Statement that writes data to Supplier table (address comes from a form from before. Not an issue so far)
sql_insert2 = "insert into suppliers (address) values ('" & address & "') "
Set con2 = Server.CreateObject("ADODB.Connection")
con2.open(data_source2)
con2.Execute(sql_insert2)

'My trouble begins here. I need to now find the last value added into the Supplier table (from above) and make it a value into my next insertion of record into the Purchases table. And here is my logic that does not work.

sql_insert2 = "SELECT max(supplierID) FROM Supplier"
Set con2 = Server.CreateObject("ADODB.Connection")
con2.open(data_source2)

'this message prints back the message 'SELECT max(supplierID) from Supplier instead of the last value written. Supplier ID is an autonumber in the access table.So I need to capture this value into a local variable and write it to the Purchase table.

Response.Write sql_insert2

'This will write the new entry into Purchase Table along with latest Supplier ID which is not working.

sql_insert = "insert into purchase (username, description,price,qty,budgetline,supplierID)) values ('" & username & "', '" & description & "', '" & price & "', '" & qty & "', '" & budgetline & "', '" & supplierID & "') "

Set con = Server.CreateObject("ADODB.Connection")
con.open(data_source)
con.Execute(sql_insert)

Response.Write ("Record has been added successfully.")

con.Close
Set con=Nothing

%>
Sep 7 '07 #1
3 3441
shweta123
692 Expert 512MB
Hi,

Response.Write sql_insert2
This statement will print sql statement and not the value of last SupplierID.

If you want to get the Max Supplier Id you should write

Dim rs
rs =server.CreateObject("Adodb.Recordset")
sql_insert2 = "SELECT max(supplierID) FROM Supplier"
set rs = con.Execute(sql_insert2)
''''''This will print Supplier ID
Response.write(rs(0))
rs.close
Sep 7 '07 #2
markrawlingson
346 Expert 100+
There are two problems here...

1. Where is your recordset object? All you are doing here is setting your SQL statement into a variable, then opening a connection to your database. You're not passing your SQL statement into the actual database itself therefore no information is returned - you've just got another open connection object.

sql_insert2 = "SELECT max(supplierID) FROM Supplier"
Set con2 = Server.CreateObject("ADODB.Connection")
con2.open(data_source2)
2. You're setting the variable SupplierID into an insert statement to insert the value of the SupplierID variable into the database. However, You're not setting the value of supplierID. Therefore, SupplierID is NULL - so it gets set into your database column as a null or empty value. Even worse is if that particular column in your database does not allow null values to be entered - it will throw an error.

sql_insert = "insert into purchase (username, description,price,qty,budgetline,supplierID)) values ('" & username & "', '" & description & "', '" & price & "', '" & qty & "', '" & budgetline & "', '" & supplierID & "') "
In addition, and this is merely a suggestion.. You don't need to have two identical connection objects open on your page, and you don't need to open them several times. It can get confusing pretty quickly, and each open object just eats up more resources. You can just open one connection object at the top of your page and pass that same connection object into as many recordset objects or execution statements as you like.


Check the code below - it should solve your problem.

Expand|Select|Wrap|Line Numbers
  1. <%
  2. 'Set your datasource and open your connection object.
  3. data_source = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source = "&_
  4. Server.MapPath("orders.mdb")
  5. 'Create the object and open it. We now have an open connection object that we can use as many times as we like.
  6. Set con = Server.CreateObject("ADODB.Connection")
  7. con.open(data_source)
  8.  
  9. 'Set an SQL insert statement and execute it against the connection object.
  10. sql_insert = "insert into suppliers (address) values ('" & address & "') "
  11. con.Execute(sql_insert)
  12.  
  13. 'Set an SQL statement to grab the MAX value of supplierID.
  14. sql_insert = "SELECT max(supplierID) FROM Supplier"
  15. 'Create a recordset object which will hold the information that we are trying to retrieve from the database
  16. Set rs = Server.CreateObject("ADODB.RecordSet")
  17. 'We open the recordset, passing the SQL statement and the connection string to it so it knows what to return.
  18. rs.Open sql_insert, con, 3, 3
  19.  
  20. 'We pull out the field we want and set it into a variable.
  21. SupplierID = rs("SupplierID")
  22.  
  23. 'We close and cleanup our recordset object because we no longer require it, we have the information we were looking for.
  24.  
  25. rs.Close
  26. Set rs = nothing
  27. 'Insert a new record into the purchase table with the values that we have.
  28. sql_insert = "insert into purchase (username, description,price,qty,budgetline,supplierID)) values ('" & username & "', '" & description & "', '" & price & "', '" & qty & "', '" & budgetline & "', '" & supplierID & "') "
  29. con.execute(sql_insert)
  30.  
  31. 'close and cleanup the connection object
  32. con.close
  33. Set con = nothing
  34. %>
  35.  
Sep 7 '07 #3
irkahs
15
Thanks Shweta and Mark.

Shweta,
Thank you for the rs(0) concept. I'd have never guessed that one. Works just fine.

Mark,
I actually did do something similar to what you wrote. Now it works perfectly.

Your timely help is truly appreciated.

Cheers!
Sep 8 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

35
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
1
by: Ryan | last post by:
I've just inherited a system and have some concerns about the speed of connections to a remote server (SQL2000). If I do a simple select statement on the table below, it takes 14 minutes to retrive...
6
by: David | last post by:
I am using ADO to run some SQL statements in Access. First, I run query to create a table using SELECT . . . INTO Table A. Then, I run a query to SELECT . . . FROM Table A to get some data and...
1
by: solomon_13000 | last post by:
connection.asp: <% Sub RunQueryString (pSQL,parms) on error resume next Set conn = Server.CreateObject("ADODB.Connection") conn.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &...
2
by: Amber | last post by:
On my DB2 UDB 8.2.7 database user User1 has been granted IMPLICIT_SCHEMA privilege, but when he issues "create schema test", the error message "SQL0552N, USER1 does not have the previliege to...
4
by: Fred Zuckerman | last post by:
I have a A2K database that includes some linked tables (these links are SQL views). Sometimes there are "issues" with the SQL server and the users receive a timeout error whenever they try to...
3
by: Joshepmichel | last post by:
Please to help me to following problem I want to do this 1. create Table Name MEMBER on the Database Name "mytestdb", 2. Add the Values to the Table through the Key board Inputs during running...
1
flexsingh
by: flexsingh | last post by:
Hello there I have kinda got gotten myself into a sticky situation. I am trying to do something which seams too big to do in my head but I feel I kinda know how to do it. My problem is I have a...
4
by: Roger | last post by:
on sql 2005, I've got a view with select permission granted, the view just "select * from table" using odbc in access97, I linked this view and I create a query to retrieve certain fields the...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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...

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.