Solved Delete unwanted characters in the TextBox Value (1 Viewer)

Hisoka

New member
Local time
Today, 15:19
Joined
Sep 20, 2023
Messages
17
Hello,
I have this code to open a conversation with customers on WhatsApp through the WhatsApp application,
Code:
CreateObject("Shell.Application").Open "https://wa.me/" & Me.tbMobNum & "/?text=" & Me.tbWaMessage

As you can see, I have the customer's number in the TextBox, But the number format in the control, like (+968)1111 1111,
But it must be like +96811111111 to work with the code above,
This means that I just want to delete the parentheses and the space in the middle

Maybe VBA Regex will do the trick, But I don't know how to use it, or is there a better way to implement this thing?

Thanks in advance
 
Solution
Following should serve:
Replace(Replace(Replace(Me.tbMobNum, " ", ""), "(", ""), ")", "")

I expect Regex could do it and if this gets more complicated, might want to or use VBA custom function.

June7

AWF VIP
Local time
Today, 06:19
Joined
Mar 9, 2014
Messages
5,472
Following should serve:
Replace(Replace(Replace(Me.tbMobNum, " ", ""), "(", ""), ")", "")

I expect Regex could do it and if this gets more complicated, might want to or use VBA custom function.
 
Solution

Edgar_

Active member
Local time
Today, 09:19
Joined
Jul 8, 2023
Messages
430
Here's another:
Code:
Function StringToPlusNumber(yourText as String) as string
    Dim res As String
    Dim i As Long
    For i = 1 To Len(yourText)
        If IsNumeric(Mid(yourText, i, 1)) Or Mid(yourText, i, 1) = "+" Then
            res = res & Mid(yourText, i, 1)
        End If
    Next i
    StringToPlusNumber= res
End Function

It's ugly as hell, but it works. OK, I'll add explanation to this ugly thing:
1. Define function, takes string as parameter
2. Loops character by character by checking how many characters it has starting from 1
3. If current character is numeric or a plus sign, it adds it to the result
4. That is all
 
Last edited:

Pat Hartman

Super Moderator
Staff member
Local time
Today, 10:19
Joined
Feb 19, 2002
Messages
43,275
Since you are looking at a value from a record rather than a newly entered value, change the name of the control. So, the controlName property is txtPhone and the name of the bound field is Phone. Now, you can refer to the control which is probably where the format is coming from OR you can refer to the actual saved table value which should NEVER have a format associated with it. No Code.
 

June7

AWF VIP
Local time
Today, 06:19
Joined
Mar 9, 2014
Messages
5,472
Doesn't matter if format is generated by Format property of textbox or field in table. It doesn't change saved value. So, my presumption is those unwanted characters are actually in data.
 
Last edited:

Pat Hartman

Super Moderator
Staff member
Local time
Today, 10:19
Joined
Feb 19, 2002
Messages
43,275
So, my presumption is those unwanted characters are actually in data.
Probably. That means that the best solution is to run an update query to get rid of the characters at the source. Then use input masks on the forms and set them to not save the formatting characters. That means all the suggested code is just bandaids to hide a data problem. Fixing the data problem is always the best solution.

PS, it is really poor practice to use formatting of any kind on your tables because you will never know what the fields actually contain when the formatting masks the actual values.
 

Users who are viewing this thread

Top Bottom