473,795 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

5 record sets making me crazy

25 New Member
I have an application that will produce a Word document based on five separate queries this has required that I create a ADODB connection:

'Create connection to current database
Dim Conn As ADODB.Connectio n
Set Conn = CurrentProject. Connection

There are five separate recordSets, one for each query. The record sets each fill out a table in the Word document, utilizing bookmarks. Some areas of the Word document have repeating tables. For example one of the tables has information about particular documents, like memos, letters. If there is to be more than one referenced document, the table repeats and is filled out based on the next record in the query. There is no problem with this.

It gets interesting when a separate query (and recordset) for the user notes is integrated. The memo or letter is described by these user notes to let the reader of the Word document know and understand the history being represented by the various letters and memos. Because of the required format of my Word document this requires a nested loop. See below code


'This repetition is for the record query and macro

'Variables for recordset in database, command for recordset
Dim lettersQuery As New ADODB.Recordset
Dim comLetters As New ADODB.Command

'Name of Access query
Dim lettersQuery As String
lettersQuery = "lettersQue ry"

With comLetters
Set .ActiveConnecti on = Conn
.CommandText = "lettersQue ry"
.CommandType = adCmdStoredProc
Set parameter = .CreateParamete r("ReferenceNo" , adVarChar, _
adParamInput, Len(RefNo), RefNo)
.Parameters.App end parameter
End With

lettersQuery.Op en comLetters, , adOpenKeyset, adLockOptimisti c

'Loop for letters
Do While Not lettersQuery.EO F

'Run macro in Word document
wordApp.Run "CreateLettersT able"

'Populate document bookmarks with query data
((code not relevant))

'Loop for notes

'Set letterNo variable equal to current record entry number
LetterNo = lettersQuery("L etterNo").Value

'Variables for recordset in database, command for recordset
Dim notesQuery As New ADODB.Recordset
Dim comNotes As New ADODB.Command

'Name of Access query
Dim notesQuery As String
notesQuery = "notesQuery "

With comNotes
Set .ActiveConnecti on = Conn
.CommandText = "notesQuery "
.CommandType = adCmdStoredProc
Set parameter = .CreateParamete r("LetterNo", adVarChar, _
adParamInput, Len(LetterNo), LetterNo)
.Parameters.App end parameter
End With

notesQuery.Open comNotes, , adOpenKeyset, adLockOptimisti c ************

'Loop for notes
Do While Not notesQuery.EOF

'Run macro in Word document
If notesQuery.Abso lutePosition > 1 Then
wordApp.Run "AddNoteLin e"
End If

'Populate document bookmarks with query data
((code not relevant))

'Move to next record in data source
notesQuery.Move Next

Loop 'End of notes loop.

'Close record set
notesQuery.Clos e
'Set notesQuery = Nothing
comNotes.Cancel

'Move to next record in data source
lettersQuery.Mo veNext

Loop 'End of letters loop

'Close record set
lettersQuery.Cl ose
comLetters.Canc el

I have tried to simplify the scenario and names of things in the code a little bit I may have so typos I didn’t catch. The point is - the code works - what happens is instead of adding the appropriate notes to the second and subsequent letters, it adds the notes for the first letter, it is as if the first recordset for the notes is still active. The stars (***) are under the line that is not working properly the second time through the loop. The query works correctly and pulls the correct information. I have tried setting the record set to nothing with no results.

I hope this makes sense
Nov 22 '06 #1
19 2788
NeoPa
32,579 Recognized Expert Moderator MVP
I'm confused that you define some variables twice (notesQuery for instance).
Though I notice it is the same for the working code.
The only thing that occurs to me is that perhaps the notes query needs more than the one parameter set.
Nov 22 '06 #2
Genalube
25 New Member
Those are some of the typos I was afraid I had created, in my code they are only defined once, I intended to type notesQueryRs or something like it for the record set. My query only has one parameter, the letters table has a one to many relationship with the notes table and it is adequate to query by letter number. The appropriate letter number is passed to the notes query, it just won't let go of the previous results.
Nov 22 '06 #3
NeoPa
32,579 Recognized Expert Moderator MVP
I think it would be a good idea to post actual tested code here.
I appreciate the stripping out of irrelevant code, but the actual code should be cleared of typos and compiled before posting.

I'm a little confused as it did sound as if you had done that already from your first post.
I suspect you're 'paraphrasing' for brevity, which is a nice idea, but more important is that the code is compiled code.

Not that I can promise to solve your problem for you - it looks quite complicated - but at least I can have another look.
BTW don't forget to use the CODE tags - it keeps the formatting of the code ;).
Nov 22 '06 #4
MMcCarthy
14,534 Recognized Expert Moderator MVP
try ...

Expand|Select|Wrap|Line Numbers
  1.  
  2. lettersQuery.Close
  3. Set lettersQuery = Nothing
  4.  
and so on.
Nov 22 '06 #5
Genalube
25 New Member
To NeoPa

I was paraphrasing my code, I only included about a tenth or less of the code I have written. In order for you to be able to test the code, I think would have to send you data, and some word templates. I think that would be asking too much. Maybe I am misunderstandin g. I tried a few other things, see below:

