Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB Databases Basics
|
VB Databases Basics Beginning-level VB coding questions specific to using VB with databases. Issues not specific to database use will be redirected to other forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB Databases Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 14th, 2008, 12:58 AM
Authorized User
 
Join Date: Aug 2007
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to zrtv
Default Incorrect Record updating in access

Hi All,
I have a strange problem, past 6 month people are working in the software no problem till, but past some days incorrect data is updating one of the table.Below is the code.

-------------------------------------------------------------------
Private Sub mi()
On Error GoTo err_handler
Dim rates
rsdocmst.Open "select*from docmst where doctype='" & "INV" & "'", db, adOpenForwardOnly, adLockReadOnly
rsinvoicedetailmaterial.Open "invoicedetailmaterial", db, adOpenDynamic, adLockOptimistic

rsmi.Open "select*from materialissue where jobid='" & Trim(txtJobID.Text) & "'", db, adOpenForwardOnly, adLockReadOnly
Do Until rsmi.EOF
    rsinvoicedetailmaterial.AddNew
    rsinvoicedetailmaterial("make") = rsmi("make")
    rsinvoicedetailmaterial("sprnum") = rsmi("sprnum")
    rsitmmst.Open "Select*from itemmaster where sprnum='" & Trim(rsmi("sprnum")) & "' and make='" & Trim(rsmi("make")) & "' ", db, adOpenDynamic
    If rsitmmst.EOF = False Then
        rsinvoicedetailmaterial("sprname") = rsitmmst("sprname")
        rsinvoicedetailmaterial("uom") = rsitmmst("uom")
    End If
    rsitmmst.Close
    rsinvoicedetailmaterial("qty") = rsmi("miqty")
    rsinvoicedetailmaterial("sprrate") = Round(rsmi("rate"), 3)
    rsinvoicedetailmaterial("amount") = (Round(rsmi("rate") * rsmi("miqty"), 3))
    rsinvoicedetailmaterial("invno") = rsdocmst("lastnum") + 1
    rsinvoicedetailmaterial("invdate") = dtpInvdate.Value
    rsinvoicedetailmaterial("profitpercentage") = Val(txtProfit.Text)
    rsinvoicedetailmaterial("jobid") = Trim(txtJobID.Text)
    rsinvoicedetailmaterial("trdate") = Date
    rsinvoicedetailmaterial("userid") = frmmain.Label1.Caption
rsinvoicedetailmaterial.update
rsmi.MoveNext
Loop
rsmi.Close
rsinvoicedetailmaterial.Close
rsdocmst.Close
Exit Sub
err_handler:
If ERR.Number > 0 Then
   MsgBox ERR.Description, vbCritical
End If
End Sub
----------------------------------------------------------
The issue table have around 16000 records,
When give job Number query is fetching that job issues and updating in invoice table, but nowadays when we give one jobNumber query is fetching diffrent job issues

eg: If jobnumber = 4011
but query is fetching job number 2741

can someone help me.....

 
Old August 14th, 2008, 10:03 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

I really don't know how we can help you. The code looks ok, but if the error is a logic error, we will have a lot of problems trying to understand why your behind logic is bad.

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
 
Old August 14th, 2008, 03:00 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

This is completely unnecessary:
Code:
... where doctype='" & "INV" & "'", ...
Just use:
Code:
... where doctype = 'INV'", ...
This creates a Variant:
Code:
    Dim rates
You should specify the data type. Plus, it is never used in the code. Why don't you just delete it altogether?

Where is this number that is misbehaving? (I can't see where in the code that item is.)

You will find your code much easier to troubleshoot if you do a few things.
Capitalize all SQL keywords.
Use the bang operator when refering to recordset fields (rs!fld rather than rs("fld")).
Use a With / End With block when you are going to refer to the same object over and over. (In my adaptation, that requires using the bang operator, or using .Fields("fld"))

Put blank lines between logical groups of statements (when the task shifts).

Break up extremely long lines of code with the line-continuation character.

If you are only returning a field or 2, use a specific field list, rather than *.
I lined up your referencing of fields from shortest name to longest name. This makes it harder to accidentally overlook a field in the list. Shortest to longest name; then within each field-length grouping, shortest to longest overall statement; then alphabetical.

Your error handler could have left recordsets open, so I added code to ensure that cannot happen.

Use the shortest name for objects that will allow recognizing what they are, and use capitalization. This rsinvoicedetailmaterial is hard to read, and unnecessary.
rsInvoiceDetailMaterial is better.
rsInvoiceDtlMtl is better.
rsInvDtlMtl even better. Just as explicit, much, much easier to recognize when coming back to modify the code.

Use indentation for blocks of code [u]every time</u>. Your life will be [u]so</u> much easier, as will the lives of anyone who has to work on what you wrote.

I have not noticed anything wrong here, so I await your answer to my question, above.
Code:
Private Sub mi()
    On Error GoTo Er

    rsDocMst.Open "SELECT * FROM docmst WHERE doctype='INV'", db, _
                  adOpenForwardOnly, adLockReadOnly

    rsInvoiceDtlMtl.Open "invoicedetailmaterial", db, _
                         adOpenDynamic, adLockOptimistic

    rsMI.Open "SELECT * FROM MaterialIssue " & _
              "WHERE  jobid = '" & Trim(txtJobID.Text) & "'", db, _
              adOpenForwardOnly, adLockReadOnly

    Do Until rsMI.EOF
        With rsInvoiceDtlMtl
            rsItmMst.Open "SELECT sprname, uom " & _
                          "FROM   itemmaster   " & _
                          "WHERE  sprnum = '   " & Trim(rsMI!sprnum) & "' " & _
                          "  AND  make   = '   " & Trim(rsMI!make) & "'", _
                          db, _
                          adOpenDynamic

            .AddNew

            If rsItmMst.EOF = False Then
                !sprname = rsItmMst!sprname
                !uom = rsItmMst!uom
            End If
            rsItmMst.Close

            !qty = rsMI!miqty
            !make = rsMI!make
            !jobid = Trim(txtJobID.Text)
            !invno = rsDocMst!lastnum + 1
            !trdate = Date
            !sprnum = rsMI!sprnum
            !userid = frmmain.Label1.Caption
            !amount = Round(rsMI!rate * rsMI!miqty, 3)
            !invdate = dtpInvdate.Value
            !sprrate = Round(rsMI!rate, 3)
            !profitpercentage = Val(txtProfit.Text)

            .Update
        End With

        rsMI.MoveNext
    Loop

    rsMI.Close
    rsDocMst.Close
    rsInvoiceDtlMtl.Close

Res: Exit Sub

Er: MsgBox Err.Number & ", """ & Err.Description & """", vbCritical

    If rsMI.State <> adStateClosed Then rsMI.Close
    If rsItmMst.State <> adStateClosed Then rsItmMst.Close
    If rsDocMst.State <> adStateClosed Then rsDocMst.Close
    If rsInvoiceDtlMtl.State <> adStateClosed Then rsInvoiceDtlMtl.Close

    Resume Res

End Sub
 
Old August 15th, 2008, 08:39 AM
Authorized User
 
Join Date: Aug 2007
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to zrtv
Default

Thanks a lot Mr.BrianWren for that detailed reply,
actually the number field I mentioned before is jobID, but in the database the data type of the jobID is string, because sometime the field uses string charecters too.

Private Sub mi()
    On Error GoTo Er

    rsDocMst.Open "SELECT * FROM docmst WHERE doctype='INV'", db, _
                  adOpenForwardOnly, adLockReadOnly

    rsInvoiceDtlMtl.Open "invoicedetailmaterial", db, _
                         adOpenDynamic, adLockOptimistic

    rsMI.Open "SELECT * FROM MaterialIssue " & _
         "WHERE [u]jobid = '" & Trim(txtJobID.Text) & "'", </u>db, _
              adOpenForwardOnly, adLockReadOnly

    Do Until rsMI.EOF
        With rsInvoiceDtlMtl
            rsItmMst.Open "SELECT sprname, uom " & _
               "FROM itemmaster " & _
               "WHERE sprnum = ' " & Trim(rsMI!sprnum) & "' " & _
               " AND make = ' " & Trim(rsMI!make) & "'", _
               db, _
               adOpenDynamic

            .AddNew

            If rsItmMst.EOF = False Then
                !sprname = rsItmMst!sprname
                !uom = rsItmMst!uom
            End If
            rsItmMst.Close

            !qty = rsMI!miqty
            !make = rsMI!make
            !jobid = Trim(txtJobID.Text)
            !invno = rsDocMst!lastnum + 1
            !trdate = Date
            !sprnum = rsMI!sprnum
            !userid = frmmain.Label1.Caption
            !amount = Round(rsMI!rate * rsMI!miqty, 3)
            !invdate = dtpInvdate.Value
            !sprrate = Round(rsMI!rate, 3)
            !profitpercentage = Val(txtProfit.Text)
            .Update
        End With
        rsMI.MoveNext
    Loop

    rsMI.Close
    rsDocMst.Close
    rsInvoiceDtlMtl.Close

Res: Exit Sub

Er: MsgBox Err.Number & ", """ & Err.Description & """", vbCritical

    If rsMI.State <> adStateClosed Then rsMI.Close
    If rsItmMst.State <> adStateClosed Then rsItmMst.Close
    If rsDocMst.State <> adStateClosed Then rsDocMst.Close
    If rsInvoiceDtlMtl.State <> adStateClosed Then rsInvoiceDtlMtl.Close

    Resume Res

End Sub


In the above code, the underlined session fetching incorrect record from the table,
For Eg: In the textfield if I am giving the jobID =4000
 then the query is fetching the record of jobID=2700 also,

after the updation, when I check the invoice table I can see that,it updates the details of jobID 4000 and 2700 in the invoice table.
is 16000 data in that(Issue) table makes any problem?

Once again thanks for your time and help.

 
Old August 15th, 2008, 09:26 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Is that field limited to 4 characters in the database structure? The contents of the textbox should't be changing during a run, and ought to be able to hold hundreds of characters...

BTW: In these WROX fora, if you precede a piece of text with the following (minus the spaces): [ c o d e ], and finish with the following (minus the spaces): [ / c o d e ]everything between those two tags will be fixed-spaced text, and will show in your post literally exactly as you type it. (The tags will not show in your post.)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Updating database record shrisangeeta Classic ASP Basics 4 February 28th, 2007 05:25 PM
Updating the record in database lwebzem ASP.NET 1.0 and 1.1 Basics 4 March 8th, 2006 09:40 AM
Updating first found record rtr1900 Classic ASP Databases 9 December 2nd, 2005 03:09 AM
updating record stoneman Access 1 July 5th, 2005 09:12 AM
Record not updating Tangerine ASP.NET 1.x and 2.0 Application Design 3 March 24th, 2004 12:00 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.