Full Version: How do I manage this control?
UtterAccess Discussion Forums > Microsoft® Access > Access Forms
arenaninja
Hey UA. I'm using the Security Demo code I found on this forum for user access in my current project. I have a form that, when a new record is entered, captures the data from the global gSecurityID.

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

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).

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)
MarkLiquorman
The logical conclusion is that cmbFkNotesID is not Null! Have you checked that out? For example, if the FkNotesID field is an integer, then it might have a default value of 0; but this column might be hidden (if you are display a description in your combobox, not the underlying ID#), so the combobox display is blank.

Because of such situations, when dealing with comboboxes I prefer to check the ListIndex property rather than the Value property, like this:

CODE
If Me.cmbFkNotesID.ListIndex = -1 then
    Cancel = True
arenaninja
Thanks Mark. That seems to do it. However, now it seems that Access is now not recognizing that no one is logged on (gSecurityID has no value). I'll probably switch it from IsNull to If gSsecurityID = 0.

Again, thanks.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.