Understand code

Dick7Access

Dick S
Local time
Today, 15:50
Joined
Jun 9, 2009
Messages
4,343
I have a address db that I download years ago and just substituted my data. Works fine, but I would like to learn what is making it work. In a datasheet form It uses the following code to open each record on a separate form. Anybody have a link I can go to, or an Idea what I can google.
Code:
=IIf(IsNull([ID]),"(New)","Open")
 
On a new record, depending on the type of field, the values will be one of three or four choices: The declared default value from the field definition, null, 0, or empty.

Obviously in the case of your ID field, the author anticipated the possibility that a new record would be displayed without having been filled in completely; and further, the ID field must therefore NOT be an autonumber field (because of course that WOULD fill in when you created the record.)
 
Going a bit further, the code line is testing whether ID is null or not, if the former the value is set to "(New)", the latter to "Open".

Presumably there are more than two records, so the field could definitely not been an autonumber as well as the fact the field is a string, not numeric.

ID is a funny name to call the field. For me, it would have been called something like Status.
 
Dick has not said which field is being updated to "(New)" or "Open". It is probably not the ID field itself but a different field (possibly called "Status") that tests if the ID field is null and displays this value.

That would suggest to me that the "ID" field is only populated when the record is saved, so this field shows whether the record being worked on is a new one or an open existing record.

Perhaps Dick could let us know the name of the field and the event that triggers it.
 
That line of code won't open another form.

An autonumber field for a new record displays "(New)" until you start editing the record and it's assigned the next number.
"(New)" is actually NULL.

Autonumbers are meaningless so they are often not displayed.

I guess in this case that a textbox has been formatted to look like a button with the above code as it's control source to set the caption and an onlclick event of something like

Code:
Private Sub Text1_Click()
    'if this is a new record save it
    If NewRecord Then Dirty = False
    
    If Text1 = "Open" Then
	'display selected record
        DoCmd.OpenForm "form2", acNormal, , "id=" & id
    Else
        'no id = blank record. don't do anything
    End If
End Sub
 

Users who are viewing this thread

Back
Top Bottom