Solved

Update Query

Posted on 2010-09-23
12
568 Views
Last Modified: 2012-05-10

Experts

I am using following Query for Insert record,
Value getting from Array List ( valuelist)

The problem is if record exist then its giving me error cause of primary key in database.
so now how can i update record
using update query

insert query working fine
but for update i am little confuse how it will work and how to get value .....
What update query will be

If valuelist <> "" Then
''''''''INSERTING DATA FROM CSV FILE'''''
    con.Execute "insert into B_Detail (BtyNo,Mode,BDate,TkNo,Code,....etc 58 fields) values (" & valuelist & ")"
    Kill "D:\Crystal\" & BNo & ".csv"
'        valuelist = Empty
End If

Open in new window

0
Comment
Question by:crystalsoft
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 3
12 Comments
 
LVL 11

Expert Comment

by:rowansmith
ID: 33742667
You must first of all check to see if the record exists using a SELECT statement, you only need to count the number of records returned.

If the Count > 0 then you can do an Update to that record.

If the COUNT==0 then you do an insert.

-Rowan
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33742690
I should consider first checking if the value already exists or not (based on your primary key )
Do all the fields change in your update or just a few ?

If valuelist <> "" Then
   ' check if record already exist
   Dim myRecCount As New ADODB.Recordset
   Set myRecCount = con.Execute("select * from B_Detail where BtyNo = " & valueList(0))

   If myRecCount.EOF = true and myRecCount.BOF = true  Then
    ' it does not exist
        con.Execute "insert into B_Detail (BtyNo,Mode,BDate,TkNo,Code,....etc 58 fields) values (" & valuelist & ")"
   else
        con.Execute("update  B_Detail set Mode ='" & valuelist(1) & "' where BtyNo = " & valueList(0))
   end if
    Kill "D:\Crystal\" & BNo & ".csv"

End If
0
 
LVL 1

Author Comment

by:crystalsoft
ID: 33742851

Thanks for quick reply

rowansmith: >> Records already exists, that's why duplicate msg occur from DB.

Dhaest: >> Record already exist, i want to change all the fields where BtyNo = ' & BNo & '
valuelist have all the fields now for update query how can i assign values and fields

0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 1

Author Comment

by:crystalsoft
ID: 33742858

valuelist code is attached
For i = 0 To UBound(props)

If Not IsNull(props(i)) And props(i) <> "" Then

    If valuelist = "" Then
        If i = 0 Then
            BNo = props(i)
        End If
        valuelist = valuelist & "'" & props(i) & "'"
    Else
        
        If i = 0 Then
            BNo = props(i)
        End If
        
        valuelist = valuelist & ",'" & props(i) & "'"
        
    End If

End If

Next i

Open in new window

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33742947
I'm not sure if you can pass a valueList to an updatequery, like you did for the insert-query.
If that's not the case, you'll have to build the sql in code ....
0
 
LVL 1

Author Comment

by:crystalsoft
ID: 33743315

Dhaest: > You mean to say, i have to put all the 56 fields value in separate 56 variable and then write update query like..

con.Execute "UPDATE B_Detail set Check = " & BD_CHK & ", LsheetNo = " & BD_LSNo & ", Status_Rec = " & BD_Status & ", Ref_Gm_No = '" & BD_RefGmNo & "' where BiltyNo = " & BName3 & ""
0
 
LVL 11

Accepted Solution

by:
rowansmith earned 125 total points
ID: 33743351
You can not pass a comma separated list to UPDATE.  You need to do a SET statement for each and every field in the table.

You could also consider deleting the offending record and then inserting a new record as part of a transaction.

So:

con.BeginTrans();
con.Execute("delete from B_Detail where BtyNo = " & valueList(0);");
con.Execute("insert into B_Detail (BtyNo,Mode,BDate,TkNo,Code,....etc 58 fields) values (" & valuelist & ")";");
con.CommitTrans();

0
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 125 total points
ID: 33743378
>> You mean to say, i have to put all the 56 fields value in separate 56 variable and then write update query like..

Yes, and confirmed by rowansmith (who gave also a possible workaround)
0
 
LVL 1

Author Comment

by:crystalsoft
ID: 33743923

Both suggestion can work on my scenario,
Now let me test..

0
 
LVL 11

Expert Comment

by:rowansmith
ID: 33744022
Let us know how you get on.
0
 
LVL 1

Author Comment

by:crystalsoft
ID: 33744223

Its working perfectly,
Instead of use 56 variable, i will go for (rowansmith's Suggestion) delete record first and then insert it,

as usual -------- > TTHHAANNKKSS    (:-D)

0
 
LVL 1

Author Closing Comment

by:crystalsoft
ID: 33744336

Thanks
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question