This topic is locked

Save data for a form

3/2/2009 4:01:02 PM
ASPRunnerPro General questions
D
dsmythe author

I have two tables like so:
TableA -> TableB (1:many). TableA has a column called total_purchases
TableB has a column called recent_purchases. I need to put the value of total_purchases into recent_purchases (this column will contain new data as a user enters it).
I've tried to save total_purchases in a session variable to populate recent_purchase, but it is always empty.
So clearly I do not know where to create (and set) the session variable.
I thought to put it into the BeforeProcessAdd event for the ADD Page for TableB, but this doesn't seem to work.
Here is the code I am trying to use.

session("recent_purchases") = dict("total_purchases")
The default value for recent_purchases = session("recent_purchases") - which is always blank.
If I enable debugging, I can see the contents of total_purchases dsiplayed on screen (and it is the correct value)
Thank you

Sergey Kornilov admin 3/2/2009

Dict variables are available in BeforeAdd/AfterAdd evemts. Your code is correct, you just need to move it to proper event.

D
dsmythe author 3/2/2009

Thank you for the pointer, I actually did put the code here:
Function BeforeAdd(dict,message,inline)
' Parameters:

' dict - Scripting.Dictionary object.

' Each field on the Add form is represented as a 'Field name'-'Field value' pair
'** Custom code ****

' put your custom code here

session("recent_purchases") = dict("total_purchases")
BeforeAdd = True
' set BeforeAdd to True if you like to proceed with adding new record

' set it to False otherwise
Also, the default value for the appropriate field is set to session("recent_purchases"). The actual value in the table for total_purchases = 33.
But the form still displays a blank field.
Thanks for any further help.

Sergey Kornilov admin 3/2/2009

Dict variables refer for fields in the same table. Also BeforeAdd event happens right before record is physically added to the database.
If you need retrieve data from another table you need to do this manually using DAL and session variables.

The best place to do this is BeforeProcessAdd.
The value of master table is contained in session variable SESSION(strTableName & "_masterkey1")

More info: http://www.xlinesoft.com/asprunnerpro/docs...n_variables.htm
Data Access Layer:

http://www.xlinesoft.com/asprunnerpro/docs...scess_layer.htm
Sample code:

set rs = TableA.Query("key = " & SESSION(strTableName & "_masterkey1"),"")

Sesson("recent_purchases")=rs("total_purchases")
D
dsmythe author 3/2/2009

thanks again, here is the entire code that I am using (not using dal yet)
Set rsTemp = server.CreateObject("ADODB.Recordset")
strSQL = "select total_purchases from [dbo].[Purchases] where account_id='" & dict("account_id") & "'"

strSQL = strSQL & " and po_num ='" & dict("po_num")& "'"
rsTemp.Open strSQL,dbConnection, 1, 2
if not rsTemp.EOF then

session("recent_purchases") = rsTemp("total_purchases")

else

session("recent_purchases") = "0.00"

end if
rsTemp.close

set rsTemp = nothing
BeforeAdd = True
I get no errors, and the SQL (when debugging) seems fine, no spaces and the account_id and po_num are correctly displayed. Yet, nothing is displayed on the Add Page (not even "0.00" if EOF).
Clearly I am doing something wrong or missing something.
thanks again.

Sergey Kornilov admin 3/2/2009

First thing to try - print SQL query on the web page and test it manually against your database to make sure it returns some data.
If this doesn't help post your application to Demo Account and contact us directly at http://support.xlinesoft.com sending your application URL. It must be something minor.

D
dsmythe author 3/3/2009

Thank you sir. I did so and everything looked fine.
What I eventually did (I think this is where you were going and trying to tell me), was to put the code her:
Sub BeforeProcessAdd(dbConnection)
All is well now.
Also used SESSION(strTableName & "_masterkey1") and SESSION(strTableName & "_masterkey2") as a requirement for accessing the correct data.
Thanks for the assist.