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
Solved

Issue with  MyConn VB6 to access dbase

Posted on 2010-11-28
10
443 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
ID: 34226275
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
ID: 34226286
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
ID: 34226307
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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 11

Author Comment

by:Wilder1626
ID: 34226321
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
 
LVL 11

Author Comment

by:Wilder1626
ID: 34226443
Hello all

This topic was closed by mistake.

Can you please re open?

Thanks
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 34226444
Hello all

This topic was closed by mistake.

Can you please re open?

Thanks
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 34226454
Something is wrong here,

I accept and it closed the topic
0
 
LVL 11

Author Closing Comment

by:Wilder1626
ID: 34226644
Is it working now?
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 34226709
lol vee_mod
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
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…

860 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