Hi All,
Here is what I am trying to achieve ...
I have a UX component bound to an MS SQL Server 2008 view. That's all working properly. It's only for display, so no update needed on the tables in the view.
I have some additional unbound fields which relate to another table. All good so far.
Now ... here is where I am having trouble and I just can't get my head around the Alpha way of doing things ... I have a button on the UX which when pressed will write add a new record to the unbound table.
Once this button has been pressed, or when the user enters the UX component, if that record already exists in the unbound table, I want to delete the record. If it doesn't exist, I want to add a new record. The whole UX is filtered by UserName, which is a session variable. Because there could be many records in the unbound table relating to the User, just toggling a flag in the UserName table wouldn't work.
The unbound table consists of four fields :
idPK int, Primary key autoinc
PromotionIDFK, int
DateSaved, date
UserName, varchar(120)
There is also an index on UserName and PromotionIDFK which is unique ... Each UserName can only have one PromotionIDFK.
The bound view has the field PromotionID, int, and UserName, varchar(120) which would be the link for me to relate to the unbound data.
Essentially we have a user who can choose promotions and store them as favourites or delete them from their favourite list.
I guess what I am looking for is the code to click a button to access the unbound table, and if a record is found with the values which match the current username and promotion ID, then display it as being flagged as a favourite, and if not found, then add a new record.
My thinking was varied (or is that random!>) ... for example to write a SQL BEGIN TRY, BEGIN CATCH ... so in pseudocode
But catches all errors that might have caused the insert to fail, not just a duplicate key.
Alternatively, I could do something like
But I am uncertain where and how to trap errorcodes in this situation and deal with them.
Then I thought to myself (which can be dangerous) ... Why not look up on entry to the UX with something like the following and then we could perhaps set a flag on the UX to set buttons hidden or shown? So I have also tried
I seem to have trouble grasping how to use the fields from the UX in the SQL ... I come from a very different background where you could just add those 'screen' variables into the SQL. Alpha confuses me on this ... I am sure I will get it once I am shown the light I am sure if I had it hammered into my little brain how to translate the variables in the UX into the SQL query, I could probably work it out from there. I'm newish to both SQL and Alpha, but getting there (I hope).
Hopefully I have explained this adequately. All assistance would be gratefully received and if clarification is required, please don't hesitate to ask.
Thanks,
Phil
Here is what I am trying to achieve ...
I have a UX component bound to an MS SQL Server 2008 view. That's all working properly. It's only for display, so no update needed on the tables in the view.
I have some additional unbound fields which relate to another table. All good so far.
Now ... here is where I am having trouble and I just can't get my head around the Alpha way of doing things ... I have a button on the UX which when pressed will write add a new record to the unbound table.
Once this button has been pressed, or when the user enters the UX component, if that record already exists in the unbound table, I want to delete the record. If it doesn't exist, I want to add a new record. The whole UX is filtered by UserName, which is a session variable. Because there could be many records in the unbound table relating to the User, just toggling a flag in the UserName table wouldn't work.
The unbound table consists of four fields :
idPK int, Primary key autoinc
PromotionIDFK, int
DateSaved, date
UserName, varchar(120)
There is also an index on UserName and PromotionIDFK which is unique ... Each UserName can only have one PromotionIDFK.
The bound view has the field PromotionID, int, and UserName, varchar(120) which would be the link for me to relate to the unbound data.
Essentially we have a user who can choose promotions and store them as favourites or delete them from their favourite list.
I guess what I am looking for is the code to click a button to access the unbound table, and if a record is found with the values which match the current username and promotion ID, then display it as being flagged as a favourite, and if not found, then add a new record.
My thinking was varied (or is that random!>) ... for example to write a SQL BEGIN TRY, BEGIN CATCH ... so in pseudocode
Code:
BEGIN TRY INSERT INTO SAVEDPROMOTIONS (PROMOTIONIDFK, DATESAVED,USERNAME) VALUES (UX_IDPROMOTIONSPK, UX_DATESAVED, UX_USERNAME) CATCH DELETE SAVEDPROMOTIONS WHERE IDPK = IDPROMOTIONSPK END CATCH
Alternatively, I could do something like
Code:
INSERT INTO SAVEDPROMOTIONS (PROMOTIONIDFK, DATESAVED,USERNAME) VALUES (UX_IDPROMOTIONSPK, UX_DATESAVED, UX_USERNAME) IF ErrorMsg 2627 THEN DELETE SAVEDPROMOTIONS WHERE IDPK = IDPROMOTIONSPK END IF
Then I thought to myself (which can be dangerous) ... Why not look up on entry to the UX with something like the following and then we could perhaps set a flag on the UX to set buttons hidden or shown? So I have also tried
Code:
dim LookUpCn as sql::Connection dim args as sql::Arguments LookUpCn.open("::name::conn") table = "SavedPromotions" 'I created arguments based on the Username field (aUserName) and PromotionIDFK (aPromotionID) and DateSaved (aDateSaved) in the UX component. filter = "username=:aUserName AND PromotionIDFK = :aPromotionID" result = ":aDateSaved" filter = evaluate_string(filter) if sql_lookup(LookUpCn,table,filter,result) <> null_value() ui_msg_box("Promotion","Already Saved") 'This never pops up! end if cn.close()
I seem to have trouble grasping how to use the fields from the UX in the SQL ... I come from a very different background where you could just add those 'screen' variables into the SQL. Alpha confuses me on this ... I am sure I will get it once I am shown the light I am sure if I had it hammered into my little brain how to translate the variables in the UX into the SQL query, I could probably work it out from there. I'm newish to both SQL and Alpha, but getting there (I hope).
Hopefully I have explained this adequately. All assistance would be gratefully received and if clarification is required, please don't hesitate to ask.
Thanks,
Phil
Comment