I'll assume the search text box is called txtSearch. Firstly in the form's
Current event procedure put:
Dim ctrl As Control
On Error Resume Next
If Me.NewRecord Then
For Each ctrl In Me.Controls
ctrl.Locked = False
Next ctrl
Else
For Each ctrl In Me.Controls
ctrl.Locked = (ctrl.Name <> "txtSearch")
Next ctrl
End If
This locks all controls other than txtSearch when the form moves to an
existing record, but unlocks them if it moves to a new record so that data
for a new record can be entered.
In the AfterUpdate event procedure of the search text box put the following
code:
Dim rst As Object
Dim ctrl As Control
Set rst = Me.Recordset.Clone
With rst
.FindFirst "[Record Number] = " & txtSearch
If Not .NoMatch Then
Me.Bookmark = .Bookmark
On Error Resume Next
For Each ctrl In Me.Controls
If ctrl.Name <> Me.ActiveControl.Name Then
ctrl.Locked = False
End If
Next ctrl
End If
End With
This navigates to the first record with the value entered in the txtSearch
control in the RecordNumber column, if it can be found, and unlocks the
controls. I've assumed that RecordNumber is a number data type, but if it’s
a text data type use:
.FindFirst "[Record Number] = """ & txtSearch & """"
However, rather than using a text box for searching why not use a combo box
with a RowSource of:
SELECT [Record Number] FROM [YourTableName] ORDER BY [Record Number];
Set its AutoExpand and LimitToList properties to True (Yes). A user can
then either scroll down the list, or type in the value, in which case the
first match in the list would be selected as each character is typed in. The
above code will work equally well with a combo box.
If the Record Number column (presumably the primary key) contains arbitrary
numeric values, then you might prefer to hide this column and show a more
meaningful column or columns in the combo box. The following describes how
this is done, using an employees table as an example:
RowSource: SELECT EmployeeID, FirstName & " " & LastName AS FullName
FROM tblEmployees ORDER BY LastName, FirstName;
BoundColum: 1
ColumnCount: 2
ColumnWidths 0cm;8cm
If your units of measurement are imperial rather than metric Access will
automatically convert the last one. The important thing is that the first
dimension is zero to hide the first column and that the second is at least as
wide as the combo box.
Once again the code to execute the search and lock/unlock the other controls
would be the same.
Ken Sheridan
Stafford, England
pfm721 said:
This is an easy concept, but it probably won't have and easy solution. I have
a form with a text box that the user enters a record number to be searched. I
would like to lock all other fields on the form at the start until a record
is found and then unlock them so they can be edited.
Any help is greatly appreciated.