View Full Version : stupid code tricks


David R
02-07-2002, 10:12 AM
I'm not sure if I've outsmarted myself or what. I want the control holding my index field to be locked to prevent accidental changes to existing records, but allow a double-click to unlock it. However it always stays locked, except yesterday it was always staying unlocked...??

Here's the relavant code snippets:

Private Sub Form_Current()
'Interfaces with ParticipantID_Lookup_AfterUpdate() to clear the lookup box and where to set the cursor
If (Me.ParticipantID_Lookup = "") Then 'No lookup, just browsing records
Me.ParticipantID_Lookup.SetFocus
Exit Sub
Else

If Me.NewRecord = False Then
'Old record, don 't want to change the ParticipantID unwittingly
Me.ParticipantID.Enabled = False
Me.ParticipantID.Locked = True
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^This was working, now it doesn't for new records.
Me.FirstName.SetFocus

Else
'New record, import the data stored in Me.ParticipantID_Lookup
Me.ParticipantID.Enabled = True
Me.ParticipantID.Locked = False
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^This was working, now it doesn't for new records.
'If (Me.ParticipantID_Lookup = "") Then Me.ParticipantID = Me.ParticipantID_Lookup
Me.ParticipantID.SetFocus
End If

Me.ParticipantID_Lookup = ""

End If

End Sub

Private Sub ParticipantID_GotFocus()
Me.ActiveControl.SelStart = 0
End Sub

Private Sub ParticipantID_BeforeUpdate(Cancel As Integer)
If (Me.NewRecord = False) Then
If (Me.ParticipantID = "" Or IsNull(Me.ParticipantID) = True) Then Me.Undo
End If

If DCount("[ParticipantID]", "tableParticipants", "[ParticipantID]= '" & Me![ParticipantID] & "'") = 1 Then
MsgBox "This is a duplicate Participant ID."
Me.Undo
Cancel = True
End If

End Sub

Private Sub ParticipantID_DblClick(Cancel As Integer)
Me.ParticipantID.Enabled = True
Me.ParticipantID.Locked = False
End Sub


Any ideas peripheral to this are as always appreciated.

David R

David R
02-08-2002, 06:55 AM
Followup information: Apparently when the form is initially opened (either by opening the database or from the Database window), the Part.ID field is locked in existing records. If I go into design view and back to the form, it is unlocked. However if I open it in Design View initially and then go to Form View, the fields remain locked (that first time).
This mystifies me since I thought going from Design View to Form View reran all the opening code.

Then again, this is Microsoft we're talking about...

David R
02-08-2002, 02:20 PM
Further information: If I use the lookup box, the field immediately becomes locked again, even after the Form View-Design View-Form View trick. The DblClick Event is just not working apparently in any way shape or fashion, or is being overridden by the Form Current event (more likely). If that's the case, how do I DISable the Form Current event? Set a form-level global variable with the double click event and check that in Form Current? That makes no sense...

My next step if no one has any suggestions is to import it all into a new form.

Alexandre
02-08-2002, 03:27 PM
Think about the enable property as an option to activate/deactivate your control. When not enabled, the control won t be sensitive to events like double clicking.
Does it enlightnen you?

Alex

David R
02-11-2002, 09:15 AM
Actually that makes perfect sense. Ahh well, the "Law of Unintended Consequences" strikes again.

Thanks, just Locking it should have the desired effect.
David R