473,387 Members | 1,863 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,387 software developers and data experts.

Export data to sequential file

Hi,

Not sure if this is the right place to post this query so apologies in advance.

I have two columns in an access table.

Example for 2 records:

Field 1 - Retail
Field 2 - Sat
Field 1 - Direct
Field 2 - Sun


Can someone tell me how you output this as a sequential file? So the text file reads Retail#SatDirect#Sun ...so that a hash is inserted after field 1 and a carriage return is entered after field 2.

Any help would be appreciated.
Mar 5 '07 #1
19 3386
Rabbit
12,516 Expert Mod 8TB
Does it have to be a hash? You could do basically the same thing with a csv.
Mar 5 '07 #2
Does it have to be a hash? You could do basically the same thing with a csv.
Yes, has to be a hash as a script then reads the text file. Can this be done...i.e so the output is not column by column but reads across the way? I have tried looking around for a solution but have drawn a big blank.
Mar 5 '07 #3
Rabbit
12,516 Expert Mod 8TB
You can choose which delimiter you want to use from the export wizard if you select the text file format.
Mar 5 '07 #4
Maybe I am not being clear. I dont need help with the export function...i.e tab delimited etc. I need help creating a sequential file.

Example:

Field 1, Field 2
Field 1, Field 2
Field 1, Field 2

The above is an example of exporting the file. What I need is the file to look like:

Field 1, Field 2, Field 1, Field 2, Field 1, Field 2

So all records run one after the other across the way.
Mar 6 '07 #5
r035198x
13,262 8TB
Changed thread title.
Mar 6 '07 #6
Rabbit
12,516 Expert Mod 8TB
Your first post said you wanted a carriage return after the second field, is that not a new line?

From the help file:
Expand|Select|Wrap|Line Numbers
  1. CreateTextFile Method
  2.  
  3. Description:
  4. Creates a specified file name and returns a TextStream object that
  5. can be used to read from or write to the file.
  6.  
  7. Syntax: object.CreateTextFile(filename[, overwrite[, unicode]])
  8.  
  9. The CreateTextFile method has these parts:
  10.  
  11. object - Required: Always the name of a FileSystemObject or Folder
  12. object.  filename Required. String expression that identifies the file
  13. to create. 
  14.  
  15. overwrite - Optional: Boolean value that indicates if an existing file
  16. can be overwritten. The value is True if the file can be overwritten; False
  17. if it can't be overwritten. If omitted, existing files are not overwritten. 
  18.  
  19. unicode - Optional: Boolean value that indicates whether the file is
  20. created as a Unicode or ASCII file. The value is True if the file is created
  21. as a Unicode file; False if it's created as an ASCII file. If omitted, an
  22. ASCII file is assumed. 
  23.  
  24.  
  25. Remarks
  26.  
  27. The following code illustrates how to use the CreateTextFile method to
  28. create and open a text file:
  29.  
  30. Sub CreateAfile
  31.     Set fs = CreateObject("Scripting.FileSystemObject")
  32.     Set a = fs.CreateTextFile("c:\testfile.txt", True)
  33.     a.WriteLine("This is a test.")
  34.     a.Close
  35. End Sub
  36.  
  37. If the overwrite argument is False, or is not provided, for a filename
  38. that already exists, an error occurs.
So what you'll have to do is create a loop that concatenates all your data into one string.
Mar 6 '07 #7
ADezii
8,834 Expert 8TB
Hi,

Not sure if this is the right place to post this query so apologies in advance.

I have two columns in an access table.

Example for 2 records:

Field 1 - Retail
Field 2 - Sat
Field 1 - Direct
Field 2 - Sun


Can someone tell me how you output this as a sequential file? So the text file reads Retail#SatDirect#Sun ...so that a hash is inserted after field 1 and a carriage return is entered after field 2.

