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
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
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!
PEB 1,418
Expert 1GB
Ok your version can run coz this is Select Query and isn't an action query -
Sub loopSchool()
-
-
Dim myConn As ADODB.Connection
-
Set myConn = CurrentProject.Connection
-
Dim MyRecSet As New ADODB.Recordset
-
Dim MyRecSet1 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 & "';"
-
-
MyRecSet1.ActiveConnection = myConn
-
MyRecSet1.Open mySQL, , adOpenStatic, adLockOptimistic
-
Open "myFile.txt" for output AS #1
-
While Not MyRecSet.EOF
-
Print #1, MyRecSet1.Fields("A").Value+", "+ MyRecSet1.Fields("B").Value ... and so on /your fields to be vizualized/
-
-
MyRecSet1.MoveNext
-
Wend
-
Close #1
-
'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
-
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!
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. -
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
-
-
-
[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
-
-
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
NeoPa 32,556
Expert Mod 16PB
Also check type of [No].
If it's a string then fine - otherwise there should be no quotes (').
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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:
|
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...
|
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,...
|
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>...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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,...
|
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...
|
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: 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...
|
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,...
| |