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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

clear some, but not all fields button

Status
Not open for further replies.

atsea

Technical User
Feb 27, 2005
51
JP
Currently I have an access form that allowes a user to fill in customer information. Easy enough.

Here is my problem...

Lets say there are 5 text boxes...two of the text boxes are for the customer name (first and last). (Note: The names of the customers are "hardcoded" in the table, the values should never change.)

On the form there is a button that will clear all fields.

How can I make the button function so that:
When the user clicks on the "clear all" button, all fields (text boxes) are cleared except fo the ones that contain the first and last name?
What is the best way to do this?

I have tried grouping the text boxes (still don't know if that is entirely possible), and putting the text boxes for first and last name in a subform box. I havn't been able to get either to work properly.

Any suggestions would be greatly appretiated.

Let me know if this question is in the right forum.

Thanks,

atsea
 
Easiest way is something like this
[tt]
Me.TextBox1 = ""
Me.TextBox2 = ""
Me.TextBox3 = ""
[/tt]
and the code below will clear all except autonumber field.
[tt]
Private Sub cmdClear_Click()
Dim CTL As Control
For Each CTL In Me.Controls
If TypeOf CTL Is TextBox Then
With CTL
.SetFocus
.text = ""
End With
End If
Next
End Sub
[/tt]

________________________________________
Zameer Abdulla
Visit Me
Minds are like parachutes. They only function when they are open. -Sir James Dewar (1877-1925)
 
You may play with the Tag property of the TextBox object to discriminate the controls you want to clear.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks for the quick replies...

zmr - I originally considerd your first suggestion however, I felt it would not be the most efficiant method, especially if the number of controls increase.
My current code is very similar to your second suggestion. I have posted it below.

PHV - I think this tag property you speak of may be the answer, althought I'm not quite sure how to use it. An example (or link to one) would be greatly appreciated.

Code:
Sub Clear_Form()

Dim ctrl As Control
For Each ctrl In Me.Controls
    If TypeOf ctrl Is TextBox Then
        ctrl = Null
    End If
Next ctrl

End Sub

Thanks again

atsea
 
In the Tag Property you need to put an identification code (for example 1) for all the textboxes you need to clear. then use the code below.
[tt]
Dim CTL As Control
For Each CTL In Me.Controls
If TypeOf CTL Is TextBox And CTL.Tag = 1 Then
With CTL
.SetFocus
.text = ""
End With
End If
Next
[/tt]



________________________________________
Zameer Abdulla
Visit Me
Minds are like parachutes. They only function when they are open. -Sir James Dewar (1877-1925)
 
Check out the samples here faq702-5010 (section 3 relates to this, but also section 4 involvesl usage of the .Tag property). There are also numerous previous threads here detailing usage of the .Tag property.

Roy-Vidar
 
hmmmmm...

Zmr - I was afraid you were going to say that (ctrl.Tag = 1). I tried to do that earlier but for some reason that doesn't seem to be an option for me.
For example: when I type "ctrl." the little list box with all the options pops up, there is no sign of the "Tag" property.
Is there some setting I'm missing?
Right now I have set the "tag" equal to "1" on all the fields I want to clear.


Roy - That was an excellent FAQ and I was able to find many good examples involving the .Tag (It actually gave me a couple good ideas for my next step). However, many of the examples follow a similar pattern to "ctrl.Tag". As mentioned above I'm having a little problem getting the "Tag" property to show up. Any suggestions?

I have a feeling I'm overlooking something.

Thanks,

atsea
 
Yes, you are overlooking something;-)

When declaring a variable of type control, it's a generic control, which is capable of being assigned all of the different native Access controls. Since Access controls can have different properties, not all will show through the intellisence (for instance command buttons and labels do not have any .Value property).

I think you should trust that when posting a faq, most of us will have tested it on at least one version... but most of us can be a bit relaxed when posting a snippet in response to a question.

As explained in the faq - "All controls have a .Tag property, available both in design view and at runtime. This property is not used by Access, so it is available for usage in for instance determining which controls to perform actions on in form looping." - this is, as far as I know a fact, not an opinion.

Before asking questions about "but but but I haven't seen this thingie before" - try them out, then it's a bit more interesting to answer "I tried this, but got a run time error N on the line <blah>". We, at least I, expect that!

Roy-Vidar
 
I am here now...
Unlike Royvidar, PHV and other gurus in these forums I do a test prior to answer most thread. They don't have to because they know access as they know themselves. In this answer I have tested the code then posted. It was working.

Roy had explained well about the intellisence no need more from me. I can add a little trick that I use.
If I know a property available in Access and doubt it applicable for a control. What I do is type the code in lowercase in the code window and press enter, VBA will verify it give me the result.
for example if I type
me.tag=1
VBA will correct it with
Me.Tag = 1
to its own style.
It is a beggining test. You will get more result at runtime.
You can understand the you have typed wrong/unavailable property name if it is not changed to Access' own style.
Hope this helps.



________________________________________
Zameer Abdulla
Visit Me
Minds are like parachutes. They only function when they are open. -Sir James Dewar (1877-1925)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top