Any help would be appreciated.
Assuming [Field 1] and [Field 2] exist in a Table named Table1, here is your basic code template. You will only need minor modifications:
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database, MyRS As DAO.Recordset
  2. Dim strLongString As String
  3.  
  4. Set MyDB = CurrentDb()
  5. Set MyRS = MyDB.OpenRecordset("Table1", dbOpenSnapshot)
  6.  
  7. Do While Not MyRS.EOF
  8.   'Build up the String here
  9.   strLongString = strLongString & MyRS![Field 1] & ", " & MyRS![Field 2] & ", "
  10.   MyRS.MoveNext
  11. Loop
  12.  
  13. MyRS.Close
  14.  
  15. 'Remove Trailing Comma and Space
  16. strLongString = Left$(strLongString, Len(strLongString) - 2)
  17.  
  18. Open "C:\Sequential.txt" For Output As #1
  19. Print #1, strLongString
  20. Close #1
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. Retail, Sat, Direct, Sun, Blah, Mon, Whatever, Tue
Mar 6 '07 #8
Rabbit
12,516 Expert Mod 8TB
Assuming [Field 1] and [Field 2] exist in a Table named Table1, here is your basic code template. You will only need minor modifications:
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database, MyRS As DAO.Recordset
  2. Dim strLongString As String
  3.  
  4. Set MyDB = CurrentDb()
  5. Set MyRS = MyDB.OpenRecordset("Table1", dbOpenSnapshot)
  6.  
  7. Do While Not MyRS.EOF
  8.   'Build up the String here
  9.   strLongString = strLongString & MyRS![Field 1] & ", " & MyRS![Field 2] & ", "
  10.   MyRS.MoveNext
  11. Loop
  12.  
  13. MyRS.Close
  14.  
  15. 'Remove Trailing Comma and Space
  16. strLongString = Left$(strLongString, Len(strLongString) - 2)
  17.  
  18. Open "C:\Sequential.txt" For Output As #1
  19. Print #1, strLongString
  20. Close #1
The problem with Print # is that with a string, it will enclose it in double quotes.
Mar 6 '07 #9
NeoPa
32,556 Expert Mod 16PB
Maybe I am not being clear. I dont need help with the export function...i.e tab delimited etc. I need help creating a sequential file.

Example:

Field 1, Field 2
Field 1, Field 2
Field 1, Field 2

The above is an example of exporting the file. What I need is the file to look like:

Field 1, Field 2, Field 1, Field 2, Field 1, Field 2

So all records run one after the other across the way.
Maybe it's me, but you seem to be contradicting yourself.
In post #1 you say you want a Carriage Return after the Field2s. Now you say you want all the data showing in a single line with commas separating?
It's particularly hard to answer questions where the questioner themself seems to be unsure what they want.
I'm absolutely sure we have an answer for your question. If only you can express it clearly and unambiguously.
Mar 6 '07 #10
ADezii
8,834 Expert 8TB
The problem with Print # is that with a string, it will enclose it in double quotes.
Rabbit:
Write # will - not Print #. That is why I intentionally used it instead of the alternative. The Output will be exactly as shown.
Mar 6 '07 #11
Rabbit
12,516 Expert Mod 8TB
Rabbit:
Write # will - not Print #. That is why I intentionally used it instead of the alternative. The Output will be exactly as shown.
That works then. I must have gotten them mixed up.
Mar 6 '07 #12
ADezii
8,834 Expert 8TB
Maybe it's me, but you seem to be contradicting yourself.
In post #1 you say you want a Carriage Return after the Field2s. Now you say you want all the data showing in a single line with commas separating?
It's particularly hard to answer questions where the questioner themself seems to be unsure what they want.
I'm absolutely sure we have an answer for your question. If only you can express it clearly and unambiguously.
NeoPa:
That's exactly why I specified 'code template' in my solution.
Mar 6 '07 #13
NeoPa
32,556 Expert Mod 16PB
NeoPa:
That's exactly why I specified 'code template' in my solution.
I'm not sure I understand what you're explaining here ADezii.
I was not implying criticism of your post - or your code.
I was trying to get the OP to understand the importance of stating his requirements clearly and accurately.
If it needs to include the <CR> in each line then the way to proceed would surely be to use the Export function rather than Print# within VBA.
If not, then your solution would be the sensible approach.
We can't know which path to suggest without a more clearly stated question.
Mar 6 '07 #14
Yes, I can see how I confused the matter when I included an example with commas.

For clarity:

Field 1 - Retail
Field 2 - Sat
Field 1 - Direct
Field 2 - Sun

The required output for the above example would be:

Retail#SatDirect#Sun

So a hash is added after field 1 and a carriage return () is added after field 2. All the data would run sequentially (left to right).

The hash and carriage return symbol is needed because a script needs that format.

Hopefully that clarifies things and a solution can be provided.

