Shorten a StringValue?

Hello,
I need to set fldDate.Text to the FIRST TWO characters of rs4.field(“Box”), which currently has the StringValue of 01-02-03

Could someone please advise me how I adapt my code below, so that the StringValue becomes only the first 2 characters (01)

fldDate.text=rs4.field("Box").StringValue

Eventually I will also need to change the StringValue to the MIDDLE TWO characters(02) and finally the LAST TWO characters (03)
All help is much appreciated as I have been trying to work this out for almost a whole day :frowning:

Thank you all in advance.

Look at

Left()
Mid()
Right()

in the docs.

Thanks Tim.
Easy when you know where to look :slight_smile:

Surely it isn’t as simple as:

[code]Dim sv As String
sv=Left(rs4.field(“Box”).StringValue, 2)

fldDate.text=sv[/code]

Another way to write it that may be clearer:

dim sv as string = rs4.Field("Box").StringValue.Left( 2 )

Thanks guys.
Just out of curiosity, was the code in my last post correct (although more long winded than Kem’s)? :slight_smile:

If it is a nullable field, remember to check for nulls before making the split.

[quote=69104:@Richard Summers]Thanks guys.
Just out of curiosity, was the code in my last post correct (although more long winded than Kem’s)? :)[/quote]

Consider James’ warning, but yes, that was correct.

The StringValue will always be in the format of 12-34-56

There will never be less characters or spaces, or be empty (if that’s what you meant by nullable?)

No, fields in a SQL database can be set to NULL if that is allowed when the field was defined. In other words, if no value at all was set for the “Box” column, then rs4.Field("Box") might return nil. In that case, rs4.Field("Box").StringValue will generate a NilObjectException.

If the field is returned at all, even if it is null, rs4.Field(“Box”) will not be nil. StringValue will actually return an empty string, “”.

But that doesn’t mean you shouldn’t test it for nil, as it could be nil for other reasons. Not very likely, but it could.

[quote=69102:@Kem Tekinay]Another way to write it that may be clearer:

dim sv as string = rs4.Field("Box").StringValue.Left( 2 ) [/quote]
And everyone missed the crucially important call to define the encoding :stuck_out_tongue:
Remember you get runs of BYTES not STRINGS until you do

dim sv as string if rs4.Field("Box") <> nil then sv = DefineEncoding(rs4.Field("Box").StringValue, Encodings.UTF8).Left( 2 )

Or:

if rs4.Field("Box") <> nil then
  sv = rs4.Field("Box").StringValue.DefineEncoding( Encodings.UTF8 ).Left( 2 )
end if

:slight_smile: