Results 1 to 14 of 14

Thread: [RESOLVED] phone number

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    110

    Resolved [RESOLVED] phone number

    this codes validates phone numbers so if the user inputs the number in as 01159313481 or 0115 9313481 it accepts it and anything else it dosnt. the only problem i have is that it dosnt check if the space is in the right place, for example the user can put in 01159 313481 and it is accepted, any ideas?

    Private Sub TextBox4_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    TextBox4.Text = Trim(TextBox4.Text)

    If TextBox4.TextLength = 11 Then
    Dim position
    position = InStr(5, TextBox4.Text, " ")
    If position = 0 Then
    spacedpost = TextBox4.Text
    TextBox4.Text = Mid(spacedpost, 1, 4) & " " & Mid(spacedpost, 5, Len(spacedpost) - 4)
    Else: MsgBox TextBox4.Text & " is not a valid mobile number. Please enter a valid 12 digit mobile number."
    TextBox4.Text = ""
    End If
    Exit Sub
    ElseIf TextBox4.TextLength = 12 Then
    Dim position1
    position1 = InStr(5, TextBox4.Text, " ")
    If position1 = 0 Then
    MsgBox TextBox4.Text & " is not a valid mobile number. Please enter a valid 12 digit mobile number."
    TextBox4.Text = ""
    ElseIf position = 1 Then
    End If
    Exit Sub
    Else: MsgBox TextBox4.Text & " is not a valid mobile number. Please enter a valid 12 digit mobile number."
    TextBox4.Text = ""
    TextBox4.SetFocus
    Cancel = True
    End If

    End Sub

    any help is much appreciated
    Joe

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: phone number

    I say remove all spaces, check for valid length, then reformat in the correct space format and be done.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    110

    Re: phone number

    how would i remove all the spaces? i also want to check that it starts with a 0. how would i do this?

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: phone number

    Use the Replace function passing the string, what to look for (space) and replace with a nullstring.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    110

    Re: phone number

    and checking that it starts with a 0??

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: phone number

    If that is a validation criteria, then yes, plus any other criteria that will make the format valid.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    110

    Re: phone number

    i now have this but i get an error, object rquired, do you know what i am doing wrong

    vb Code:
    1. Private Sub TextBox4_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    2.  
    3. spacedpost = Replace(TextBox4.Text, " ", "")
    4. If spacedpost.TextLength = 11 Then
    5. TextBox4.Text = Mid(spacedpost, 1, 4) & " " & Mid(spacedpost, 5, Len(spacedpost) - 4)
    6. Else
    7. MsgBox TextBox4.Text & " is not a valid mobile number. Please enter a valid 12 digit mobile number."
    8. TextBox4.Text = ""
    9. TextBox4.SetFocus
    10. Cancel = True
    11. End If
    12.  
    13. End Sub

    what criteria would i have to use to check that the number began with a 0?/

    thanks
    Last edited by RobDog888; Mar 25th, 2007 at 03:13 PM. Reason: Added [highlight] tags

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: phone number

    You could use the Instr to look at position 1.

    Ps, I edited your post and reformatted the code with highligh=vb code tags for easier reading.

    vb Code:
    1. Private Sub TextBox4_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    2.     Dim strPhone As String
    3.  
    4.     strPhone = Replace(TextBox4.Text, " ", "")
    5.  
    6.     If Len(strPhone) = 11 And Instr(1, strPhone, "0") = 1 Then
    7.         strPhone = Mid(strPhone, 1, 4) & " " & Mid(strPhone, 5, Len(strPhone) - 4)
    8.         Text4.Text = strPhone
    9.     Else
    10.         MsgBox TextBox4.Text & " is not a valid mobile number. Please enter a valid 12 digit mobile number."
    11.         TextBox4.Text = ""
    12.         TextBox4.SetFocus
    13.         Cancel = True
    14.     End If
    15. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    110

    Re: phone number

    if i put in 01159313481 i get a runtime error 424: object rquired, any idea why?

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: phone number

    What line of code? Diid you use what I posted?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    110

    Re: phone number

    i used exactly what you posted and it gave me that error, it highlighted this line

    Text4.Text = strPhone
    in the first part of the if statement. the rest of it works though.

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: phone number

    Arrg, my VB 6 brain is taking over lol.

    Change it to TextBox4.Text and not Text4.Text.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    110

    Thumbs up Re: phone number

    yep that works. brilliant, thankyou very much for all your help. joe

  14. #14
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED] phone number

    No prob. Glad to help.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

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



Click Here to Expand Forum to Full Width