Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Business Applications
Tag Cloud
access acer asus bios bsod computer crash desktop dns driver drivers error ethernet excel freeze gaming graphics hard drive hardware hdmi internet laptop malware memory monitor motherboard network printer problem ram registry repair router slow software sound trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Software & Hardware > Business Applications >
Solved: MS Access VBA Module

Reply  
Thread Tools
Hockeyball's Avatar
Junior Member with 5 posts.
 
Join Date: Nov 2009
Experience: Intermediate
11-Nov-2009, 09:12 AM #1
Solved: MS Access VBA Module
I have a module which I loop through and create a comma separated text file. It works for the most part but 1 line of text I write I cannot get in the right spot, it is a trailer record.
Here is my code and after that is a sample table and then my output and then how I would like the output. I cannot get the TRL,MOAA, line to print after each ID, it prints after each SEQ.
Basically it is a trailer record for the ID.

Code
Private Sub BuildFile()
Dim db As Database, rst As Recordset, ID, Item
Dim lngFile As Long
Set db = CurrentDb
Set rst = db.OpenRecordset("SELECT * " & "FROM InputTable ORDER BY ID, Seq", dbOpenDynaset)
'/open text file
lngFile = FreeFile
Open "C:\FileLocation\Testfile.txt" For Output Access Write As #lngFile
rst.MoveFirst
Do Until rst.EOF
Print #lngFile, "This is ID " & Format(rst!ID, "000000000") & "HDR," & rst![Test] & ",END"
ID = rst!ID
Do While rst!ID = ID
Item = rst!Item
Print #lngFile, "This is ID " & Format(rst!ID, "000000000") & "DTL," & rst![Seq] & ",END"
Print #lngFile, "This is ID " & Format(rst!ID, "000000000") & "DTLTX," & rst![Seq] & ",TSQ1" & rst![ID] & ",TLN1,END"
Print #lngFile, "This is ID " & Format(rst!ID, "000000000") & "DTLTX," & rst![Seq] & ",TSQ2" & rst![MainLine] & ",TLN2,END"
Print #lngFile, "This is ID " & Format(rst!ID, "000000000") & "TRL,MOAA,NDTL" & rst![SeqCount] & ",NMS0,NBS1,NTX" & rst![DetailLines] & ",END"
rst.MoveNext
If rst.EOF Then Exit Do
Do While rst!Item = Item
rst.MoveNext
If rst.EOF Then Exit Do
Loop
If rst.EOF Then Exit Do
Loop
Loop
rst.Close
db.Close
Close lngFile
Set rst = Nothing
End Sub

Table
ID SEQ ITEM MAINLINE TEST SEQCOUNT DETAILLINES
100 1 Item1 John Y 2 2
100 2 Item2 George Y 2 2
101 1 Item5 Deb N 1 2
102 1 Item1 Jewel Y 2 2
102 2 Item3 Lisa Y 2 2

Current Output
This is ID 000000100HDR,Y,END
This is ID 000000100DTL,1,Item1,END
This is ID 000000100DTLTX,1,TSQ1100,TLN1,END
This is ID 000000100DTLTX,1,TSQ2John,TLN2,END
This is ID 000000100TRL,MOAA,NDTL2,NMS0,NBS1,NTX2,END
This is ID 000000100DTL,2,Item2,END
This is ID 000000100DTLTX,2,TSQ1101,TLN1,END
This is ID 000000100DTLTX,2,TSQ2George,TLN1,END
This is ID 000000100TRL,MOAA,NDTL2,NMS0,NBS1,NTX2,END
This is ID 000000101HDR,N,END
This is ID 000000101DTL,1,Item5,END
This is ID 000000101DTLTX,1,TSQ1101,TLN1,END
This is ID 000000101DTLTX,1,TSQ2Deb,TLN2,END
This is ID 000000101TRL,MOAA,NDTL1,NMS0,NBS1,NTX2,END
This is ID 000000102HDR,Y,END
This is ID 000000102DTL,1,Item1,END
This is ID 000000102DTLTX,1,TSQ1102,TLN1,END
This is ID 000000102DTLTX,1,TSQ2Jewel,TLN2,END
This is ID 000000102TRL,MOAA,NDTL2,NMS0,NBS1,NTX2,END
This is ID 000000102DTL,2,Item3,END
This is ID 000000102DTLTX,2,TSQ1102,TLN1,END
This is ID 000000102DTLTX,2,TSQ2Lisa,TLN2,END
This is ID 000000102TRL,MOAA,NDTL2,NMS0,NBS1,NTX2,END

Expected Output
This is ID 000000100HDR,Y,END
This is ID 000000100DTL,1,Item1,END
This is ID 000000100DTLTX,1,TSQ1100,TLN1,END
This is ID 000000100DTLTX,1,TSQ2John,TLN2,END
This is ID 000000100DTL,2,Item2,END
This is ID 000000100DTLTX,2,TSQ1101,TLN1,END
This is ID 000000100DTLTX,2,TSQ2George,TLN2,END
This is ID 000000100TRL,MOAA,NDTL2,NMS0,NBS1,NTX2,END
This is ID 000000101HDR,N,END
This is ID 000000101DTL,1,Item5,END
This is ID 000000101DTLTX,1,TSQ1101,TLN1,END
This is ID 000000101DTLTX,1,TSQ2Deb,TLN2,END
This is ID 000000101TRL,MOAA,NDTL1,NMS0,NBS1,NTX2,END
This is ID 000000102HDR,Y,END
This is ID 000000102DTL,1,Item1,END
This is ID 000000102DTLTX,1,TSQ1102,TLN1,END
This is ID 000000102DTLTX,1,TSQ2Jewel,TLN2,END
This is ID 000000102DTL,2,Item3,END
This is ID 000000102DTLTX,2,TSQ1102,TLN1,END
This is ID 000000102DTLTX,2,TSQ2Lisa,TLN2,END
This is ID 000000102TRL,MOAA,NDTL2,NMS0,NBS1,NTX2,END
OBP's Avatar
OBP OBP is offline
Computer Specs
Distinguished Member with 14,665 posts.
 
Join Date: Mar 2005
Location: UK
Experience: An old Basic Programmer
11-Nov-2009, 09:40 AM #2
The End line needs to be outside the Loop.
Hockeyball's Avatar
Junior Member with 5 posts.
 
Join Date: Nov 2009
Experience: Intermediate
11-Nov-2009, 10:26 AM #3
As soon as I do that I get Run-time error '3021' No current record.
I believe I still need it as part of the loop as I need to print after each ID,
OBP's Avatar
OBP OBP is offline
Computer Specs
Distinguished Member with 14,665 posts.
 
Join Date: Mar 2005
Location: UK
Experience: An old Basic Programmer
11-Nov-2009, 10:55 AM #4
Did you put outside the Inner Loop?
Hockeyball's Avatar
Junior Member with 5 posts.
 
Join Date: Nov 2009
Experience: Intermediate
11-Nov-2009, 11:03 AM #5
I thought so but maybe I did something wrong.
Can you show me what you mean?
OBP's Avatar
OBP OBP is offline
Computer Specs
Distinguished Member with 14,665 posts.
 
Join Date: Mar 2005
Location: UK
Experience: An old Basic Programmer
11-Nov-2009, 11:12 AM #6
Well I would try it here
rst.MoveNext
If rst.EOF Then Exit Do
Loop
Code Here
If rst.EOF Then Exit Do
Loop
Loop

However as you are using the Items for the "Current ID" perhaps it would be easier to assign the values you want to use to Variables, you can then use them anywhere in your code.

Can you explain what each Loop does?
__________________
OBP
I do not give up easily
Hockeyball's Avatar
Junior Member with 5 posts.
 
Join Date: Nov 2009
Experience: Intermediate
11-Nov-2009, 11:59 AM #7
No luck I get the same error.
I am a novice at this and will do my best to explain the loops.
I am first looping through the ID's and then looping the items and printing the lines for each item under an ID. I then need to print a trailer at the end of the ID.
So I may have 2 items so what I need is
ID1 Header
ID1 Item 1 line 1
ID1 Item 1 line 2
ID1 Item 2 Line 1
ID1 Item 2 Line 2
ID1 Trailer
ID2 Header
ID2 Item 2 line 1
ID2 Trailer
ID3 Header
ID3 Item 3 line 1
ID3 Item 3 line 2
ID3 Item 3 line 3
ID3 Trailer

Does that make sense?
OBP's Avatar
OBP OBP is offline
Computer Specs
Distinguished Member with 14,665 posts.
 
Join Date: Mar 2005
Location: UK
Experience: An old Basic Programmer
11-Nov-2009, 12:14 PM #8
You obviously know how to use Variables as you used one for the File number.
So you could do something like this
Dim currentID as String
currentID = Format(ID, "000000000")
'add this after your line ID = rst!ID
Now try printing that currentID to the file between each of the Loops,
so that you should end up with 3 of them. That should show you where it needs to go.
Once you have that you can just set the other Items that you want to print to their own variables.
__________________
OBP
I do not give up easily
Hockeyball's Avatar
Junior Member with 5 posts.
 
Join Date: Nov 2009
Experience: Intermediate
11-Nov-2009, 01:05 PM #9
That got it.
Set the Variable and printed it after the second loop and it is working perfectly.
Thanks for your help!
OBP's Avatar
OBP OBP is offline
Computer Specs
Distinguished Member with 14,665 posts.
 
Join Date: Mar 2005
Location: UK
Experience: An old Basic Programmer
11-Nov-2009, 01:44 PM #10
Great job.
Reply

Tags
access, loop, module, vba

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 09:40 PM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.