473,407 Members | 2,312 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,407 software developers and data experts.

Parse data from 1 table into multiple txt files

I am trying to write a module that will basically loop through a list of values, execute a sql statement based on the value and write the query results to a file with the filename being XXvalue.txt

I have gotten to the point where the sql statement is built and I can print it out in the immediate window. But I can't for the life of me get the darn SQL statement to execute. Here is my code:

Sub loopSchool()

Dim myConn As ADODB.Connection
Set myConn = CurrentProject.Connection
Dim MyRecSet As New ADODB.Recordset
MyRecSet.ActiveConnection = myConn
MyRecSet.Open "Schools", , adOpenStatic, adLockOptimistic
Dim schNbr
Dim mySQL As String

MyRecSet.MoveFirst

While Not MyRecSet.EOF

schNbr = MyRecSet.Fields("No").Value
mySQL = "select * from schools where no = '" & schNbr & "'"

'Debug.Print mySQL 'this works
DoCmd.RunSQL mySQL, 0 'this doesn't

MyRecSet.MoveNext
Wend

MyRecSet.Close
Set MyRecSet = Nothing
Set myConn = Nothing

End Sub
Oct 3 '06 #1
5 2453
PEB
1,418 Expert 1GB
Hi,

Yeah it shouldn't work! It's sure!

Try with

MyRecSet1.Open mySQL, , adOpenStatic, adLockOptimistic

And cycle this recordset to output the information in your txt file!

Best regards!

I am trying to write a module that will basically loop through a list of values, execute a sql statement based on the value and write the query results to a file with the filename being XXvalue.txt

I have gotten to the point where the sql statement is built and I can print it out in the immediate window. But I can't for the life of me get the darn SQL statement to execute. Here is my code:

Sub loopSchool()

Dim myConn As ADODB.Connection
Set myConn = CurrentProject.Connection
Dim MyRecSet As New ADODB.Recordset
MyRecSet.ActiveConnection = myConn
MyRecSet.Open "Schools", , adOpenStatic, adLockOptimistic
Dim schNbr
Dim mySQL As String

MyRecSet.MoveFirst

While Not MyRecSet.EOF

schNbr = MyRecSet.Fields("No").Value
mySQL = "select * from schools where no = '" & schNbr & "'"

'Debug.Print mySQL 'this works
DoCmd.RunSQL mySQL, 0 'this doesn't

MyRecSet.MoveNext
Wend

MyRecSet.Close
Set MyRecSet = Nothing
Set myConn = Nothing

End Sub
Oct 8 '06 #2
Hi,

Yeah it shouldn't work! It's sure!

Try with

MyRecSet1.Open mySQL, , adOpenStatic, adLockOptimistic

And cycle this recordset to output the information in your txt file!

Best regards!



How can I cycle the recorset to send to the TXT file? Can you please provide the code? Thanks much!
Nov 8 '06 #3
PEB
1,418 Expert 1GB
Ok your version can run coz this is Select Query and isn't an action query
Expand|Select|Wrap|Line Numbers
  1. Sub loopSchool()
  2.  
  3. Dim myConn As ADODB.Connection
  4. Set myConn = CurrentProject.Connection
  5. Dim MyRecSet As New ADODB.Recordset
  6. Dim MyRecSet1 As New ADODB.Recordset
  7. MyRecSet.ActiveConnection = myConn
  8. MyRecSet.Open "Schools", , adOpenStatic, adLockOptimistic
  9. Dim schNbr
  10. Dim mySQL As String
  11.  
  12. MyRecSet.MoveFirst
  13.  
  14. While Not MyRecSet.EOF
  15.  
  16. schNbr = MyRecSet.Fields("No").Value
  17. mySQL = "select * from schools where no = '" & schNbr & "';"
  18.  
  19. MyRecSet1.ActiveConnection = myConn
  20. MyRecSet1.Open mySQL, , adOpenStatic, adLockOptimistic
  21.     Open "myFile.txt" for output AS #1
  22.     While Not MyRecSet.EOF
  23.      Print #1, MyRecSet1.Fields("A").Value+", "+ MyRecSet1.Fields("B").Value ... and so on /your fields to be vizualized/
  24.  
  25.     MyRecSet1.MoveNext
  26.     Wend
  27.    Close #1
  28. 'Debug.Print mySQL 'this works
  29. DoCmd.RunSQL mySQL, 0 'this doesn't
  30.  
  31. MyRecSet.MoveNext
  32. Wend
  33.  
  34. MyRecSet.Close
  35. Set MyRecSet = Nothing
  36. Set myConn = Nothing
  37.  
  38. End Sub
  39.  
Here i give you idea how you can export your recordsets in files.. but nothing about the names of your fields and the columns of your recordsets... Here you can a lot on this function

There is also other way... Save the SQL that you've generated in a query and then use docmd. to transfer the information to a text file...

As you want and as is more suitable for your case!
Nov 19 '06 #4
MMcCarthy
14,534 Expert Mod 8TB
Ok, the first thing is you can't use DoCmd.RunSQL on a SELECT statement. It is for Action queries only, like INSERT, UPDATE and DELETE.

Secondly, what you are doing doesn't make any sense. You open a recordset of the table Schools. In your loop you get the value in the No field. The you try to run a select everything from schools where that value is in the no field.

However, because you are looping through every record in the Schools table then even if it was working it would create multiple repeats. For instance if the value in no was repeated 1000 times you would have a query of those 1000 records being repeated 1000 times for every time the loop reached the next record of that value in the query.

If you tell me the logic of what you are trying to do and why I will help you build the function.


Expand|Select|Wrap|Line Numbers
  1. Sub loopSchool()
  2.  
  3. Dim myConn As ADODB.Connection
  4. Set myConn = CurrentProject.Connection
  5. Dim MyRecSet As New ADODB.Recordset
  6. MyRecSet.ActiveConnection = myConn
  7. MyRecSet.Open "Schools", , adOpenStatic, adLockOptimistic
  8. Dim schNbr
  9. Dim mySQL As String
  10.  
  11. MyRecSet.MoveFirst
  12.  
  13. While Not MyRecSet.EOF
  14.  
  15. schNbr = MyRecSet.Fields("No").Value
  16. mySQL = "select * from schools where no = '" & schNbr & "'"
  17.  
  18. 'Debug.Print mySQL 'this works
  19. DoCmd.RunSQL mySQL, 0 'this doesn't
  20.  
  21. MyRecSet.MoveNext
  22. Wend
  23.  
  24. MyRecSet.Close
  25. Set MyRecSet = Nothing
  26. Set myConn = Nothing
  27.  
  28. End Sub
  29.  
  30.  
  31. [quote=sherylnance]I am trying to write a module that will basically loop through a list of values, execute a sql statement based on the value and write the query results to a file with the filename being XXvalue.txt
  32.  
  33. I have gotten to the point where the sql statement is built and I can print it out in the immediate window. But I can't for the life of me get the darn SQL statement to execute. Here is my code:
  34.  
  35. Sub loopSchool()
  36.  
  37. Dim myConn As ADODB.Connection
  38. Set myConn = CurrentProject.Connection
  39. Dim MyRecSet As New ADODB.Recordset
  40. MyRecSet.ActiveConnection = myConn
  41. MyRecSet.Open "Schools", , adOpenStatic, adLockOptimistic
  42. Dim schNbr
  43. Dim mySQL As String
  44.  
  45. MyRecSet.MoveFirst
  46.  
  47. While Not MyRecSet.EOF
  48.  
  49. schNbr = MyRecSet.Fields("No").Value
  50. mySQL = "select * from schools where no = '" & schNbr & "'"
  51.  
  52. 'Debug.Print mySQL 'this works
  53. DoCmd.RunSQL mySQL, 0 'this doesn't
  54.  
  55. MyRecSet.MoveNext
  56. Wend
  57.  
  58. MyRecSet.Close
  59. Set MyRecSet = Nothing
  60. Set myConn = Nothing
  61.  
  62. End Sub
Nov 19 '06 #5
NeoPa
32,556 Expert Mod 16PB
Also check type of [No].
If it's a string then fine - otherwise there should be no quotes (').
Nov 19 '06 #6

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

Similar topics

19
by: Peter A. Schott | last post by:
I've got a file that seems to come across more like a dictionary from what I can tell. Something like the following format: ###,1,val_1,2,val_2,3,val_3,5,val_5,10,val_10...
10
by: dreamcatcher | last post by:
I want my program to parse INI files, only have little clue of how to do that, though, hope you guys might shed some light on this, thanx. for example:
19
by: Johnny Google | last post by:
Here is an example of the type of data from a file I will have: Apple,4322,3435,4653,6543,4652 Banana,6934,5423,6753,6531 Carrot,3454,4534,3434,1111,9120,5453 Cheese,4411,5522,6622,6641 The...
7
by: Randy Yates | last post by:
I'm a complete newbie to postgres so please look the other way if these questions are really stupid. Is it legitimate to have one database per data file? For organizational and backup purposes,...
7
by: akira | last post by:
Hello, I need to parse this XML file or XML stream . Is it possible to parse it with DOM reading each DataContainer one by one. <DataContainer> <name> </name> <birthDate> </birthDate>...
14
by: Rob Meade | last post by:
Hi all, I'm working on a project where there are just under 1300 course files, these are HTML files - my problem is that I need to do more with the content of these pages - and the thought of...
5
by: BMeyer | last post by:
I have been losing my mind trying to parse an XML document (with nested child elements, not all of which appear in each parent node) into a DataGrid object. What I want to do is "flatten" the XML...
5
by: Chris | last post by:
I have a meetings section I'm developing on our intranet. Using PHP/MySQL. Meeting info and Meeting docs reside on 2 related tables in the db. Users may want to upload anywhere from 1 to 10 or...
13
by: DH | last post by:
Hi, I'm trying to strip the html and other useless junk from a html page.. Id like to create something like an automated text editor, where it takes the keywords from a txt file and removes them...
1
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.