> - Search a record with P_ID or lastname or firstname (P_ID is Pri key)
> called Form1
[quoted text clipped - 4 lines]
>
> - A Form3 open to edit this record from previous search (Form2)
I think I understand what you want to do here: but forgive me if I have
got the question wrong. You want to force the user to use a search form
before opening a form on that one record. What I do is this:
Create a search form: the top half of this has a couple of text boxes for
the stuff to search on, and a command button called search. The command
button runs code to create a SELECT query that uses the contents of the
text boxes, usually with LIKE operators rather than =. The results of
this query are displayed in a list box in the lower half, along with
command buttons OK and Cancel. Cancel unloads the form, OK simply hides
it. Other logic in the form caters for <new> record requests, etc. A
custom form method returns the selected value from the list box: more on
this later.
Create a Public Function that calls the search form in dialog mode. When
this returns, if the form is not loaded, then the user cancelled out, and
the function returns an error code. If the form is loaded and hidden,
then use the custom method to retrieve the requested record id, and
unload the form. If the requested record is a valid record, then return
that ID from the function. If it is the code for a <new> record, create a
new record and return a new identifier from the function.
OK so far?
The bit that actually works is calling the function from the Form_Open
event (of the main form). If the function returns the cancelled error,
then set the Cancel argument and Access will close the form before it
appears. If the function returns a valid record, then you create a sql
command like "select * from mytable where table_id = 10445" and poke it
into the RecordSource of the form.
The whole sequence is something like:
DoCmd.OpenForm MainForm
-> Private sub Form_Open(Cancel as Integer) ' on Main Form
dwRecordNum = GetARecordNumber()
-> public function GetARecordNumber() as Long
docmd.Openform SearchForm
-> SearchForm is eventually hidden by user clicking "OK"
-> return to GetARecordNumber()
If Not IsLoaded("SearchForm") Then
' user cancelled
GetARecordNumber = -1
ElseIf Forms!SearchForm.GetSelectedRecord = 0 Then
' <new> record requested
GetARecordNumber = MakeANewRecord()
DoCmd.Close SearchForm
Else
' return the selected number
GetARecordNumber = Forms!SearchForm.GetSelectedRecord
DoCmd.Close SearchForm
End If
-> back in Form_Open
If dwRecordNum = -1 Then
' user cancelled
Cancel = True
Exit Sub
Else
strSQL = "SELECT * FROM MyTable WHERE TableID = " & dwRecordNum
Me.RecordSource = strSQL
End If
The smart thing to do then is put a command button called OK on the main
form, which calls the code a bit like this:
' relaunch the search form and get a new recordsource
Call Form_Open(Cancel)
' does the user want to stop doing this now?
If Cancel = True then DoCmd.Close acForm, Me.Name
' otherwise it's just reopened on the new record anyway
It sounds a lot more complicated than it really is; but looks pretty
seamless to the user.
Hope it helps
Tim F
MN - 08 Mar 2005 20:37 GMT
Tim Thanks you a lot ...
But could you help me from beginner :-( I am not expert for Access. So
please patient with my dumb question.
My form1 have:
P_ID
Lastname
Firstname
Agency
Record source: dbo_personinfo
So, How can I created a button call Search on click event() and How it will
display in a list box on lower half of the form? The user can search by
Lastname if they could not remember P_ID (?)
Also, I got an error 2465 "Application-defined or Object-defined error" at
command line:
"Forms!SearchViewrecord.getselectrecord = 0 Then"
Hope to hear from you soon,
Regards,
MN
> I think I understand what you want to do here: but forgive me if I have
> got the question wrong. You want to force the user to use a search form
[quoted text clipped - 90 lines]
>
> Tim F
Tim Ferguson - 09 Mar 2005 18:01 GMT
> So, How can I created a button call Search on click event() and How it
> will display in a list box on lower half of the form?
The solution I suggested does require quite a solid understanding of
programming, custom forms design, events and so on. In general it is hard
to get very much that is useful out of Access without some ability with VBA
and the rest.
That said, perhaps it would be best for you to lower your sights from a
full-blown custom search form etc. Have you tried Query-by-form? -- this
requires no programming and is useable straight from the access user
interface. Your fastest answer may be just to train your users to do that.
All the best
Tim F
MN - 09 Mar 2005 18:53 GMT
Thank you.
>
> > So, How can I created a button call Search on click event() and How it
[quoted text clipped - 13 lines]
>
> Tim F