I would like to be able to open a form, filtered on one record via a
unique id, but have the form only display that record, i.e. not enable
the user to scroll through, or navigate to other records.
The way I do this is to
(a) design the form in bound mode, but delete the recordsource property
before saving it for the "last" time.
(b) use the Form_Load event to call a function like
public function GetNewRecordID(ByRef PKValue as Long) As Integer
' returns 0 if the user cancelled,
' returns 1 if it's a valid recordID
' returns 2 if it user requests a new empty record
' (sometimes this function will create the new record and
' return the new RecordID instead)
'
' If I wasn't so lazy I'd create this as an Enum instead!
which displays some kind of search dialog or way of selecting a new
record by the users. Back to the main form, it's something like this:
Private sub Form_Load(Cancel as integer)
dim newRecord As Integer
dim newRecordID as Long
' ask the user for a new record to look at
newRecord = GetNewRecordID(newRecordID)
Select Case newRecord
Case 0 ' user cancelled
Cancel = True
Exit Sub
Case 1 ' valid ID, easy
' actually I usually modularised better than this...
me.RecordSource = "SELECT This, That, TheOther " & _
"FROM MyTable " & _
"WHERE PKValue = " & Format(newRecordID)
' this is necessary if renewing
Me.Requery
Cancel = False ' not strictly necessary, but it's a reminder
Case 2 ' new record; if you don't want to allocate it directly
' the below is air code; I don't usually do it this way
newRecordID = GetNextVacantRecordID()
me.DataEntry = True
me.GoToNewRecord
me!txtRecordID.Value = newRecordID
Cancel = False ' not strictly necessary, but it's a reminder
Case Else
' oops....
Msgbox globalSystemErrorMessage
Cancel = True
End Select
End Sub
and don't forget the cmdExit button:
private sub cmdExit_Click()
dim Cancel as Integer
' don't want to see the form in the background with the
' old data
Me.Visible = False
' load a new record into the form
Call Form_Load(Cancel)
If Cancel = True then
' user wants to go home, close down the form
DoCmd.Close acForm, me.Name
Else
' otherwise become visible again with the new data
me.Visible = True
End If
End sub
This way the user just sees a cycle of select record -- edit record and
can simply cancel out of the select record dialog when she is bored.
Hope that helps
Tim F