To mmccarthy

I tried doing that but it didn't work

I think when I run the query the second time in the loop, and it uses the first query's name for the recordset, it decides I want the first query's results. Even after setting the first recordset to nothing, the same thing happens.

I guess the query results could be stored somewhere other than the recordset, but I thought that was what the recordset did.

If I could come up with a way to rename the recordset each time it loops my problem would be solved.

I tried making an array of recordsets but I haven't done that before and am having a time of it - at least naming the recordsets differently in the array.
Nov 28 '06 #6
NeoPa
32,579 Recognized Expert Moderator MVP
Genalube,

I appreciate that (in both senses of the word). Chopping it down makes it a lot easier to deal with.
The point I was trying to make (without intending to sound critical) is that when you chop anything out it still needs to be tested to ensure :
1. It still retains its integrity (compiles - works as expected etc)
2. It still reflects the same problem that is being searched for.
Otherwise we could be wasting even more time than was saved 'chasing shadows'.

Again, let me assure you this is just a fuller explanation of what I was trying to say and should not be taken as criticism.
Good luck with your problem btw.
Nov 29 '06 #7
MMcCarthy
14,534 Recognized Expert Moderator MVP
Can I make a suggestion.

Unless you need to have this code in ADO for some reason can it be rewritten in DAO as I think this will solve most of your problems.

Although I don't normally recommend it, I think we will need to see all of your code as I can't follow the logic of what you are doing with what you've posted.

Post the full code and if you don't have a problem with DAO I will rewrite it as DAO code and I think this will solve the problem.

Mary
Nov 29 '06 #8
Genalube
25 New Member
NeoPa, Mary

Thank you, I hope my reply didn't sound angry, I am just new at this and feel like I am imposing myself upon others.

I found examples of ADO, so I utilized them, I don't really understand the difference. What are the advantages of both?

Let me apologize for my code, I am sure I am writing sloppily and excessively, but I think I comment everything (perhaps to excess).

I tried to make my code generic when I was paraphrasing it, it is a very specific application designed to work with real estate and the documents that are associated with it.

There is a lot of code, about nine printed pages of it, I don't want you rewrite all of it, but understand you need to see all of it to understand what I am doing. I feel like I am taking advantage or abusing this forum by posting this much code. Forgive the impropriety.

Thank you both very much, whenever I run into a problem and web search for an answer, I find them on this forum more often than any other. It is very kind of you to help people like me.

I tried to put the repetition of the record set into a different sub (like the set bookmark sub), but found the connection variable couldn't be seen inside it, the bookmark setting is also quite variable, which I tried to do with case statements. Creating this application is my first experience with Visual Basic, and am new to programming in general, I am learning a lot, but it probably violates good formatting conventions.

----------------

Apparently there is too much code to post, I can break it up or send it to you some other way. What would you recomend?
Nov 29 '06 #9
MMcCarthy
14,534 Recognized Expert Moderator MVP
OK.

Firstly 9 pages of code sounds way too much.

DAO vs. ADO is a long running argument that I won't get into now. However, ADO is not needed for programming connections between or within Access Databases and DAO is easier to understand and code.

Forget about posting the 9 pages as I suspect the code is impractical.

Post the logic of what you are trying to do in English giving the relevant table names, field names, forms and controls. Post each of the recordset statements in full descibing what you are trying to do in each case.

Using english to descibe what steps you are trying to take and what results you want from each step. This should make things clearer and if you provide all the necessary information as above we should be able to help you put the code together in DAO.

Don't worry so much about the nature of your problem as we appreciate the fact that you've obviously tried very hard to put this together yourself.

Mary
Nov 29 '06 #10

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

Similar topics

1
1728
by: David | last post by:
Hi, I have a continuous form based on a query. Lets say the form displays 6 records. I also have a button against each record which sets a field to Y or N for each record. I am trying to copy partial fields only for those records where the field = Y
0
1287
by: swapna_munukoti | last post by:
Hi all, I have seen in so many articles that record locking is not possible in MS-Access, but we can achieve it by making the size of each record to 1024 bytes(Let us say this is the record locking limit size). I want to know if there is any setting in the DB that sets the record locking limit size. Thanks, Swapna.
2
2442
by: Mark Reed | last post by:
Hi All, I have created a multi-user application at work which is working perfectly apart from a small problem which I believe to more a of a user issue (maybe some will set me straight on that one). I have set the 'Default Record Locking' option to 'Edited Record' in the hope that if someone tries to edit a record someone has open, they would not be able to do so. After a little testing, I found this not to be the case. Instead, if 2...
9
24681
by: Joshua.Buss | last post by:
I am trying to move a record from one linked table to another within access, but I'm a complete beginner to VBA and don't know exactly where to begin. I have an access file that has the two linked tables, which each link to separate SQL databases using separate ODBC connections. I have a form that has a view of table1 and a set of buttons for manipulating the data in database1.
0
9522
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10448
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10003
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9046
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7544
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6784
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5566
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2922
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.