Consulting

Results 1 to 7 of 7

Thread: Add Clear button to Userform

  1. #1
    VBAX Tutor
    Joined
    Nov 2005
    Posts
    225
    Location

    Exclamation Add Clear button to Userform

    Here's one for fumei,

    You recently advised on how best to add a 'Click to Clear" button to my userform.

    I obviously misunderstood because it hasn't worked.

    The code is included below so that you can see what I have done wrong.

    Sorry to be such a nuisance. Your help is appreciated.

    greg.


    [VBA]Option Explicit
    Private Sub ClickToFinish_Click()
    Dim varCheck
    varCheck = IsNumeric(txtAmount.Text)
    If varCheck = True Then
    Call FillABookmark("Amount", txtAmount.Text)
    Else
    MsgBox "Amount does not appear to be properly numeric. " & _
    "Please re-enter."
    txtAmount.Text = ""
    txtAmount.SetFocus
    Exit Sub
    End If
    varCheck = IsNumeric(txtRate.Text)
    If varCheck <> True Then ' is NOT numeric
    If txtRate.Text = "" Then
    MsgBox "Interest rate is blank. Please input a numeric entry."
    txtRate.SetFocus
    Exit Sub
    Else
    MsgBox "Interest is not numeric. Please input a numeric entry."
    txtRate.Text = ""
    txtRate.SetFocus
    Exit Sub
    End If
    Else ' it IS numeric
    Call FillABookmark("Rate", txtRate.Text)
    Call FillABookmark("CostsOnClaim", txtCostsOnClaim.Text)
    Call FillABookmark("EntryCosts", txtEntryCosts.Text)
    Call FillABookmark("MoniesPaid", txtMoniesPaid.Text)
    End If
    Call FillABookmark("StartDate", txtStartDate.Text)
    Call FillABookmark("EndDate", txtEndDate.Text)
    Call FillABookmark("NumDays", DateDiff("d", txtStartDate, txtEndDate))
    Call FillABookmark("DailyRate", FormatCurrency((CLng(txtAmount) * CLng(txtRate) / 100) / 365))
    ActiveDocument.Fields.Update
    Unload Me
    End Sub
    Private Sub CostsOnClaim_Click()
    End Sub
    Private Sub Label3_Click()
    End Sub
    Private Sub UserForm_Click()
    End Sub

    Option Explicit
    Private Sub CommandButton1_Click()
    UserForm1.Show
    End Sub
    Private Sub CommandButton2_Click()
    Unload Me
    End Sub
    Next
    ActiveDocument.Fields.Update
    Unload Me
    End Sub
    Private Sub UserForm_Initialize()
    Dim oBM As Word.Bookmarks
    Set oBM = ActiveDocument.Bookmarks
    If oBM("Amount").Range.Text <> "" Then
    Me.txtAmount.Text = oBM("Amount").Range.Text
    End If
    If oBM("Rate").Range.Text <> "" Then
    Me.txtRate.Text = oBM("Rate").Range.Text
    End If
    If oBM("StartDate").Range.Text <> "" Then
    Me.txtStartDate.Text = oBM("StartDate").Range.Text
    End If
    If oBM("EndDate").Range.Text <> "" Then
    Me.txtEndDate.Text = oBM("EndDate").Range.Text
    End If
    If oBM("CostsOnClaim").Range.Text <> "" Then
    Me.txtCostsOnClaim.Text = oBM("CostsOnClaim").Range.Text
    End If
    If oBM("EntryCosts").Range.Text <> "" Then
    Me.txtEntryCosts.Text = oBM("EntryCosts").Range.Text
    End If
    If oBM("MoniesPaid").Range.Text <> "" Then
    Me.txtMoniesPaid.Text = oBM("MoniesPaid").Range.Text
    End If
    Set oBM = Nothing

    End Sub

    Sub Clear_Click()
    Dim ctl As Control
    For Each ctl In Me.Controls
    If TypeOf ctl Is TextBox Then
    Me.Controls(ctl.Name).Text = ""
    ElseIf TypeOf ctl Is CheckBox Then
    Me.Controls(ctl.Name).Value = False
    ElseIf TypeOf ctl Is ComboBox Then
    Me.Controls(ctl.Name).ListIndex = 0
    End If
    Next
    End Sub
    [/VBA]

  2. #2
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Greg

    How hasn't it worked?

  3. #3
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Which is something, AGAIN Greg, that I have advised you to NOT do in your posts. "hasn't worked" means nothing.

    NOTHING Greg. Please stop doing that. You have good questions, and I can see that you are working hard at this. But we are not mind readers. Please have some respect for that.

    "hasn't worked". If you got just that....would YOU know what it meant? Hmmmmm? I don't think so.

    So it makes it literally impossible for me to respond. However, I think it is probably the checkboxes. But....who knows?

  4. #4
    VBAX Tutor
    Joined
    Nov 2005
    Posts
    225
    Location
    You're right, it doesn't help so let me try again.

    I press my "Data Form" menu button to bring up my form. Then, after entering my data, I click "Finish" to produce the desired result on my Word document.

    Likewise, if I press the "More Data" button, I am able to bring up a second form to enter additional data, although, as this is a work in progress, there is nothing in that other form yet.

    However, when I press the "Clear" button there is absolutely no response. That is, the button gets depressed but the data in the form remains intact and unaltered.

    I tried culling some of the redundant code (at least I thought it was) but I still didn't get a response, so I put the code back in.

    Now, I am too weary to give the thing my full concentration.

    As I am trying to master this stuff I don't want you to do it for me but you might offer a suggestion or two and I'll then try to solve the problem by a process of elimination.

    If, as I suspect, you still require more information, I'll do what I can.

    By way of general comment, I have found the MS site to be helpful but I feel that it would be much better if it offered more real world scenarios that people like me could draw from.

    For example, I would have thougt that this "Clear Form" function would be common and yet I can find no mention of it anywhere. Perhaps I was looking in the wrong place.

    Greg.

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Take a look at the attached. It is using the same code. If it works for you...and it should...then you changed something in your code.

    I hope we are not having a serious miscommunication.
    That is, the button gets depressed but the data in the form remains intact and unaltered.
    If you mean the data in the document...no, it does not affect the document whatsoever. It affects the userform, and only the userform.

    If what you are asking is something to clear out the stuff in the bookmarks, in the document, then that is something else.

  6. #6
    I have used this in my forms in the past. Hope it helps.

    ThisDocument.Unprotect 'Unprotect the document.
    Selection.WholeStory 'Selects all forms in the document.
    Selection.Fields.Update 'Updates all selected fields (returns values to "")
    ActiveDocument.Protect Type:=wdAllowOnlyFormFields 'Reprotects the document

  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Depending on version, you can also use:[vba]ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReSet:=False[/vba]However, I am not sure what the issue is still.

    Clearing the userform, or clearing the document form.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •