Results 1 to 11 of 11

Thread: [RESOLVED] Help with recordset result!

  1. #1
    Hyperactive Member doctrin13th's Avatar
    Join Date
    Sep 08
    Posts
    265

    Resolved [RESOLVED] Help with recordset result!

    I have 3 table

    1. cat_Tbl = category table which consist of fields:
    catNo = category number (primary key)
    catDesc = category description

    2. prod_Tbl = product table which consist of fields:
    pIndex = index number of a product (primary key)
    pText = product description

    3 FM_Tbl = items ordered(in an order slip) table which consist of fields:
    OSno = Order slip number
    FMitemNo = item number
    Pindex = index number of an ordered product
    FMqty = quantity ordered

    Now, I have an order slip record #12704. in that slip, i ordered several items, each has its unique item number. but when I run the code below, the result set exceeds the number of items i actually ordered. when I check the result, few items were duplicated. this is my code:

    Code:
    strSQL = "SELECT cat_Tbl.catDesc,FM_Tbl.FMitemNo,FM_Tbl.Pindex,FM_Tbl.FMqty,prod_Tbl.Ptext,prod_Tbl.Pum,prod_Tbl.Pprice FROM cat_Tbl,FM_Tbl,prod_Tbl WHERE (FM_Tbl.OSno = " & lngNum & ") AND FM_Tbl.Pindex=prod_Tbl.Pindex AND cat_Tbl.catNo=prod_Tbl.Pcat ORDER BY FM_Tbl.FMitemNo"
        
        'clear order table
        grdOrder.Rows = 1
        
        rsMain.Open strSQL, cnxMain, adOpenForwardOnly, adLockReadOnly, adCmdText
        Do Until rsMain.EOF
            With grdOrder
                .AddItem ""
                .TextMatrix(.Rows - 1, 0) = rsMain!FMitemNo 'Item no.
                .TextMatrix(.Rows - 1, 1) = rsMain!catDesc 'Category
                .TextMatrix(.Rows - 1, 2) = rsMain!Ptext 'Description
                .TextMatrix(.Rows - 1, 3) = rsMain!FMqty    'Qty
                .TextMatrix(.Rows - 1, 4) = rsMain!Pum 'Unit of measurement
                .TextMatrix(.Rows - 1, 5) = rsMain!Pprice 'Unit Price'Total
                .TextMatrix(.Rows - 1, 6) = rsMain!Pindex   'Product id
            End With
            rsMain.MoveNext
        Loop
        rsMain.Close
    Is there a problem with the strSQL? What's wrong with my code?

    What I want is to display just what I ordered; exact number of items, exact items. and just referencing to other tables its category and product description.

  2. #2
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 00
    Location
    off others' brains
    Posts
    4,329

    Re: Help with recordset result!

    I can't run the code (have moved far away from VB6), but try and use JOINs explicitly. Perform an INNER JOIN on the tables and then add the WHERE clause to it.

    Please post your results here after you have tried it.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  3. #3
    Hyperactive Member doctrin13th's Avatar
    Join Date
    Sep 08
    Posts
    265

    Re: Help with recordset result!

    Can you compose the sql statement based on my code? Please... Thnx

  4. #4
    Hyperactive Member doctrin13th's Avatar
    Join Date
    Sep 08
    Posts
    265

    Re: Help with recordset result!

    Anyone??

  5. #5
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 00
    Location
    off others' brains
    Posts
    4,329

    Re: Help with recordset result!

    Quote Originally Posted by doctrin13th View Post
    I have 3 table

    1. cat_Tbl = category table which consist of fields:
    catNo = category number (primary key)
    catDesc = category description

    2. prod_Tbl = product table which consist of fields:
    pIndex = index number of a product (primary key)
    pText = product description

    3 FM_Tbl = items ordered(in an order slip) table which consist of fields:
    OSno = Order slip number
    FMitemNo = item number
    Pindex = index number of an ordered product
    FMqty = quantity ordered
    My apologies. I should have mentioned this earlier itself. The three tables you have mentioned should be related to each other. While the FM_Tbl and prod_Tbl are linked through the Pindex, there is no link between the prod_Tbl and the cat_Tbl (at least I don't see one in your post above). Ideally the prod_Tbl should have another column catNo which is a foreign key to the catNo (PK) column of your cat_Tbl table.

    Quote Originally Posted by doctrin13th View Post
    Now, I have an order slip record #12704. in that slip, i ordered several items, each has its unique item number. but when I run the code below, the result set exceeds the number of items i actually ordered. when I check the result, few items were duplicated. this is my code:

    Code:
    strSQL = "SELECT cat_Tbl.catDesc,FM_Tbl.FMitemNo,FM_Tbl.Pindex,FM_Tbl.FMqty,prod_Tbl.Ptext,prod_Tbl.Pum,prod_Tbl.Pprice 
    FROM cat_Tbl,FM_Tbl,prod_Tbl WHERE 
    (FM_Tbl.OSno = " & lngNum & ") AND FM_Tbl.Pindex=prod_Tbl.Pindex AND cat_Tbl.catNo=prod_Tbl.Pcat ORDER BY FM_Tbl.FMitemNo"
        
        ...
    Is there a problem with the strSQL? What's wrong with my code?

    What I want is to display just what I ordered; exact number of items, exact items. and just referencing to other tables its category and product description.
    The query would then look something like this:
    Code:
    strSQL = "SELECT cat_Tbl.catDesc,FM_Tbl.FMitemNo,FM_Tbl.Pindex,FM_Tbl.FMqty,prod_Tbl.Ptext,prod_Tbl.Pum,prod_Tbl.Pprice 
    FROM FM_Tbl inner join prod_Tbl On FM_Tbl.Pindex = prod_Tbl.pIndex INNER JOIN cat_Tbl on prod_Tbl.catNo = cat_Tbl.catNo 
    WHERE (FM_Tbl.OSno = " & lngNum & ")"
        
        ...
    .
    Last edited by honeybee; Jul 21st, 2012 at 04:35 AM.
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  6. #6
    New Member
    Join Date
    Jul 12
    Posts
    1

    Re: Help with recordset result!

    SELECT cat_Tbl.catDesc,FM_Tbl.FMitemNo,FM_Tbl.Pindex,FM_Tbl.FMqty,prod_Tbl.Ptext,prod_Tbl.Pum,prod_Tbl.Ppri ce FROM cat_Tbl,FM_Tbl,prod_Tbl WHERE (FM_Tbl.OSno = " & lngNum & ") AND FM_Tbl.Pindex=prod_Tbl.Pindex AND cat_Tbl.catNo=prod_Tbl.Pcat ORDER BY FM_Tbl.FMitemNo

    try using this code in an ms access query and see if it works. if yes then check the codes for showing the data in the grid. If it does not produce the records then check the records. They ma have been duplicated during the adding process.

    Or another reason for the dublication is the relation ships. The data is being fetched from different tables but you have not specified any join or anything to indicate to vb6 whether there is any relationship among the records from the tables.

  7. #7
    Hyperactive Member doctrin13th's Avatar
    Join Date
    Sep 08
    Posts
    265

    Re: Help with recordset result!

    Quote Originally Posted by honeybee View Post
    My apologies. I should have mentioned this earlier itself. The three tables you have mentioned should be related to each other. While the FM_Tbl and prod_Tbl are linked through the Pindex, there is no link between the prod_Tbl and the cat_Tbl (at least I don't see one in your post above). Ideally the prod_Tbl should have another column catNo which is a foreign key to the catNo (PK) column of your cat_Tbl table.



    The query would then look something like this:
    Code:
    strSQL = "SELECT cat_Tbl.catDesc,FM_Tbl.FMitemNo,FM_Tbl.Pindex,FM_Tbl.FMqty,prod_Tbl.Ptext,prod_Tbl.Pum,prod_Tbl.Pprice 
    FROM FM_Tbl inner join prod_Tbl On FM_Tbl.Pindex = prod_Tbl.pIndex INNER JOIN cat_Tbl on prod_Tbl.catNo = cat_Tbl.catNo 
    WHERE (FM_Tbl.OSno = " & lngNum & ")"
        
        ...
    .

    My apology; I didn't include prod_Tbl.Pcat (the category number of a product) in my above listing. But it is included in the query, however.

    Do the keys have to be identical in name in different tables to have a relationship(i.e. Pcat in prod_Tbl, catNo in cat_Tbl)?

    I tried this query:
    Code:
    SELECT cat_Tbl.catDesc,FM_Tbl.FMitemNo,FM_Tbl.Pindex,FM_Tbl.FMqty,prod_Tbl.Ptext,prod_Tbl.Pum,prod_Tbl.Pprice FROM FM_Tbl INNER JOIN prod_Tbl ON prod_Tbl.Pindex=FM_Tbl.Pindex INNER JOIN cat_Tbl ON cat_Tbl.catNo=prod_Tbl.Pcat WHERE (FM_Tbl.OSno = " & lngNum & ") ORDER BY FM_Tbl.FMitemNo
    and this error appears:
    Code:
    Error No. -2147217900
    Syntax error (missing operator) in query expression 'prod_Tbl.Pindex=FM_Tbl.Pindex INNER JOIN cat_Tbl ON cat_Tbl.catNo=prod_Tbl.Pcat'.
    What could cause the problem?
    Last edited by doctrin13th; Jul 22nd, 2012 at 11:14 PM. Reason: additional info

  8. #8
    PowerPoster
    Join Date
    Feb 12
    Location
    West Virginia
    Posts
    4,954

    Re: Help with recordset result!

    I've never tried this but I would think the problem is that you have the inner join in there twice.

    http://msdn.microsoft.com/en-us/libr...v=sql.80).aspx

  9. #9
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 00
    Location
    off others' brains
    Posts
    4,329

    Re: Help with recordset result!

    @doctrin13th, I suggest you compose the sql query in the database that you are using, so the query gives you the desired results, and then incorporate the query in your code. This will be much faster both in terms of development time and in terms of debugging.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  10. #10
    Hyperactive Member doctrin13th's Avatar
    Join Date
    Sep 08
    Posts
    265

    Re: Help with recordset result!

    My apologies to all of you. I think I've wasted your time unintentionally, helping me with my problem.
    I found the culprit!
    I manually dig through my database and found out that there are accounts with duplicate OS number (OSno field). Upon changing the duplicate OS number, the desired output is obtained, finally, without error or any problem.
    I appreciate all your help very much. I'm very grateful for your efforts and I'm very sorry if I've been stupid not trying enough ways before asking.

    Again, thank you and I apologize!
    Last edited by doctrin13th; Jul 25th, 2012 at 09:29 PM.

  11. #11
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 00
    Location
    off others' brains
    Posts
    4,329

    Re: [RESOLVED] Help with recordset result!

    Oh, no need to apologize, we are all here to help and we aren't perfect either.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •