Friday, February 6, 2009

Update or Append (Insert) a Single Record

What if you want to update a record, but if its not already in the table you want to insert it (Append query in Access).

One way is to check if the record is there with a select statement and then either update or append.

But easier is to run the update statement and then check the recordsAffected property of the database object to see if the value is equal to 0, if not run the append (Insert) query.

For example:

sqlUpdateStatement = "Update field1 where field 2 = somevalue"

dbs.execute sqlUpdateStatement , dbFailOnError

if dbs.RecordsAffected = 0 then
dbs.Execute sqlAppendStatement
end if

What about multiple records?

Well, then you'd have to know the count of the records you want updated (a select statement run priorly), then run the update, check if records affected was less, if so append where id not in (records already in the table).

Tuesday, January 6, 2009

Triggering an Event via VB Code - Name of event = string, ie dynamic

Lets say you're using a calendar control to populate a text box. (I recommend Allen Browns version). You set it up and everything works just fine, however you want the afterUpdate event to trigger after the calendar form updates the textbox value. Because the value is updated via code (visual basic) the event does not trigger.

I've researched for quite awhile trying to figure out how to make it trigger but I couldn't find a solution - raiseEvent? Seems to have something more to do with creating a new class with events and then raising them (dim withEvents aControl new control - never used this before).

callByName - they seems like a possibility but couldn't figure it out.

Eval (eventname as text) didn't work.

The only solution (that is NOT really dynamic) that works is a case or if statement that says if the control.name = the control name that has the event that I want to trigger then [call] thatControls_actualEventName.

Please note that the event (sub procedure) that you want to call from another form possible must then be a public sub (or function) instead of a private sub (or function)

Any better solutions - actual dynamic solutions?