473,385 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Subscript Out of Range

Hi again -
I have a text file that is received on a daily basis and I use VBA to break down each field and add to an existing table. There are 50 fields in the text file (separated by semicolon) and I've had no problem whatsoever in it working. However, there have been 3 new fields (time - ie., 13:47:51) added to the text file and I need to modify my existing code (all fields not shown since it would take up too much space):

Expand|Select|Wrap|Line Numbers
  1. strFileName = "C:\Documents and Settings\user\Desktop\sometextfile.txt"
  2.  
  3. Set fso = CreateObject("Scripting.FileSystemObject")
  4. If (fso.FileExists(strFileName)) Then
  5. Else
  6. MsgBox "File Not Found"
  7. Exit Sub
  8.  
  9. Set rst = New ADODB.Recordset
  10. rst.ActiveConnection = CurrentProject.Connection
  11. rst.Open _
  12. Source:="Select * from [test]", _
  13. CursorType:=adOpenKeyset, _
  14. LockType:=adLockOptimistic, _
  15. options:=adCmdText
  16.  
  17. Open strFileName For Input As #1
  18.  
  19. Do Until EOF(1)
  20.     Line Input #1, strLineInput
  21. strfield(1) = Mid(strLineInput, 1, 12)
  22. strfield(2) = Mid(strLineInput, 14, 3) - - and everything in between
  23. strfield(50) = Mid(strLineInput, 720, 8)  <----last field
  24. rst.AddNew
  25. rst![F1] = Trim(strfield(1))
  26. rst![F2] = Trim(strfield(2))
  27. rst![F50] = Trim(strfield(50))
  28. rst![DateStamp] = Date
  29. rst.Update
  30. Loop
  31. Close #1
  32. rst.Close
  33. Set rst = Nothing
It's as simple as adding three (actually 6) more lines (parse the fields):
Expand|Select|Wrap|Line Numbers
  1. strfield(51) = Mid(strLineInput, 729, 8)
  2. strfield(52) = Mid(strLineInput, 738, 8)
  3. strfield(53) = Mid(strLineInput, 747, 8)
And updating the table
Expand|Select|Wrap|Line Numbers
  1. rst![F51] = Trim(strfield(51))
  2. rst![F52] = Trim(strfield(52))
  3. rst![F53] = Trim(strfield(53))
The fields were added to the table as F51 - Date/Time - format is Long Time - etc.

I get the Subscript out of Range on strfield(53) = Mid(strLineInput, 747, 8). I've even changed the start position to 738 an length to 8 (same as 52) and still get the error.

I sure don't want to have to do something different since this was working before. Anyone have any ideas as to why it won't bring in that last field?

Any help is much appreciated.
Oct 31 '07 #1
5 3243
Rabbit
12,516 Expert Mod 8TB
I don't see strField dimmed anywhere. However, here's what I think the problem is, your array might be 50 elements big, but the index starts, by default, at 0 and ends at 49. So the first element of the array is strField(0) not strField(1). And the last element is strField(49) not strField(50).

But, isn't there a command to import a delimited text file? Why write code for each field?
Oct 31 '07 #2
I don't see strField dimmed anywhere. However, here's what I think the problem is, your array might be 50 elements big, but the index starts, by default, at 0 and ends at 49. So the first element of the array is strField(0) not strField(1). And the last element is strField(49) not strField(50).

But, isn't there a command to import a delimited text file? Why write code for each field?
Thanks Rabbit!!! - strfield was dimmed but it was strfield(52)...I changed it to 53 and all is fine!! I didn't do the import command because even though the field length is 25 - - the actual field imported could be 17...wanted to trim the spaces upon import.

Thanks so much!!!!
Oct 31 '07 #3
Rabbit
12,516 Expert Mod 8TB
Thanks Rabbit!!! - strfield was dimmed but it was strfield(52)...I changed it to 53 and all is fine!! I didn't do the import command because even though the field length is 25 - - the actual field imported could be 17...wanted to trim the spaces upon import.

Thanks so much!!!!
Not a problem. But that's weird, you're trying to access index 50, which is under 52 so there shouldn't have been a problem.
Oct 31 '07 #4
Not a problem. But that's weird, you're trying to access index 50, which is under 52 so there shouldn't have been a problem.
Index 50 is where the original code stopped - I had to add 51-53 in order to get the additional 3 fields in there and it stopped at 53 (cause i had it dimmed at 52)...which I didn't look at before you mentioned it. Ya'll are amazing - - I have been trying to figure it out since this morning and even though ya'll can't "see" the code, the extra pair of eyes help out tremendously! Thanks!
Oct 31 '07 #5
Rabbit
12,516 Expert Mod 8TB
Not a problem, good luck.
Oct 31 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: andy.rich | last post by:
I am getting the following error and I do not know why. Can anyone help? -------------------------------------------------------- this is what appears on the screen...
5
by: Richard Delorme | last post by:
The n869 draft says: J.2 Undefined behavior The behavior is undefined in the following circumstances: -- An array subscript is out of range, even if an object is ...
8
by: VB Programmer | last post by:
I'm acutally using VB6, not VB.NET, but I couldn't find the newsgroup for version 6.... I need help for something that should be simple. I keep getting a "subscript out of range" error and...
51
by: Pedro Graca | last post by:
I run into a strange warning (for me) today (I was trying to improve the score of the UVA #10018 Programming Challenge). $ gcc -W -Wall -std=c89 -pedantic -O2 10018-clc.c -o 10018-clc...
0
by: wennerd | last post by:
I am getting a Subscript out of range after executing the following command Set oRstPick = connTemp.Execute(sSQL) It is a simple query: sSQL = "SELECT idCategory, categoryDesc, idParentCategory...
6
by: josh | last post by:
Hi I've a dubt! when we have overloaded functions the compiler chooses the right being based on the argument lists...but when we have two subscript overloaded functions it resolves them being...
6
by: Andy | last post by:
Hi all, I started developing a little app on my Mac using XCode some month ago. The app is running fine on my mac like a sharm. Now I am nearly ready and yesterday I moved the whole source code...
2
by: subramanian100in | last post by:
For vector and deque, the 'at( )' member function throws out_of_range exception if the argument to the 'at( )' function is not in range. But the subscript operator does not throw this exception...
4
by: Han | last post by:
when I exe my project in vs.net2005,I got the error following: Debug Assertion Failed! Program:........ File:c:\program files\microsoft visual studio 8\vc\include\vector Line:756 ...
6
by: brandon01 | last post by:
Keep getting "subscript out of range" any idea why? This function is looped btw.. Thx... Private Sub getBday() bDay = List1.List(nextBday) bDayArr = Split(bDay, " - ") Text1.Text =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.