So the code looks like this:
CODE
Private Sub Form_Current()
If Me.NewRecord Then
Me.cmbFkAssociateID.Value = getGlobal("gSecurityID")
Else
'Do nothing
End If
Me.cmbFkAssociateID.Requery
End Sub
If Me.NewRecord Then
Me.cmbFkAssociateID.Value = getGlobal("gSecurityID")
Else
'Do nothing
End If
Me.cmbFkAssociateID.Requery
End Sub
And on the BeforeUpdate, like this:
CODE
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim db As DAO.Database
Dim rst As DAO.Recordset
Set db = CurrentDb
Set rst = db.OpenRecordset("tblAppNotes", dbOpenDynaset)
If Me.NewRecord Then
If IsNull(Me.cmbFkNotesID) Then
Cancel = True
ElseIf IsNull(gSecurityID) Then
Cancel = True
MsgBox "You must be logged on to save Notes.", vbOKOnly, "No username detected"
Else
' Do nothing, save
End If
Else
With rst
.Edit
![fkAssociateID] = Me.cmbFkAssociateID
![notesDate] = Now()
![fkNotesID] = Me.cmbFkNotesID
.Update
End With
End If
End Sub
However, when I click on a record, a new record is added, even though cmbFkNotesID is blank (violating my BeforeUpdate IF).Dim db As DAO.Database
Dim rst As DAO.Recordset
Set db = CurrentDb
Set rst = db.OpenRecordset("tblAppNotes", dbOpenDynaset)
If Me.NewRecord Then
If IsNull(Me.cmbFkNotesID) Then
Cancel = True
ElseIf IsNull(gSecurityID) Then
Cancel = True
MsgBox "You must be logged on to save Notes.", vbOKOnly, "No username detected"
Else
' Do nothing, save
End If
Else
With rst
.Edit
![fkAssociateID] = Me.cmbFkAssociateID
![notesDate] = Now()
![fkNotesID] = Me.cmbFkNotesID
.Update
End With
End If
End Sub
What am I doing to cause this? I've moved the code around a few times, with different results, but something remains offbeat and I don't get the results needed.
What I need is:
- If a user is adding a new record, capture the user (gSecurityID) in cmbFkAssociateID
- If a user is modifying a record, capture that user (gSecurityID) in cmbFkAssociateID
- Since it's a continuous form and the last record remain blank, I want that blank row to display the user that is logged on (gSecurityID), not the first value on the table
- And for all existing records, obviously use the control's assigned RowSource (<-- this one is working)