1

i need to fix this line it says object required: If FullName.Contains(",") Then

This is my code:

Private Sub retrieveinput_Click()
     FullName = inputText.Text
     Dim NameArray() As String

     If FullName.Contains(",") Then
         NameArray = Split(FullName, ",")
         First = NameArray(1)
         Last = NameArray(0)
     Else
         NameArray = Split(FullName)
         First = NameArray(0)
         Last = NameArray(1)
     End If

     TextBox2.Text = First
     TextBox3.Text = Last

End Sub
Reisclef
  • 2,056
  • 1
  • 22
  • 25

2 Answers2

2

You can't do this:

Dim foo As String
foo = "foobar"

If foo.Contains("bar") Then
...

This isn't VB.NET, strings aren't objects in VBA, so there's no Contains method to call. The VBA way of doing this is to use the InStr function, like this:

If InStr(FullName, ",") > 0 Then

See this post: Check if a string contains another string

You could also make yourself a helper function like this:

Public Function Contains(ByVal string_source As String, ByVal find_text As String, Optional ByVal caseSensitive As Boolean = False) As Boolean

    Dim compareMethod As VbCompareMethod

    If caseSensitive Then
        compareMethod = vbBinaryCompare
    Else
        compareMethod = vbTextCompare
    End If

    Contains = (InStr(1, string_source, find_text, compareMethod) <> 0)

End Function

And then you could do

If Contains(FullName, ",") Then
Community
  • 1
  • 1
Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
-1

An error such as 'Object required' usually means that you have missed an assignment somewhere. In this case if the error is on the line

If FullName.Contains(",") Then

then FullName is null or hasn't been defined. This may be because of the following:

  1. inputText is not a variable or control
  2. Text is not a property or method of inputText
  3. inputText.Text value is null

First thing to check in these circumstances is if the variable you are trying top access has a value (i.e. not null)

Steve Padmore
  • 1,710
  • 1
  • 12
  • 18
  • it worked for the else statement before i made my if statement. I put my name into the text, hit my button and it split the name into two other text boxes but when using the comma it does not work –  Oct 17 '15 at 19:49
  • Place a breakpoint on the line with the error - then check the value of inputText.Text. As mentioned before, ensure that Dim FullName is before use in your method ( or in scope outside). – Steve Padmore Oct 17 '15 at 19:53
  • i literally know nothing about this, it was thrown at me and am not a CS major –  Oct 17 '15 at 19:57
  • Click in the margin on the left of the line (a red circle should appear) or select the line and press F9, or right-click the line and select breakpoint - there are many ways to do this depending on the environment you are using. Then run it in debug mode and the program will pause execution at the breakpoint. – Steve Padmore Oct 17 '15 at 19:59
  • got that besides run in debug mode? i ran it normally while it was red and nothing different happened –  Oct 17 '15 at 20:02
  • What's the environment? Excel? Word? Web? – Steve Padmore Oct 17 '15 at 20:03
  • also just did something and said breakpoint not allowed on this line –  Oct 17 '15 at 20:04
  • excel vba using macros –  Oct 17 '15 at 20:04
  • If the code is EXACTLY as you have above, then change it to be: Dim FullName As String FullName = inputText.Text (two lines) – Steve Padmore Oct 17 '15 at 20:07
  • it is exactly as above –  Oct 17 '15 at 20:09
  • thanks for all the help i got it with this: InStr(FullName,",") –  Oct 17 '15 at 20:20