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

Quick question..

Status
Not open for further replies.

griswom

Technical User
May 13, 2002
40
US
Is there a quick and easy VB method to reset all of the controls on an unbound form to their default values? I sure hope so... Thanks in advance.

Griz
 
Note: Run this in debug mode to trap the specific error message for controls which do not have a default property like labels, etc. Call the subroutine like so, passing it the Me.Form as a parameter so that it can spin through the collection of controls on the form and reset them to their default value if they have one. This is off the top of my head at work, so there may be some slight syntactical errors but this should do the trick for you.

ResetToDefaults Me.Form <== Call the sub this way in code

Good Luck!

Sub ResetToDefaults(ByRef rfrmMe As Form)
Dim ctl As Control

On Error GoTo Err_ResetToDefaults
For Each ctl In rfrmMe.Controls
Debug.Print ctl.Value, ctl.Default
ctl = ctl.Default
Debug.Print ctl.Value, ctl.Default
Next

Exit_ResetToDefaults:
Exit Sub

Err_ResetToDefaults:
Select Case Err.
Case whatever
REM Do nothing
Resume Next
Case Else
MsgBox Err.Description & &quot; &quot; & Err.Number
Resume Exit_ResetToDefaults
End Select
End Sub
 
A Buckeye in Irish country...must be scary. I'm sitting in sunny Columbus, OH myself right now. Thanks for the code. I will try and implement this, and let you know if I can get it to work...a dicey prospect considering my abilities....

Griz
 
If you post an email, I will be happy to send you working code from home. My email address is in my profile. If you don't have a programming background, give some more detail about what you are trying to do as the above code could be called from several places depending upon your answer.

Basically, everything in Access is a collection of something, and a form has a collection of controls associated with it. The above code just spins through all of the controls on the form resetting the value for you.

The for each is just a special case of a for i = 1 to x next loop specifically for containers. Kind of like, for each empty cupcake wrapper, fill with batter for baking.

Yes, it gets lonely out here. But, when someone gives me too hard of a time, I just ask them for the score to the 95 game, or if that is too painful, the 96 game. My desktop wallpaper is Eddie George running away from a whole posse of Domers :).

Have a great day!
 
Mr. Buckeye,

First off, thanks for your help. I scaled down your code a little bit so that I could understand it, but here's what I'm using. It works wonderfully.

Sub ResetToDefault(rfrm As Form)

Dim ctl As Control

On Error GoTo Err_ResetToDefaults

For Each ctl In rfrm.Controls
ctl = ctl.DefaultValue
Next

Exit_ResetToDefaults:
Exit Sub

Err_ResetToDefaults:
Resume Next


End Sub

You have saved me a lot of time and effort!

Griz

(P.S. GO BUCKS!)

&quot;The essence of strategy is knowing one thing, to know ten thousand things.&quot;

-Miyamamoto Musashi &quot;A Book of Five Rings&quot;
 
Nicely done, Grizwom.

A slight style point, indent the ctl=ctl.default piece as you have the others.

Because you pass the form object into the procedure as a parameter, you can drop this code anywhere you want to on other forms and it will work without changes.

Good Luck!

PS. Keep me in mind for excess Buckeye football tickets.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top