Webmatrix - Conditional database changes? - razor

So i have an issue, where that properties on my website are either active or inactive. A property is deemed as inactive by default, and i want it to only become active once it has met some criteria. For example, i only want it to become active when a description is provided, at least one rate has been applied, and only when it has a valid ZIP code assigned.
How would i achieve this, since a user might not populate all this information upon first registration, and may want to come back at a later date to finish updating before it becomes active.
The only way i can think of, is to run an "if" statement, each time i update information on a property, which i can work with, but i was wondering if there was any other conditional way of making this happen, where the database regularly checks to see if should set something to active?
Thanks, Gavin

If you were running a full version of SQL Server, you could accomplish this with a trigger and a bitflag column like VoidKing suggested. On the save the trigger would trigger and you would write a complex sql statement that test if all the needed fields are filled and set the bitflag to true or false depending.
I did something like this in a simpler way by having two save buttons. A "save" that didn't trigger any validation and a "Save and Release" button that applied validation to all the needed fields. Both buttons saved the data, but the save and release button would only save if the validation passed and it would save the bitfield "isActive" to 1 where the other save method would save it as a 0.
Might be a good approach else you are left with your checking logic on the single save button, which isn't a bad way to go either.

Related

Updateable subform adds "#http://#" to end of hyperlink field when edited

I have a form with a subform that lists current material information being entered on the main form, like a log/history view.
On the subform there is a field that specifies which skid the product was assigned. I made this field a hyperlink so that I could use the OnClick event to launch a report and be able to extract row information needed to generate the proper summary.
That works great, and I thought it was finished until I attempted to edit the skid number from within the subform.
Allowing edits in the subform is a project requirement. Editing the skid number in the hyperlinked field breaks my code. Access automatically appends #http://# to the end of whatever value I enter into the cell and updates the table with this string. That row then gets omitted from the report because I'm keying off of the skid numbers. By design, this hyperlink has no path or address, I just use it to determine which row is being clicked.
How can I prevent Access from appending the #http://# while keeping the "Is Hyperlink" property to Yes? Is there another property to set this behavior or should I use the AfterUpdate event to undo the addition, which just seems like a waste of resources?
I am running Access 2010.
There is a long and very thorough article here that I will not copy ;) It elaborately states why its a bad idea to even use the hyperlink-field and what to do to get the same result you need without a hyperlink-field.
Basically, the main point of the hyperlink-field is all that behind the scenes shenanigans you cannot do anything about - and thats not what you want as a programmer.
The Hyperlink-field internally consists of three parts: A Text-part, a Link-Part and a Tooltip-Part. And although you can access them separately, access always tries to create a link.
You can try to format the data you have in the internally used format (DisplayText#Address#SubAddress) but I think that is not what you need...
So I think you best take Philipps advice and change your field-type.

How to know if ADOQuery.Post will change database?

I have a project in Delphi 7 that uses a database in MySQL to store some configuration.
Whenever I change a config, a "Save" button is enabled. The OnClick procedure in this button calls TADOQuery.Edit, Select the fields with the SQL property, TADOQuery.Open and set the various FieldsByName. In the end, it TADOQuery.Post the configuration and Requery it.
This works well, only if at least one of these fields is actually changed.
If, for example, I check a checkBox (originally unchecked) and then unCheck it again, the Save button would go enabled, but the actual data in database doesn't change. In this case, when I call Post, an Exception is raised.
I saw this question that would solve my problem, checking if there ara any modification, but as soon as I set the first field, the Modified property of TADOQuery becomes true, invalidating this solution.
Another option would be to check, before setting the field, if it will actually change, setting an flag to, in the end, actually post it or not. But, there are hundreds of fields to do that, which will be pretty boring to do.
A third alternative I thought is to create a new field in database with a "Last Modified" datestamp, which forces to always have at least one modification, but I prefer to not mess with the existing database.
Is there any other way to know if a TADOQuery.Post will trigger an exception, because no data has really changed? How can I solve this problem? Or there's a simple workaround for it?
The ADOQuery variable is dynamically created in the Save button's routine (and free'd in the end).
It would be a nice approach to use CheckBrowseMode() instead of Post().
ADOQuery1.CheckBrowseMode
CheckBrowseMode():
Automatically posts or cancels data changes when the active record changes.
You can read more about here:
http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/DB_TDataSet_CheckBrowseMode.html

MS Access - Form tries to create new record on open and record jumps about a continuous form

