User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Visual Basic 4 / 5 / 6 section within the Software Development category of DaniWeb, a massive community of 425,874 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,362 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums
Sep 20th, 2004
Views: 15,754
Clearing Controls & Forms in Access (VBA)
visualbasic Syntax | 5 stars
  1. ---- start of revised code ----
  2. Private Sub cmdClearCriteria_Click()
  3.  
  4. Dim ctl As Control
  5.  
  6. For Each ctl In Me.Controls
  7. Select Case ctl.ControlType
  8. Case acTextBox, acComboBox, acListBox, acCheckBox
  9. If ctl.ControlSource = "" Then
  10. ctl.Value = Null
  11. End If
  12. Case Else
  13. End Select
  14. Next ctl
  15.  
  16. End Sub
  17.  
  18. '---- end of revised code ----
  19.  
  20. 'OR
  21.  
  22. '*********** CLEAR FORM FIELDS CODE ***********************
  23. Sub Clear_Form()
  24. Dim X As Control
  25. For Each X In Me.Controls
  26. If TypeOf X Is TextBox Then
  27. X = Null
  28. End If
  29. Next X
  30.  
  31. End Sub
  32.  
  33. '************************************************************
  34.  
  35. OR
  36.  
  37. '***********************************************
  38. Sub ClearFormText(frm As Form)
  39. Dim ctl As Control
  40. For Each ctl In frm.Controls
  41. If ctl.ControlType = acTextBox Then
  42. ctl.Value = ""
  43. End If
  44. Next ctl
  45. End Sub
  46.  
  47. Call the code from any event with the following syntax:
  48.  
  49. ClearFormText Me
  50.  
  51. The 'Me' keyword will pass in a reference to the current form and the code will clear all text boxes.
  52.  
  53. '********************************************
Comments (Newest First)
colinnwn | Newbie Poster | Nov 12th, 2007
When I try any of the above, I get runtime error 2448 "you can't assign a value to this object".

The object is
ctl.Value = Null or X = Null or ctl.value=""

Does anyone have a thought on why it doesn't work for me? Or are there any other ways to clear all fields on a form?

Thanks.
samershalha | Newbie Poster | Feb 13th, 2006
thank for the beautifull code
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 6:12 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC