[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 171
  • Last Modified:

Split subscript out of range

I'm trying to load information into 3 textboxes. My codes doesn't seem to work in all instances. The strings may be:

Test / Test2 / Test3
or
Test / Test2
or
Test
or nothing at all.  My code doesn't seem to work:

        txtEditGenre(0).Text = Split(CNull(rstUsers.Fields("Genre").Value), "/")(0)
        If Len(txtEditGenre(0).Text) Then txtEditGenre(0).Text = Trim(txtEditGenre(0).Text)
        txtEditGenre(1).Text = Split(CNull(rstUsers.Fields("Genre").Value), "/")(1)
        If Len(txtEditGenre(1).Text) Then txtEditGenre(1).Text = Trim(txtEditGenre(1).Text)
        txtEditGenre(2).Text = CNull(rstUsers.Fields("Genre").Value)

Function CNull(txt As Variant) As String
If IsNull(txt) Or txt = "" Then
  CNull = ""
Else
  CNull = txt
End If
End Function
0
Takamine334
Asked:
Takamine334
  • 4
  • 4
1 Solution
 
DhaestCommented:
myString = Split(CNull(rstUsers.Fields("Genre").Value), "/")
if ubound(myString) > 0 then txtEditGenre(0).text = myString(0) else txtEditGenre(0) = "" end if
if ubound(myString) > 1 then txtEditGenre(1).text = myString(1) else txtEditGenre(1) = "" end if
if ubound(myString) > 2 then txtEditGenre(2).text = myString(2) else txtEditGenre(2) = "" end if
0
 
Takamine334Author Commented:
That didn't seem to work. It all set txteditgenre to ""
0
 
Takamine334Author Commented:
?UBound(mystring) > 0
False
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
DhaestCommented:
Dim words() As String
words() = Split("/")

For i = 0 To UBound(words)
    txtEditGenre(i).text = words(i)
Next
0
 
Takamine334Author Commented:
Thank you!

Wonder why the other didn't work
0
 
Takamine334Author Commented:
On the same lines, if I'm trying to put that in a database and some of the textboxes are blank, how could I do that?

Genre='" & IIf(Len(Form1.CheckField(txtEditGenre(0).Text)), txtEditGenre(0).Text, Null)
0
 
DhaestCommented:
With your code, you would have an error, because your subscript was out of range. If you split something in parts, you can only acces the number of fields it was splitted into. For example test1/test2 will give an error when you try to reach the third field

>> On the same lines, if I'm trying to put that in a database and some of the textboxes are blank, how could I do that?
What exactly do you mean ? Do you want to insert a null-value in the database or an emtpy ("") string ?
0
 
DhaestCommented:
Recommendations: answered partial by dhaest
On the second part of the question was no feedback from the asker
0
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now