Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

How to use Instr to search for the first tab

I have a simple question. I need to parse a string into pieces. it is tab delimited and i want to segregate everything that is between each tab. I tried using Instr but I don't know how to make it look for the first tab. Please help.
[238 byte] By [tjm1976] at [2007-11-11 7:50:48]
# 1 Re: How to use Instr to search for the first tab
Seems to me that the best way to parse a tab delimited string would be to use the Split() function. This function will take each piece of the string that is delimited by a tab and place it in an array for you. You can then access each element of the array as you like.

Example:

Dim my_string As String

my_string = "dog" & vbTab & "cat" & vbTab & "frog"

Split(my_string, vbTab)(0) ....will = dog
Split(my_string, vbTab)(1) ....will = cat
Split(my_string, vbTab)(2) ....will = frog
doofusboy at 2007-11-11 17:26:21 >
# 2 Re: How to use Instr to search for the first tab
doofusboy posted the preferred method for splitting a string.

But if you want to find the first instance, you can use something like this:

iPos = INSTR(my_string, vbtab, 1)

Now iPos contains the position of the first tab. (or -1 of there are no tabs.)

To find the next tab, replace the value of 1 (the starting position) with iPos + 1

iPos = INSTR(my_string, vbtab, iPos + 1)
edburdo at 2007-11-11 17:27:22 >
# 3 Re: How to use Instr to search for the first tab
Oh, and you can also use the Split command like this (saves calls to split, which are processor expensive).

Dim sR() as String
Dim my_string As String

my_string = "dog" & vbTab & "cat" & vbTab & "frog"

sR = split(my_string, vbtab)

'sR(0) now contains "dog"
'sR(1) now contains "cat"
'sR(2) now contains "frog"
edburdo at 2007-11-11 17:28:19 >
# 4 Re: How to use Instr to search for the first tab
Thanks the Split method is what i am looking for. I have tab delimited files that i want to dynamically generate tablenames inside an Access database and am needing to pull in the field names to get the table structure down. Also i am pulling in the first row of data to determine data types. Does anyone know how to dynamically check a strings value to determine it's data type?
tjm1976 at 2007-11-11 17:29:27 >
# 5 Re: How to use Instr to search for the first tab
Knowing the value in a database field does not tell you what it's data type is.

For example, a date can be stored as a date or a string.

What you need is a way to examine the table definitions of your database. At the moment, I can't recall the syntax for doing this with an Access database. Perhaps someone else can provide for you.
doofusboy at 2007-11-11 17:30:25 >
# 6 Re: How to use Instr to search for the first tab
What i am doing is building new tables off of these text files dynamically and by the values in the text file for each record i want my code to assume a data type.
tjm1976 at 2007-11-11 17:31:24 >
# 7 Re: How to use Instr to search for the first tab
You'll never be able to automate EVERYTHING.

Assuming a data type based on the contents of fields in a text file sounds like a risky proposition at best. Just developing the rules/logic behind discerning whether a value should be stored as a date, numeric [integer, double, long], string/text or any of the other data types would seem to me to be quite a daunting task. And then you need to code all that.

If I were you, I'd review the text files you are pulling from to determine patterns or sets of data, if any are readily apparent. Then you can define your tables and validate individual pieces of data to make sure they are the proper data type before inserting them into your tables.

Conversely, you could store everything as strings and then convert them to other data types in your code if you needed to do something with them that can't be done with strings. For example, store all numbers as strings, but then convert them to integers via code after retrieving them from your database if you need to sum the values for a particular field.
doofusboy at 2007-11-11 17:32:25 >
# 8 Re: How to use Instr to search for the first tab
Thanks for your help. I appreciate it.
tjm1976 at 2007-11-11 17:33:28 >