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

Using BeforeUpdate to format textbox, Excel not showing updated

Status
Not open for further replies.

vaxman9

Programmer
Feb 26, 2002
80
US
Greetings all,

I have a UserForm I am using for data input with an XLS file. I have a textbox that I am using for phone number entry that has a cell in the XLS sheet as the control source. In the BeforeUpdate method, I call a function that does a check for 7 or 10 digits and if the check clears, returns a formatted value:
Code:
Private Sub txtC1_Phone_BeforeUpdate(ByVal cancel As MSForms.ReturnBoolean)
    txtC1_Phone = Check_Phone(Me.txtC1_Phone, cancel, "Contact #1 Phone Number")
End Sub

Public Function Check_Phone(objContl As Object, ByVal Cancel As MSForms.ReturnBoolean, strText As String)
   Dim varWkr As Variant, i As Integer
    varWrk = Replace(objContl, "(", "")
    varWrk = Replace(varWrk, ")", "")
    varWrk = Replace(varWrk, "-", "")
    varWrk = Replace(varWrk, " ", "")

    Select Case Len(varWrk)
    Case Is = 0
       '
       '   Empty
       '
    Case Is = 7
       Check_Phone = Format("   " & varWrk, "###-####")    
    Case Is = 10
       Check_Phone = Format(varWrk, "(###) ###-####")
    Case Else
       Check_Phone = objContl
       Cancel = True
        MsgBox "The " & strText & _
	" you've entered does not seem complete, Please Check", _
                vbInformation + vbOKOnly, "Please Check" 
    End Select
End Function

After the BeforeUpdate, the textbox is showing the correct format, but the cell in the XLS sheet is showing the "Non-formatted" value.
For example:
I type "1234567890" in the textbox in my UserForm and hit enter. The cell in the XLS sheet shows "1234567890", but the Textbox is showing "(123) 456-7890". It seems the control source is being updated with the value of the textbox prior the the BeforeUpdate event fires. If the event results in a Cancel=True, the orginal value is restored.

Any insights would be greatly appreciated.
Thank you,
msc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top