Solved

Issue with  MyConn VB6 to access dbase

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

In my first code , everything was ok to find if the value in txtNomemploye1.Text was in my access dbase.

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


But now, i need to look if txtNomemploye1 and txtPrenomEmploye exist.


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 & "'" _
    & "Prénom ='" & txtPrenomEmploye.Text & "'" _
    & "MyConn, adOpenForwardOnly, adLockReadOnly &")


If it exist, it will say:

 Else
        MyRecSet1.close
        MsgBox "Existe déjà dans la base de données", vbCritical, "Failure adding"
End if


Now i have a run time error 3709

Thanks for your help.

If (a = 6) Then
        ' il a cliqué sur OUI
    'Insert data in Microsoft Access Data Base
    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 & "'" _
    & "Prénom ='" & txtPrenomEmploye.Text & "'" _
    & "MyConn, adOpenForwardOnly, adLockReadOnly &")


    If MyRecSet1.EOF Then
        MyRecSet1.close
        Set MyRecSet1 = MyConn.Execute( _
          "INSERT INTO [Employés]  (Nom, Prénom, Adresse, Ville, Code_postal, Numéro_de_Téléphone, Cellulaire, Courriel, Jour, Mois, Année, Numéro_assurance_social) VALUES ( '" & _
          txtNomemploye1.Text & _
          "' , '" & txtPrenomEmploye.Text & "' ,  '" & txtAdresse.Text & "' ,'" & txtVille.Text & "' ,'" & txtCodePostal.Text & "' ,'" & txtNumeroDe.Text & "' , '" & txtCellulaire.Text & _
          "' , '" & txtCourriel.Text & "' , '" & ComJour.Text & "' , '" & ComMois.Text & "' ,'" & ComAnnée.Text & _
          "' , '" & txtAssuranceSocial.Text & "' )")
        MsgBox "Nouvelle employé enregistré"  ', vbCritical, "Succesful adding"
    Else
        MyRecSet1.close
        MsgBox "Existe déjà dans la base de données", vbCritical, "Failure adding"
    End If

    MyConn.close

Open in new window

0
Comment
Question by:Wilder1626
  • 6
  • 3
10 Comments
 
LVL 65

Expert Comment

by:rockiroads
Comment Utility
perhaps adding the AND statement would help here
syntax of multiple checks is like this

WHERE ...
AND ...

there is also

OR


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 & "'  AND " _
    & "Prénom ='" & txtPrenomEmploye.Text & "'" _
    & "MyConn, adOpenForwardOnly, adLockReadOnly &")
0
 
LVL 11

Author Comment

by:Wilder1626
Comment Utility
Hello rockiroads,

I tried this:

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 & "' AND " _
    & "Prénom ='" & txtPrenomEmploye.Text & "'" _
    & "MyConn, adOpenForwardOnly, adLockReadOnly &")

But still the same error.
0
 
LVL 65

Accepted Solution

by:
rockiroads earned 500 total points
Comment Utility
oh, I just noticed MyConn on the end of your string. You are appending MyConnn rather than adding as an argument. Looks like copy/paste error
as well as no space after the WHERE

good practice is to define your sql in a string then use that. you will find it easier to debug
eg


Dim sSql As String

Set MyConn = New ADODB.Connection

    MyConn.Open "Provider=Microsoft.ACE.OLEDB.12.0;;Data Source=" & Form4.txtBaseDe.Text

    sSql = "SELECT * FROM Employés WHERE " _
            & "Nom ='" & txtNomemploye1.Text & "' AND " _
            & "Prénom ='" & txtPrenomEmploye.Text & "'"

    MyRecSet1.Open sSql, MyConn, adOpenForwardOnly, adLockReadOnly
    On Error GoTo cmdExportToExcel_Click_Error
0
 
LVL 11

Author Comment

by:Wilder1626
Comment Utility
Just this and now it work

Dim sSql As String

Set MyConn = New ADODB.Connection

    MyConn.Open "Provider=Microsoft.ACE.OLEDB.12.0;;Data Source=" & Form4.txtBaseDe.Text

    sSql = "SELECT * FROM Employés WHERE " _
            & "Nom ='" & txtNomemploye1.Text & "' AND " _
            & "Prénom ='" & txtPrenomEmploye.Text & "'"

    MyRecSet1.Open sSql, MyConn, adOpenForwardOnly, adLockReadOnly
 

I just gona some test
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 11

Author Comment

by:Wilder1626
Comment Utility
Hello all

This topic was closed by mistake.

Can you please re open?

Thanks
0
 
LVL 11

Author Comment

by:Wilder1626
Comment Utility
Hello all

This topic was closed by mistake.

Can you please re open?

Thanks
0
 
LVL 11

Author Comment

by:Wilder1626
Comment Utility
Something is wrong here,

I accept and it closed the topic
0
 
LVL 11

Author Closing Comment

by:Wilder1626
Comment Utility
Is it working now?
0
 
LVL 65

Expert Comment

by:rockiroads
Comment Utility
lol vee_mod
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 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

16 Experts available now in Live!

Get 1:1 Help Now