Dustin Stanley
asked on
MS Access My Loop Code Returns Record 1 Several Times Instead Of 4 Individual Records Why?
I have a Query called "Qry_EbayAddFixedPriceItem 1" in that query it has 4 records. With the loop code I am using it appends the exact same values from record 1 only....But it does it 4 times.
I need it to append the values for record 1 then move to record 2 and etc......
What is wrong here?
I need it to append the values for record 1 then move to record 2 and etc......
What is wrong here?
Dim Manufacture As String
Dim MPN As String
Dim UPC As String
Manufacture = DLookup("Manufacture", "Qry_EbayAddFixedPriceItem1", "")
MPN = DLookup("MPN", "Qry_EbayAddFixedPriceItem1", "")
UPC = DLookup("UPC", "Qry_EbayAddFixedPriceItem1", "")
XmlString = "<PrivateListing>1</PrivateListing>" & vbNewLine
XmlString = XmlString & "<ProductListingDetails> ProductListingDetailsType" & vbNewLine
XmlString = XmlString & "<BrandMPN>" & vbNewLine
XmlString = XmlString & "<Brand>" & Manufacture & "</Brand>" & vbNewLine ' Manufacture
XmlString = XmlString & "<MPN>" & MPN & "</MPN>" & vbNewLine ' MPN
XmlString = XmlString & "</BrandMPN>" & vbNewLine
XmlString = XmlString & "<UPC>" & UPC & "</UPC>" & vbNewLine ' UPC
'******************************************************************************************************
'Loop Code
'*****************************************************************************************************
Dim sFile As String
Dim sText As String
Dim FileNumber As Integer
Dim rst As DAO.Recordset
sFile = "C:\Users\Station\Documents\Access XML Save Files\ChilKat\Append.xml"
sText = XmlString
FileNumber = FreeFile ' Get unused file number
Open sFile For Append As #FileNumber ' Connect to the file
Set rst = CurrentDb.OpenRecordset("Qry_EbayAddFixedPriceItem1", dbOpenSnapshot)
Do While Not rst.EOF
Print #FileNumber, sText ' Append our string
rst.MoveNext
Loop
rst.Close
Set rst = Nothing
Close #FileNumber ' Close the file
ASKER
Right now it is giving me this: (All record 1)
But I need this: (Record 1-4)
<PrivateListing>1</PrivateListing>
<ProductListingDetails> ProductListingDetailsType
<BrandMPN>
<Brand>TOSHIBA</Brand>
<MPN>LAPTOP123</MPN>
</BrandMPN>
<UPC>786958125896</UPC>
<PrivateListing>1</PrivateListing>
<ProductListingDetails> ProductListingDetailsType
<BrandMPN>
<Brand>TOSHIBA</Brand>
<MPN>LAPTOP123</MPN>
</BrandMPN>
<UPC>786958125896</UPC>
<PrivateListing>1</PrivateListing>
<ProductListingDetails> ProductListingDetailsType
<BrandMPN>
<Brand>TOSHIBA</Brand>
<MPN>LAPTOP123</MPN>
</BrandMPN>
<UPC>786958125896</UPC>
<PrivateListing>1</PrivateListing>
<ProductListingDetails> ProductListingDetailsType
<BrandMPN>
<Brand>TOSHIBA</Brand>
<MPN>LAPTOP123</MPN>
</BrandMPN>
<UPC>786958125896</UPC>
But I need this: (Record 1-4)
<PrivateListing>1</PrivateListing>
<ProductListingDetails> ProductListingDetailsType
<BrandMPN>
<Brand>TOSHIBA</Brand>
<MPN>LAPTOP123</MPN>
</BrandMPN>
<UPC>786958125896</UPC>
<PrivateListing>1</PrivateListing>
<ProductListingDetails> ProductListingDetailsType
<BrandMPN>
<Brand>GE</Brand>
<MPN>Fridge457</MPN>
</BrandMPN>
<UPC>786933325896</UPC>
<PrivateListing>1</PrivateListing>
<ProductListingDetails> ProductListingDetailsType
<BrandMPN>
<Brand>Johnson</Brand>
<MPN>LP90HJ</MPN>
</BrandMPN>
<UPC>787628125896</UPC>
<PrivateListing>1</PrivateListing>
<ProductListingDetails> ProductListingDetailsType
<BrandMPN>
<Brand>Jerry's</Brand>
<MPN>H77Red</MPN>
</BrandMPN>
<UPC>786958128965</UPC>
Here is the whole code if it is easier for you:
Dim Manufacture As String
Dim MPN As String
Dim UPC As String
'******************************************************************************************************
'Loop Code
'*****************************************************************************************************
Dim sFile As String
Dim sText As String
Dim FileNumber As Integer
Dim rst As DAO.Recordset
sFile = "C:\Users\Station\Documents\Access XML Save Files\ChilKat\Append.xml"
sText = XmlString
FileNumber = FreeFile ' Get unused file number
Open sFile For Append As #FileNumber ' Connect to the file
Set rst = CurrentDb.OpenRecordset("Qry_EbayAddFixedPriceItem1", dbOpenSnapshot)
Do While Not rst.EOF
Manufacture = rst("Manufacture")
MPN = rst("MPN")
UPC = rst("UPC")
XmlString = "<PrivateListing>1</PrivateListing>" & vbNewLine
XmlString = XmlString & "<ProductListingDetails> ProductListingDetailsType" & vbNewLine
XmlString = XmlString & "<BrandMPN>" & vbNewLine
XmlString = XmlString & "<Brand>" & Manufacture & "</Brand>" & vbNewLine ' Manufacture
XmlString = XmlString & "<MPN>" & MPN & "</MPN>" & vbNewLine ' MPN
XmlString = XmlString & "</BrandMPN>" & vbNewLine
XmlString = XmlString & "<UPC>" & UPC & "</UPC>" & vbNewLine ' UPC
Print #FileNumber, sText ' Append our string
rst.MoveNext
Loop
rst.Close
Set rst = Nothing
Close #FileNumber ' Close the file
ASKER
That is not working. It is the same effect....Thanks though.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you that works Great. I am sorry but what is it that wasn't right????
Was it that my XmlString wasn't inside the loop???
Was it that my XmlString wasn't inside the loop???
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
And I assume you did not copy my code verbatim,
I did but then i replaced sText with XmlString and it gave the same effects.
The darn Dlookups....I know they are not the best choice but I didn't know that. I knew there was other ways but was unsure.
So just to confirm the real problem here was the Dlookups and once they were replaced with
Manufacture = rst("Manufacture")
MPN = rst("MPN")
UPC = rst("UPC")
then all is good? Nothing else.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Awesome! Thanks a ton. I see now what you are sayng!
First you need to write the beginning of the xml file:
Open in new window
Then, in the loop you need to initialise the inner body of the XML:Open in new window
And then you need to add the remaining part and close the file:
Open in new window