Hi Jeremiah,
I'd do this by writing code to pre-process the text file before
importing, converting it into a regular text file with all lines having
the same fields.
VBA-like pseudocode:
Open file for input as #fIn
Open new text file for output as #fOut
Do Until EOF(fIn)
Line Input #fIn, strLine 'read line
If Left(strLine, 1) <> " " Then 'it's a date header
StartDate = blah 'extract dates from line
EndDate = blah
Else 'strLine contains an ordinary record
Trim leading and trailing spaces
strLine = StartDate & ";" & EndDate & strLine
'maybe at this point check and adjust
'the number of fields in the output line
'maybe also replace ; with , to convert fOut
'into a standard CSV file that Access can
'import without an import specification
Print #fOut, strLine
End If
Loop
Close #fIn
Close #fOut
The comment about adjusting the number of fields is because I noticed
that the records in your sample data don't have a consistent number of
fields. It might be worth appending a delimiter to the end of each line
that doesn't have the final field ("HPS" in your sample).
I usually use Perl for this sort of thing because it has much more
powerful text file and string-handling functions than VBA. Here's a
working Perl script that processes your test data:
use strict;
my $start; #start date
my $end; #end date
my $line;
my $fieldsNeeded = 5; #number of fields that should be output
my $fieldsToAdd; #number of empty fields to be appended
while (<>) { #read file line by line
chomp;
if (m/^\s+/) { #line beginning with space => an ordinary record
$_ =~ s/^\s+//; #trim leading space
$_ =~ s/\s+$//; #trim trailing space
$line = "$start;$end$_"; #prepend dates
$fieldsToAdd = $fieldsNeeded - split(/;/, $line);
$line .= ';' while (--$fieldsToAdd >= 0) ; #add fields if needed
$line ~= s/;/,/g; #change ';' to ','
print "$line\n"; #output line
} else { #heading line with dates
($start, $end) = split /-/ ;
}
}
Below is the first few lines of data - I need to be able to associate each
line of data with the correct date range - I import it delimited by
semicolons but the date headings do not follow the same format. I need to be
able to query and summarize by date.
02/25/07-03/03/07
;000.9122;NAME NOT AVAILABLE ;HPS
;000.9122;NAME NOT AVAILABLE
04/15/07-04/21/07
; 0030178;JOHN DOE;
; 0030178;NAME NOT AVAILABLE
;B0009530;NAME NOT AVAILABLE
;B0009530;NAME NOT AVAILABLE
:
Can you give an example of what the text file contains?
On Thu, 26 Apr 2007 09:14:03 -0700, jeremiah
When importing a text file that has a date range (04/23/07 - 04/30/07) as a
heading in it. I created a query that separates the dates into 2 new fields
by using...Start Date: First(Left([SHIFT],8)) & End Date:
First(Right([SHIFT],8)) and puts those dates into the new fields for each
record following that heading.
Each time I import the text and append it to the previous table there will
be a new date range, which may be row 10 or row 100, I never know which line
the date range will be in. I need to be able to use the New dates in those
records, not the dates in the first line of the table. Any ideas?