Link to home
Start Free TrialLog in
Avatar of Dustin Stanley
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_EbayAddFixedPriceItem1" 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?

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

Open in new window

Avatar of chaau
chaau
Flag of Australia image

you need to reinitialise the xmlstring for each line in the loop.
First you need to write the beginning of the xml file:
XmlString = "<PrivateListing>1</PrivateListing>" & vbNewLine
    XmlString = XmlString & "<ProductListingDetails> ProductListingDetailsType" & vbNewLine
Print #FileNumber, XmlString

Open in new window

Then, in the loop you need to initialise the inner body of the XML:
Do While Not rst.EOF
     
Manufacture = rst("Manufacture")
MPN = rst("MPN")


      XmlString = "<BrandMPN>" & vbNewLine
        XmlString = XmlString & "<Brand>" & Manufacture & "</Brand>" & vbNewLine ' Manufacture
        XmlString = XmlString & "<MPN>" & MPN & "</MPN>" & vbNewLine ' MPN
      XmlString = XmlString & "</BrandMPN>" & vbNewLine     
    Print #FileNumber, XmlString ' Append our string
     
    rst.MoveNext
Loop
rst.Close
Set rst = Nothing

Open in new window


And then you need to add the remaining part and close the file:
    XmlString = "</ProductListingDetails> " & vbNewLine
    Print #FileNumber, XmlString ' Append our string
Close #FileNumber                       ' Close the file

Open in new window

Avatar of Dustin Stanley
Dustin Stanley

ASKER

Right now it is giving me this: (All record 1)
<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>

Open in new window



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>

Open in new window

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

Open in new window

That is not working. It is the same effect....Thanks though.
ASKER CERTIFIED SOLUTION
Avatar of chaau
chaau
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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???
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Awesome! Thanks a ton. I see now what you are sayng!