Searching using own forms (1 Viewer)

callumwatson

Registered User.
Local time
Today, 09:04
Joined
Jun 22, 2001
Messages
22
I would like to be able to search for a specific person ID in a table called person. However I want to use the person's first name a nd surname as my search criteria. I would like to provide the means for my user to type in the name and for the database to look up the ID and return it. i know this is simple but when I try to work through it I keep producing forms that change my data. I don't know how to use unbound text boxes to look up my table.
 

kgcrowther

Registered User.
Local time
Today, 09:04
Joined
Jun 1, 2001
Messages
52
I'm glad there a question that I can actually answer. But there's more than one thing that needs to be done. This is one way to do it. Go to the database setup wizard and look at the account ledger database after it's set up. Read the directions below and look at those things specifically on the ledger database.

1. Get familiar with the properties box. This can be found by double clicking on the form/report/query in design view or clicking on the form and then clicking the properties button from the toolbar or by right clicking where you want and clicking the properties item form the pull down.

2. Create the form with the design that you want and the space to enter the search criteria.

3. Open the properties box for the entire form (top-left corner). In the data section, item called 'Record Source' make sure this is blank. Delete everything that might be there. In the event section, item 'OnOpen' click on the down arrow on the right and put [Event Procedure] in. then click on the (...) and use the following code:

'The form is ready for entry.
Private Sub Form_Open(Cancel As Integer)
Me.Caption = Forms![FormNameHere].OpenArgs
End Sub

3. Now you can create the report. Use the wizard and once you've got it how you want it you look, then pull up the properties box for the entire report (remember its in the top left) On the record source click on the (...) thing to bring up the query builder. Build a parameter query as the record source entering [Forms]![Whatever the form name is]![whatever the field name is] as the parameter

4. In the properties under events click on 'OnOpen' and enter something like:

'As soon as the report starts to open, open the form to have the query information entered.
Private Sub Report_Open(Cancel As Integer)
DoCmd.OpenForm "Report Equipment Info", , , , , acDialog, "FormNameHere"
If Not IsLoaded("ReportNameHere") Then
Cancel = True
End If
End Sub

'This is supposed to give you the message when there is no data to report, but
'it doesn't work yet.
Private Sub Report_NoData(Cancel As Integer)
MsgBox "Sorry, there aren't any matches to this info!"
Cancel = -1
End Sub

'When the report is closed. Close the form also. Otherwise it'll still be open.
Private Sub Report_Close()
DoCmd.Close acForm, "Report Equipment Info"
End Sub

This should fill in your other events.

5. Now back to the form. Open the properties box for the individual field entries and delete the record source. This should cause 'Unbound' to appear in the place to enter.

6. Now on the form create a control button. In the properties box on the control under 'OnClick' event enter something like.

'Take the focus off the form and back to the report.
Private Sub Preview_Report_Click()
Me.Visible = False
End Sub

I hope this helps.

Kenneth
 

D-Fresh

Registered User.
Local time
Today, 09:04
Joined
Jun 6, 2000
Messages
225
Kenneth gave a good procedure but I think you're looking for something a little more simple... If I understand your question correctly, you just want to return the ID of a user with a given First and Last name... To create unbound textboxes on your form, just simply click on the textbox icon in the toolbar, icon with ab| on it. The just single click on your form. From there you can change the caption of the label to something appropriate, like "First Name", and change the name of the unbound text box in the properties sheet, something like txtFirstName. Now to refer to this box in code, just use the syntax me!txtFirstName. The following code will get you the ID...

Dim MyDB as database
Dim MyRecs as recordset
Dim MySQL as string

MySQL = "SELECT * FROM [TableName] WHERE [FirstName]='" & me!FirstName & "' AND [LastName]='" & me!LastName & "'"

Set MyDB = currentdb
Set MyRecs = MyDB.openrecordset(MySQL)

if not MyRecs.eof then
msgbox "The users ID is " & Myrecs!ID
'Do whatever else you would like with the ID
else
msgbox "That User was not found. Please try again."
end if

'Clean Up
MyDB.close
Set MyRecs = nothing
Set MyDB = nothing

I hope this is what you need.

Doug

[This message has been edited by D-Fresh (edited 06-26-2001).]
 

Users who are viewing this thread

Top Bottom