Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CLEARING FORMS

Status
Not open for further replies.

Mksmall

Technical User
Oct 18, 2001
18
US
What is the easiest way to clear a form? I have a form with a search box. The search button runs a macro to pull in the information that fills out the form. If you do another search, I would like the form to clear before the search is made. I've tried the "setvalue" function of the macro to set all boxes to null before I run the search part of the macro, but I get " the primary key cannot be null" message. Any ideas??
 
Can you do something like :

Private Sub Form_Activate()

txtSearch = Null

End Sub
 
Not being proficient at Access yet, could that be included in this event procedure:

Private Sub SEARCH_CONTACT_Click()
On Error GoTo Err_SEARCH_CONTACT_Click

Dim stDocName As String

stDocName = "SEARCH"
DoCmd.RunMacro stDocName

Exit_SEARCH_CONTACT_Click:
Exit Sub

Err_SEARCH_CONTACT_Click:
MsgBox Err.Description
Resume Exit_SEARCH_CONTACT_Click

End Sub

or do I need to create an event procedure and do away with the macro.

thanks
 
Hi!

The message you are getting is telling you that you are trying to set all of the fields in the current record to null. Access doesn't mind doing that except for the PK field which is why you are getting the message about the primary key. Of course, you don't really want to null out the whole record anyway. You may be able to go to a new record prior to the search, which will blank out all of the fields. One last thing, your text box which you are using to search from, if it is bound to a field, you will want to make sure that you aren't changing any data in your table when you type in a new value to search for.

hth
Jeff Bridgham
bridgham@purdue.edu
 
As Jeff says you are in danger of overwriting existing data. The problem can be solved by placing an unbound textbox away from the main data (I used the form header).
Name this textbox Search or something similar then put this code in the "AfterUpdate" event. From reading your posting I think you are searching on a single field.

DoCmd.GoToControl "SearchOnField"
DoCmd.FindRecord [Search]
DoCmd.GoToControl "Search"
[Search] = ""


If you are not then you will have to modify the code a bit. Add a dropdown list of fields you search and modify the first line of the code above to pick up the field to be searched from the dropdown.
Sandy
 
Yes, you are correct. I am searching on a single field to populate the form with the table data. I have moved the search box to the form header, but how do I get the tab stop to begin in the search box in the form header and not in the form detail when the form is opened.
 
You could have a piece of code in the form's on open event to activate the first record.

Something like

Me![FieldName].SetFocus
Sandy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top