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!

Clear all controls on form at once 2

Status
Not open for further replies.

jw5107

Technical User
Jan 20, 2004
294
US
I have a form that has nothing but unbound text boxes on it.
Data is entered via a textbox that performs a dlookup function on an afterupdate event.
I have been looking at all the examples showing how to clear the txtboxes at once, but I cannot get any of them to work. I keep getting error messages... I do not want to create a line of code for each textbox to clear (like the example below). I have alot of controls on the form.
Is there a way to loop thru all the controls on the form and clear (or make the value null) all at once?? Below is an example of what I am working with. This works fine but like I said, I have alot of unbound txtboxes on the form. This code also changes the color of the labels forecolor, backcolor and bordercolor. Any other example coding would be most appreciated!!

Thanks, jw5107

For Each ctrl In Forms (strWWReviewHelper).Controls
If TypeOf ctrl Is TextBox Then
Forms![WWReviewHelper]![USAGE] = Null
Forms![WWReviewHelper]![PRIME] = Null
Forms![WWReviewHelper]![Text508] = Null
End If
If TypeOf ctrl Is Label Then
ctrl.ForeColor = vbBlack
End If
If TypeOf ctrl Is Label Then
ctrl.BackColor = 16777215
End If
If TypeOf ctrl Is Label Then
ctrl.BorderColor = 16777215
End If
Next ctrl
 
Try this:

Code:
Dim ctrl As Control
For Each ctrl In Form.Controls
    If (ctrl.ControlType = acTextBox) Then
       ctrl = vbNullString
    End If
Next ctrl

HTH
Mike

[noevil]
 
How about this.....

Dim ctl As Control
For Each ctl in Me
Select Case ctl.ControlType
Case acTextBox:
ctl.Value = ""
Case acLabel:
ctl.ForeColor = vbBlack
ctl.BackColor = 16777215
ctl.BorderColor = 16777215
End Select
Next ctl

 
I tried both of the examples below and I keep getting the same error -

"you can't assign a value to this object" and on debug it highligths - ctl.Value = "" or ctrl.Value = vbNullString

What am I missing??

Oh yeah - thanks for the ultimate fast response!!!!

jw5107
 
It sounds like you have at least one text box that is NOT unbound. Could this be true?
 
jw5107

I'm having a similar problem with a bunch of option buttons on a form. I have a button "Clear" that I want to cause all the option buttons to be unselected and gray out when the button is clicked. I get the same error - "can't assign a value to this object."

I have done this before with a few textboxes and not had any problem if each textbox is specifically named in the clear procedure, like:

Me.tbxName = Null

Of course, you shouldn't have to name them all individually, especially if you have a lot of them. But have you tried doing that, say, with one at a time to be sure that it works before you perform this using a loop? I mean, sometimes it is a data type mismatch problem or some other property setting that's preventing the code from compiling (ie - ever notice that sometimes the error message you get doesn't in the least correspond to what the problem was after you've pinned it down and resolved it?).

Okay, having said that, if I solve it, I'll post to this thread and share my solution.

In the meantime, best of luck -

Vie
 
Randy700,

You were right!! I had 2 bound textboxes. I re-structured the form and whalla!!! Right on the money. Thank you very much for the hookup!!!

THANKS!!!

JW5107
 
Just in case anyone else reads this thread and is interested in the solution to the option buttons problem - you need to reference the acOptionGroup (not acOptionButton) and, like mgolla suggested:

Code:
Dim ctrl As Control
For Each ctrl In Form.Controls
    If (ctrl.ControlType = acOptionGroup) Then
       ctrl = Null
    End If
Next ctrl

I'm guessing because the option buttons in an OptionGroup are all mutually exclusive, referencing the whole frame is enough to clear any selected butttons.

Glad you got it worked out, jw5107.
 
Vie, I Change your code to clear a listbox. Do you know how to change the code to clear a simple listbox.

Irene
 
Randy700, Thank you but I asked for the wrong thing. I don't want the listbox to blank out. I just what the ones that where selected. To be deselected by a command button. Irene
 
Use the RemoveItem method....

Me.Listboxname.RemoveItem Me.Listboxname- 1


Randy
 
This gaves me a error that row source type should be a value list. And my listboxe is a table/query

Irene
 
ok. In that case, you'll need to remove the item from the table or make changes that cause it to not appear in the query. Then do a me.listboxname.requery.

Randy
 
Randy, I put me.lstSupChoice.requery on the click event on my command button. And I change the listbox row source to a query name tblOption query. And when I click the button nothing happens.

Let me try to explain again what I'm trying to do. The form I made is to make stock cards. The top have is for the stock number, vin, color ect. Then the listboxes is a muli-select of all the option the card has. I have a button that clear the top. because must car we get have the same option. Know I need a button that reset the option but leave them on the screen so that different option can be selected for the next card that needs to be made up.

Irene
 
Vie,

Try these:

To "unselect" the selected items in a listbox:
YourListBoxName = ""
No item will be selected after.
You can programmmatically make selections in a listbox
YourListBoxName = Some value

To set an option group to no selection:
YourOptionGroupName = ""
All the option buttons will be up afterwards.
You can programmmatically make selections in an option group:
YourOptionGroupName = Some value
Afterwards that button will be "down".

Best,

C
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top