Big Thanks
Mar 7 '07 #15
ADezii - Thanks for the code. Do I just copy this into a module? Just not sure how I use the code so that I can see the text file generated.
Mar 7 '07 #16
ADezii
8,834 Expert 8TB
ADezii - Thanks for the code. Do I just copy this into a module? Just not sure how I use the code so that I can see the text file generated.
Just Copy and Paste it wherever you want. It can be placed in the Click() Event of a Command Button. a Function Procedure, Sub-Routine Procedure, etc. Replace the comma after Field1 if you want a different Delimiter, replace the comma after Field2 if you want an End of Record indicator. Change the Path of Sequential.txt if you like, and simply Open it to view the Output. Any other questions, feel free to ask.
Mar 7 '07 #17
NeoPa
32,556 Expert Mod 16PB
Yes, I can see how I confused the matter when I included an example with commas.

For clarity:

Field 1 - Retail
Field 2 - Sat
Field 1 - Direct
Field 2 - Sun

The required output for the above example would be:

Retail#SatDirect#Sun

So a hash is added after field 1 and a carriage return () is added after field 2. All the data would run sequentially (left to right).

The hash and carriage return symbol is needed because a script needs that format.

Hopefully that clarifies things and a solution can be provided.

Big Thanks
Do you need a record separator of a Carriage Return specifically, or would a Carriage Return / Line Feed sequence (standard DOS/Windows line separator) do?
If so, then a standard text export would do for you.
If not, ADezii's code, with simple modifications to use the '#' & <CR> (vbCR constant in VBA) in place of the ','s would be the recommended approach.
In truth, for the former, using ADezii's code with the VbCrLf constant instead, would also work perfectly well for you.
Mar 7 '07 #18
Pasted the code in the click event of a command button but got a file/path access error. When I go into debug it highlights the line 'Open "C:\Sequential.txt" For Output As #1'.

I created a folder in my C: called sequential...was that the right thing to do?

Not very good with visual basic etc so not sure why that last line of code is not working.

Ps...cheers to all for the help, appreciated!
Mar 7 '07 #19
ADezii
8,834 Expert 8TB
Pasted the code in the click event of a command button but got a file/path access error. When I go into debug it highlights the line 'Open "C:\Sequential.txt" For Output As #1'.

I created a folder in my C: called sequential...was that the right thing to do?

Not very good with visual basic etc so not sure why that last line of code is not working.

Ps...cheers to all for the help, appreciated!
There may be restrictions on the System that involved writing to the Root Directory of Drive C: (C:\). You can create any Folder/Sub-Folder you like but just remember to modify your code, specifically:
Open "C:\NewFolder\Sub-Folder\Sequential.txt" For Output As #1
Mar 7 '07 #20

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

Similar topics

14
by: atse | last post by:
Hi experts, I retrieve data from the database and display on ASP, then I export these data to a file, like Excel (the best) or text file. Is it possible? I think it is possible, but how can I do...
0
by: Shawn Mehaffie | last post by:
I have the following class that I've wirtten to take a Dataset and automatically export it to either XML, ASCII or Tab delimited file. The reason I wrote it they way I did was that I don't want to...
3
by: Michael Van Altena via .NET 247 | last post by:
I'm trying to figure out how to read a formatted binary file intoa structure definition in C#. I've tried using the"StructLayout" attribute with both LayoutKind.Explicit andLayoutKind.Sequential...
1
by: Matt | last post by:
I have an ASP page that calls ASP routines that I created that execute a database query and return the results to a recordset. I then iterate through the recordset and display the data in a table....
1
by: Do Park via SQLMonster.com | last post by:
Hello all, I don?t often export data from a table. I am wondering how you export data from a table. I?d like to know how you export in real world. Do you export data from a table to a flat...
6
by: maricel | last post by:
Is there anybody out there who have any idea why EXPORT is relatively slower when putting the output file on a network drive - map drive from onother PC compared to putting it on my local PC drive...
0
by: Shawn Mehaffie | last post by:
I have the following class that I've wirtten to take a Dataset and automatically export it to either XML, ASCII or Tab delimited file. The reason I wrote it they way I did was that I don't want to...
1
by: chaitu | last post by:
Hi, Can anybody tell me what the error code for Export is. Like i get the message SQL3104N The Export utility is beginning to export data to file...
1
by: smaczylo | last post by:
Hello, I've recently been asked to work with Microsoft Access, and while I feel quite comfortable with Excel, I'm at a complete loss with databases. If someone could help me with this issue I'm...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.