So I have two issues with my database, which I think are linked together.
Firstly if tblDeliveries is empty and frmMainView is opened it comes up with "You must enter a value in the 'tblDeliveries.deliverySlot' field" but the form shouldn't be trying to create a record and I can't see any reason why it would. Opening frmDailyView (which is a subform of frmMainView) doesn't show this behaviour but it is the one with the continuous form on...
Secondly if tblDeliveries has one (or more) rows the first row of the table will jump about... For example in frmMainView if the date of the first record is set to 23/01/2015 and you navigate to the 24/01/2015 and click anywhere on the subform and then press F5 to refresh the record will have jumped to that date, the date even changes in tblDeliveries.
I have no idea why it's doing it, I even removed all of the VBA code for the form to stop it, but it happens anyway.
If anyone has come across anything like this before, please let me know how you fixed it, or any suggestions would be very welcome. Thanks!
EDIT: If you download the database, add your computer login name to tblUsers. Also the date is UK formatted, so might need tweaking to work with US dates.
The offending database (1.5MB)
Taking a look at your database, several issues are occurring.
You first issue can be resolved by going into the Design View of the table tblDeliveries, select the deliverySlot field and change Required (in bottom panel) from Yes to No.
Your second issue is multi-faceted with many issues but a critical problem is that the parent-child link field, deliveryDate, between the main form and subform is missing in the recordsource of the sub form and the field's table origin is not in the query's join statements. For me, I can't even update anything in subform (all boxes are grayed out).
Essentially, you are trying to normalize Delivery Dates and Delivery Times (one-to-many) but the left join query (qryDeliveryIncBlanks) setup is not aligned to this effect. You user login also does not integrate smoothly into frmMainView. My ultimate advice is revamp your database design relational model then use forms as the visual representation of those related tables.
I am not re-iterating the points of #Parfait.
Access Forms have a property DataEntry. If it is set to Yes (or True in VBA), then the form will open itself pointing to a new empty data record. Set this property to No (False in VBA).
If you want to give the user the possibility to manually enter a new record, set the property AllowAdditions to Yes and to No otherwise. Yes displays an empty record marked with a star * and the end of the data sheet or continuous form.
I've resolved the issue, thanks to some of the comments I realised I could just give the deliverySlot field a default value with a number outside of the range it would look for, I set it to 99, but it only looks for 1-20. The date still jumps around but since it's not in the range of the query it's hidden. It's resolved both issues.

Handling #DELETED messages on records that have been deleted while a form is active

There may be a better way to do this, but currently, if the user enters i value that is not allowed (in this case, a project that has been closed), then it notifies them. However, because it's on a sub-form linked to the table, it automatically created a record anyway. This is how it's currently handled (if a bad input was entered):
Clear the fields of that record
Delete the record from the table
Requery / Refresh
Currently it works fine, except it leaves #DELETED in all the controls associated with the records. However, if I hit F5, it disappears. Is there a way to make it so the deleted record disappears (like how it does when F5 is hit) programmatically?
I think you have a couple options to handle this. The main thing is that you want to avoid deleting the record and try to handle it before it ever becomes a saved record. You should be able to do this using one of several different form events (not control events): Dirty, BeforeUpdate or BeforeInsert.
If you use the Dirty event, check to see if the project is closed and check to see if the record they are working on is a NewRecord. If both are true then try using Cancel = True and/or Me.Undo.
If you use the BeforeUpdate event instead, it works much the same way. You'll probably need to cancel the edits and undo the edits as I mentiond directly above, in which order I'm not sure.
I don't have a good way to test the BeforeInsert event at the moment but I think you can prevent new records from coming into existence inside this event by using Cancel = True.
If you cannot handle the record before it gets saved, and you must truly clear #DELETED from your form, you'll probably need to use a Me.Requery statement. Exactly how this will work depends on whether or not the form is bound and what it is bound to but it sounds like you are using a fairly standard Access form that is bound to a table or SQL statement so I believe the Requery statement should work.

Is there a way to override automatic record updating of Access 2007 forms created with Form Wizard?

I'm new to Access VBA, and have created a form using the Form Wizard that displays records in a table. That's a piece of cake.
The behavior that I get with the form, though, is that updates to the records occur automatically when I move around records.
What I'd like is to have the updates occur only when I click an "Update" button that I put in the form.
It seems like I can construct the form from scratch, update all of the (unbounded) controls programmatically, and then update the record from the controls programmatically, but this seems like too much work.
Is there a way to "turn off" the automatic updating behavior from within Access, or using VBA code?
Thanks!
As Robert suggested, you can avoid saving a changed record by canceling the before update event. The code would look something like this:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Me.Undo
Cancel = True
End Sub
However, that approach requires you discard any changes you've made to the record before you can move off it. Without Me.Undo, you can cancel the update, but Access won't allow you to move to a different record. You must either save or discard the changes to the current record before moving to another.
If you want to move to another record but not discard changes first, I think you need to try a disconnected recordset. It's an ADO recordset you create in memory, not bound to any data source. You can add a command button to save your changes on command. If it sounds useful, see this article by Danny Lesandrini at Database Journal: Create In-Memory ADO Recordsets
If users are accidentally changing data and moving record to record causing updates, maybe you should have an Edit button to only start editing when needed. You can use other suggested code to either undo the changes if they move to another record or prevent them from moving at all unless they save or cancel.
Is there a way to "turn off" the
automatic updating behavior from
within Access, or using VBA code?
You can try cancelling the Form.BeforeUpdate event, unless a flag is set. When the button is clicked, set the flag, set Form.Dirty to false (to save the data), and then clear the flag in the Form.BeforeUpdate event handler.
Why would you want to? Access works on a record by record basis. Not like Excel which only saves the spreadsheet when you choose to save or exit.
It sounds like you have a continuous form with multiple records on the screen. The only way to do this would be to use a "temporary" table and the save the contents of the "temporary" table to the permanent table once you're ready. However then you will lose the ability to determine if someone else has changed the records unless you do a lot more work.