Split in VBA does not work

2

I have the following code snippet:

CorpID = rsSQL.Fields("Name")
Tmp14 = Split(CorpID, , 1)
Tmp15 = Join(Tmp14)
Me.nomeUsuario.Caption = Tmp15

    
asked by anonymous 15.10.2014 / 16:27

2 answers

4

The problem is the third parameter.

By official method documentation , the third parameter is the maximum amount of substrings you want. If you are not informed, or if you enter the default value (-1), there is no substrings limit to return. But in your case ...

If you say that you want at most one substring, it does not matter how many times the delimiter appears in the input. You will get only one string.

If you want to separate by first and last name, pass at least 2 in the third parameter. This transforms:

"Derpino Augusto de Oliveira Lima"

In:

"Derpino",  "Augusto de Oliveira Lima"

You can omit the third parameter to break the string in all spaces. And the default value for the second parameter, which is the delimiter, is space ( " " ), so you can omit it as well. It looks like this:

Tmp14 = Split(CorpID)

Good luck!

    
15.10.2014 / 16:48
2

There are two problems. Considering the value "José Roberto" in rsSQL.Fields("Name") :

  • 1) In Split(CorpID, , 1) , you limit the split to 1, so it ends up returning the full name, "Jose Roberto".

  • 2) Even if you correct to Tmp14 = Split(CorpID) to return Tmp14(0) = "José" and Tmp14(1) = "Roberto" , on the next line you rejoin them: Tmp15 = Join(Tmp14) . That is, Tmp15 again contains the full name "Jose Roberto".

The code corrected below will only show "Joseph" in Me.nomeUsuario.Caption :

CorpID = rsSQL.Fields("Name")
Tmp14 = Split(CorpID)
'Tmp15 = Join(Tmp14)
Me.nomeUsuario.Caption = Tmp14(0)

I fixed the split ignored the join settling in the caption the first item of the result of the split; in this case, "Joseph."

    
15.10.2014 / 17:48