Solved

Update in Access dbase from VB6

Posted on 2010-11-28
14
187 Views
Last Modified: 2012-05-10
Hello all,

I'm trying to update a few data and i have an error.

I want to update "adress and Ville" field.

How can i fix this please?

Thanks
If (a = 6) Then
        Dim MyConn As ADODB.Connection
    Dim MyRecSet1 As New ADODB.Recordset

    Set MyConn = New ADODB.Connection
    MyConn.Open "Provider=Microsoft.ACE.OLEDB.12.0;;Data Source=" & Form4.txtBaseDe.Text
    MyRecSet1.Open "SELECT * FROM [Employés] WHERE Nom='" & txtNomemploye1.Text & "'", MyConn, adOpenForwardOnly, adLockReadOnly

  
    If MyRecSet1.EOF = False Then
        MyConn.Execute ( _
            "Update [Employés] SET Adresse= '" & txtAdresse.Text & "' AND " _
            & "Ville ='" & txtVille.Text & "'" _
            & Where _
            & "Nom ='" & txtNomemploye1.Text & "' AND " _
            & "Prénom ='" & txtPrenomEmploye.Text & "'")

Open in new window

0
Comment
Question by:Wilder1626
  • 7
  • 7
14 Comments
 
LVL 8

Expert Comment

by:Den_HBR
ID: 34226581
What error do you get?
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 34226589
This is the error
error.jpg
0
 
LVL 8

Expert Comment

by:Den_HBR
ID: 34226595
replace

& Where _

with

& " Where " _
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 34226610
That is good, no more error.

But it does not change anything also.

Altually, it change "Adresse" with a"0" by default and "Ville" stay the same.
0
 
LVL 8

Accepted Solution

by:
Den_HBR earned 500 total points
ID: 34226627
replace

"Update [Employés] SET Adresse= '" & txtAdresse.Text & "' AND " _
            & "Ville ='" & txtVille.Text & "'" _

with

"Update [Employés] SET Adresse= '" & txtAdresse.Text & "' ," _
            & "Ville ='" & txtVille.Text & "'" _
0
 
LVL 8

Expert Comment

by:Den_HBR
ID: 34226635
and just a hint...
It will not work if there is an ' in 1 off you textboxes.
Better use

replace(txtAdresse.Text,"'","''")
replace(txtVille.Text,"'","''")
replace(txtNomemploye1.Text,"'","''")
replace(txtPrenomEmploye.Text,"'","''")
0
 
LVL 11

Author Closing Comment

by:Wilder1626
ID: 34226636
Thanks for your help. This is great
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 11

Author Comment

by:Wilder1626
ID: 34226642
What do you mean by:

replace(txtAdresse.Text,"'","''")
replace(txtVille.Text,"'","''")
replace(txtNomemploye1.Text,"'","''")
replace(txtPrenomEmploye.Text,"'","''")
0
 
LVL 8

Expert Comment

by:Den_HBR
ID: 34226647
replace a single quote with 2 single quotes.

Trust me!
Try to use "test'test" as your ville and then try to use "test''test" as your ville
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 34226669
So i need to do this?

 "Update [Employés] SET Adresse= '" & txtAdresse.Text & "' , & "' ," _
0
 
LVL 8

Expert Comment

by:Den_HBR
ID: 34226699
No.

MyConn.Execute ( _
            "Update [Employés] SET Adresse= '" & replace(txtAdresse.Text,"'","''") & "' ," _
            & "Ville ='" & replace(txtVille.Text,"'","''") & "'" _
            & Where _
            & "Nom ='" & replace(txtNomemploye1.Text,"'","''") & "' AND " _
            & "Prénom ='" & replace(txtPrenomEmploye.Text,"'","''") & "'")
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 34226715
This is weird cause if i enter in the ville: St-Hubert, it is not working. But if i remove the -, it work.

Do you know why?
0
 
LVL 8

Expert Comment

by:Den_HBR
ID: 34226754
Change your code to


Dim strSQL as String

strSQL = " Update [Employés] "
strSQL = strSQL   & " SET "
strSQL  = strSQL  &  "Adresse= '" & replace(txtAdresse.Text,"'","''")  & "'"
strSQL  = strSQL  & " , "
strSQL  = strSQL  & " Ville ='" & replace(txtVille.Text,"'","''")  & "'"
strSQL  = strSQL  &  " Where "
strSQL  = strSQL  &  " Nom ='" & replace(txtNomemploye1.Text,"'","''") & "'"
strSQL  = strSQL  &  " AND "
strSQL  = strSQL  &  " Prénom ='" & replace(txtPrenomEmploye.Text,"'","''") & "'"

MyConn.Execute (strSQL)



set a breakpoint on MyConn.Execute (strSQL) and post the value of strSQL


0
 
LVL 11

Author Comment

by:Wilder1626
ID: 34226842
Oh perfect

I like that.

Thank you so much
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

863 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now