Results 1 to 12 of 12
  1. #1
    turbo910 is offline Novice
    Windows Vista Access 2007
    Join Date
    Nov 2009
    Posts
    21

    Error when clearing form

    Hi guys



    Using access 2007

    Basically i have created a form (Majority unbound) and wanted the user to be able to save the form (done) and then clear the form in order to input the new data.



    I already have a clear button that clears a majority of the controls on the form using this VBA code



    Private Sub Clear_form_Click()
    Dim ctl As Control

    On Error Resume Next

    For Each ctl In Me.Controls
    ctl.Value = ctl.DefaultValue
    Next

    Set ctl = Nothing


    End Sub



    This is all well except one point :



    The last text box doesn't clear, instead it says #Error. The Text box is simple i just created it from the MS ribbon and entered the name of it and a simple calculation which is : =[Quantity]-[Quantity_out].



    this obviously means that not all the controls are clear and new so How exactly do i over come this??



    Anything else i can do please ask



    Thankyou!!

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows XP Access 2002
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    How about:
    Code:
    For Each ctl In Me.Controls
    If ctl.Name <> "Name of the Calculated control" Then
       ctl.Value = ctl.DefaultValue
    End If
    Next

  3. #3
    turbo910 is offline Novice
    Windows Vista Access 2007
    Join Date
    Nov 2009
    Posts
    21
    Input this into my current code and if so where? or use this as the only code and input all my control names??

    Thanks for fast reply

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows XP Access 2002
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Just change your current code so it incorporates what I posted; using *your* [Name of the Calculated control] of course. It is just the two lines. One that begins with IF and the other begins with END IF

  5. #5
    turbo910 is offline Novice
    Windows Vista Access 2007
    Join Date
    Nov 2009
    Posts
    21
    I input the code below the set ctl = nothing

    and i get this error when i click the clear button on my form but without the code you told me to put in it works fine but i still have the error in that particular text box
    the error is:

    Compile error: User-defined type not defined


  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows XP Access 2002
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Post all of the code starting with:
    Private
    .
    . to
    .
    End Sub

  7. #7
    turbo910 is offline Novice
    Windows Vista Access 2007
    Join Date
    Nov 2009
    Posts
    21
    Private Sub Clear_form_Click()
    Dim ctl As Control

    On Error Resume Next

    For Each ctl In Me.Controls
    ctl.Value = ctl.DefaultValue
    Next
    Set ctl = Nothing

    If ctl.Name <> New Quantity Then
    ctl.Value = ctl.DefaultValue
    End If
    End Sub

    When the error occurs and i debug it it highlights all of the first line: Private Sub Clear_form_Click()

  8. #8
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows XP Access 2002
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Try this code *INSTEAD* of what you have:
    Code:
    Private Sub Clear_form_Click()
       Dim ctl As Control
       For Each ctl In Me.Controls
          If ctl.Name <> "New Quantity" Then
             ctl.Value = ctl.DefaultValue
          End If
       Next
    End Sub

  9. #9
    turbo910 is offline Novice
    Windows Vista Access 2007
    Join Date
    Nov 2009
    Posts
    21
    Nope didnt work my friend :S

  10. #10
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows XP Access 2002
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Just reread the first post. Are the [Quantity] and [Quantity_out] controls both unbound? What is their DefaultValue? How about putting 0 in them with the Clear routine?

  11. #11
    turbo910 is offline Novice
    Windows Vista Access 2007
    Join Date
    Nov 2009
    Posts
    21
    Excellent all sorted thankyou

  12. #12
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows XP Access 2002
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    That's great. Do you want to follow the link in my sig and mark this thread as Solved?

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 11-22-2009, 07:46 AM
  2. Error displaying image on form
    By mamba716 in forum Forms
    Replies: 6
    Last Post: 06-17-2009, 09:13 AM
  3. clearing a form
    By macsterling in forum Forms
    Replies: 0
    Last Post: 07-31-2008, 10:38 AM
  4. Clearing the clipboard with VBA?
    By Simon Sweet in forum Programming
    Replies: 1
    Last Post: 04-17-2008, 03:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums