Solved

How to Clear/Reset an ADODC recordset

Posted on 2003-10-31
33
2,242 Views
Last Modified: 2013-12-25
Hello,

I am having some difficulty here with an ADODC control on my VB6 form.  I have a couple of textboxex and I can perform a search on my database using some of these textboxes.  My First Search result is perfect.  If I have more than one record it returns all the found set and displays the ADODC data control and I can browse through the records that match my search criteria.  

Next I have another button that clears all textboxes on that form so that I can perform another search.  The problem arises there.  I enter my search criteria and then click on search.  The results have nothing to do with my search fields.  It always displays the found set from the first search when I originally launched the program.

Therefore I tried to do a ADODC.RECORDSET.CLOSE  This works for 2 searches, on the third search I get the error message, Cannot perform operation when object is closed.  

If I comment out the Recordset.close I get the following message after a while of doing new searches even if I get the wrong answers:

The changes you requested to the table were not successful because they would create duplicate values in the index, primary key or relationship...

I am not performing any changes or applying any changes.  I am just clicking on the ADODC Data Control buttons.

The Lock type is Optimistic.

I would like to post my code but I use a lot of modules and a lot of forms so I think that it might get messy.  However I am going to post how I bind and unbind the control to the ADODC data control.

I think that I just need to reset the recordset in the ADODC each time I perform a search.

Your help would be greatly appreciated.

Thanks,

Mark

*****************************

Public Sub ActivateControl()
   
    FormEmployee.AdodcEmployee.Visible = True
   
   
    With FormEmployee.AdodcEmployee
    .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=X:\Ipecac\Main.mdb;Persist Security Info=False;Jet OLEDB:Database Password=sesco"
    .RecordSource = SearchString
    End With
   
    Set FormEmployee.TextPrenom.DataSource = FormEmployee.AdodcEmployee
    FormEmployee.TextPrenom.DataField = "FirstName"
    Set FormEmployee.TextNom.DataSource = FormEmployee.AdodcEmployee
    FormEmployee.TextNom.DataField = "LastName"
    Set FormEmployee.TextAdresse.DataSource = FormEmployee.AdodcEmployee
    FormEmployee.TextAdresse.DataField = "Address"
    Set FormEmployee.TextVille.DataSource = FormEmployee.AdodcEmployee
    FormEmployee.TextVille.DataField = "Ville"
    Set FormEmployee.TextCodePostale.DataSource = FormEmployee.AdodcEmployee
    FormEmployee.TextCodePostale.DataField = "CodePostal"
    Set FormEmployee.TextDepartement.DataSource = FormEmployee.AdodcEmployee
    FormEmployee.TextDepartement.DataField = "Department"
    Set FormEmployee.TextDateEmbauche.DataSource = FormEmployee.AdodcEmployee
    FormEmployee.TextDateEmbauche.DataField = "DateEmployed"
    Set FormEmployee.TextNumeroEmploye.DataSource = FormEmployee.AdodcEmployee
    FormEmployee.TextNumeroEmploye.DataField = "EmployeeNumber"
    Set FormEmployee.TextSalaire.DataSource = FormEmployee.AdodcEmployee
    FormEmployee.TextSalaire.DataField = "Salary"
    Set FormEmployee.TextTelephone.DataSource = FormEmployee.AdodcEmployee
    FormEmployee.TextTelephone.DataField = "Telephone"
    Set FormEmployee.TextAutreNumero.DataSource = FormEmployee.AdodcEmployee
    FormEmployee.TextAutreNumero.DataField = "AutreNumero"
    Set FormEmployee.TextDateDeNaissance.DataSource = FormEmployee.AdodcEmployee
    FormEmployee.TextDateDeNaissance.DataField = "DOB"
    Set FormEmployee.TextEmail.DataSource = FormEmployee.AdodcEmployee
    FormEmployee.TextEmail.DataField = "Email"
    Set FormEmployee.TextAssuranceSociale.DataSource = FormEmployee.AdodcEmployee
    FormEmployee.TextAssuranceSociale.DataField = "SIN"
    Set FormEmployee.RichTextBoxNotes.DataSource = FormEmployee.AdodcEmployee
    FormEmployee.RichTextBoxNotes.DataField = "Notes"
    Set FormEmployee.CheckUtilisateur.DataSource = FormEmployee.AdodcEmployee
    FormEmployee.CheckUtilisateur.DataField = "Utilisateur"
   
    'Set FormEmployee.PicturePhoto.DataSource = FormEmployee.AdodcEmployee
    'FormEmployee.PicturePhoto.Picture = LoadPicture(Rs.Fields("Picture").Value)
    'LoadPicture(Rs.Fields("Picture").Value)
   
   
End Sub

Public Sub DeactivateControl()

    FormEmployee.AdodcEmployee.Visible = False
   
    Set FormEmployee.TextPrenom.DataSource = Nothing
   
    Set FormEmployee.TextNom.DataSource = Nothing
   
    Set FormEmployee.TextAdresse.DataSource = Nothing
   
    Set FormEmployee.TextVille.DataSource = Nothing
   
    Set FormEmployee.TextCodePostale.DataSource = Nothing
   
    Set FormEmployee.TextDepartement.DataSource = Nothing
   
    Set FormEmployee.TextDateEmbauche.DataSource = Nothing
   
    Set FormEmployee.TextNumeroEmploye.DataSource = Nothing
   
    Set FormEmployee.TextSalaire.DataSource = Nothing
   
    Set FormEmployee.TextTelephone.DataSource = Nothing
   
    Set FormEmployee.TextAutreNumero.DataSource = Nothing
   
    Set FormEmployee.TextDateDeNaissance.DataSource = Nothing
   
    Set FormEmployee.TextEmail.DataSource = Nothing
   
    Set FormEmployee.TextAssuranceSociale.DataSource = Nothing
   
    Set FormEmployee.RichTextBoxNotes.DataSource = Nothing
   
    Set FormEmployee.CheckUtilisateur.DataSource = Nothing
   
'     Set FormEmployee.AdodcEmployee.Recordset.ActiveConnection = Nothing
'    Set FormEmployee.AdodcEmployee.Recordset = Nothing
   
End Sub

******************************************************
0
Comment
Question by:markrhyman
  • 18
  • 15
33 Comments
 
LVL 29

Expert Comment

by:leonstryker
ID: 9659107
Try using the Filter method of the Recordset to do your search

In the code behind the Button you should add:

something like this
FormEmployee.AdodcEmployee.Filter = "([LastName] = '')"                                                    <----- This will reset the filter to all

FormEmployee.AdodcEmployee.Filter = "([LastName] = '" & Trim(txtLastName.Text) & "')"        <----- This should return only the record with the name entered
0
 

Author Comment

by:markrhyman
ID: 9659572
Thanks for the suggestion but I do not have that method and I get the error message when trying to use what you wrote.  I get Method or Data Member not found.  FILTER does not even appear in the list of data members for ADODC.

Here is the code behind the search button

*******************************************

            strSQL = SearchString()
           
            Set Rs = Cn.Execute(strSQL)
           
            FormEmployee.AdodcEmployee.Filter = "([LastName] = '" & Trim(TextNom.Text) & "')"
           
            If Not (Rs.BOF And Rs.EOF) Then
           
                Call EnableTextBoxes(FormEmployee)
               
                'Display Related Employee Fields for a search
                Call DisplayEmployeeFields
               
                'Display all fields and their appropriate labels
                Call RetourChampNormal
               
                If Rs.Fields("Utilisateur").Value Then
               
                    FormEmployee.CheckUtilisateur.Value = 1
                   
                Else
               
                    FormEmployee.CheckUtilisateur.Value = 0
                   
                End If
           
                FormEmployee.RichTextBoxNotes.TextRTF = Rs.Fields("Notes").Value
               
                FormEmployee.PicturePhoto.Picture = LoadPicture(Rs.Fields("Picture").Value)
           
                FormEmployee.CommandEmployee.Enabled = True
               
                FormEmployee.CommandEmployeeRecherche.Enabled = False
            Else
           
                MsgBox "Aucun Employé Trouvé"
                Call ClearAllText(FormEmployee)
                FormEmployee.TextPrenom.SetFocus
                   
            End If

*********************

Public Function SearchString() As String

    SearchString = "SELECT * FROM EmployeesQuery WHERE Employees.LastName = '" _
                    & FormEmployee.TextNom.Text & "' OR Employees.FirstName = '" _
                    & FormEmployee.TextPrenom.Text & "' OR Employees.Department = '" _
                    & FormEmployee.TextDepartement.Text & "' OR Employees.EmployeeNumber = '" _
                    & FormEmployee.TextNumeroEmploye.Text & "';"

End Function
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 9659875
Would you be willing to switch from ADODC to regular ADO  and a grid (I would take you throught the steps to do so)?

Leon
0
 

Author Comment

by:markrhyman
ID: 9659993
I am actually willing to do almost anything.  What would be different though to use ADO instead?  Just before we do the switch I just want to mention that I read on Microsoft that this was caused when not using the acmdTable in the command type of the ADODC data control.  I switched to AcmdTable and now I get systax error in FROM clause which is the from clause above.  I thus replaced the SELECT * FROM EmployeesQuery to SELECT * from Employees since Employees is the table name but I still get the SYNTAX error...

Anyway if you say ado is better let's go, I'm Ready to roll :)

0
 

Author Comment

by:markrhyman
ID: 9660019
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 9660044
Well basically all you have to do is:

1. Make a reference to ADO
2. Place a msgrid on your form
3. Create and open a ADODB.Connection
4. Create and open a ADODB.Recordset
5. Bind the the recordset to the grid
6. Use the text boxes to navigate the data

I will go through each step in the posts
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 9660087

1. Go to Projet/Properties and click on Microsoft ActiveX DataObjects 2.5 (or higher) library
2. Go to Projet/Controls and click on Microsoft DataGrid Control

3. Declare as a Global level variable: Public cnConn as ADODB.Connection

What kind of database you are using? That will determine you connection string
0
 

Author Comment

by:markrhyman
ID: 9660110
I am a bit Puzzled.  I did put a grid on my form.   The grid looks like a sort of Spreadsheet right?  How you want me to navigate my records using this grid?
0
 

Author Comment

by:markrhyman
ID: 9660119
I am using MS access right now and I do have globals already like:

Global Rs As ADODB.Recordset
Global Cn As ADODB.Connection

    Set Cn = New ADODB.Connection
    Set Rs = New ADODB.Recordset
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 9660140
No the grid is going to display your information.  You can use the same one you had before.  It is just easier for me to talk to something new, since I do not see you form.
0
 

Author Comment

by:markrhyman
ID: 9660171
To Bind the Grid to the recordset I would use something like DataGrid1.RecordSource = Recordset?

I cannot bind the Grid.
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 9660177
Your connection string should look something like this:

 strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & strFilePath & ";"

    With Cn
        .CursorLocation = adUseClient
        .Open strConn
    End With
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 9660186
I tshould be

Set DataGrid1.DataSource = Recordset?
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 9660204
Or more like

Set Rs = New ADODB.Recordset

Rs.Open strSQl, Cn, adOpenKeyset, adLockOptimistic
Set DataGrid1.DataSource = Rs
0
 

Author Comment

by:markrhyman
ID: 9660237
The connection is as you said

   
    ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=X:\Ipecac\Main.mdb;Jet OLEDB:Database Password=sesco"
   
    With Cn
        .CursorLocation = adUseClient
        .Open ConnStr
    End With


And This

Set FormEmployee.DataGrid1.DataSource = Rs

Does Work.  I get all my information in the grid pertaining to the Search Criteria.





0
 

Author Comment

by:markrhyman
ID: 9660247
Now my question is about the same as the beginning.... When I get multiple records back from my search... How can I navigate through them using some sort of Data Control so that when I click Next, Then theTextBoxes changes to the Next Record in the found Set and when I click back the same happens?
0
IT, Stop Being Called Into Every Meeting

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!

 
LVL 29

Expert Comment

by:leonstryker
ID: 9660249
Great, now to do the searches, how many text boxes do you have to search?
0
 

Author Comment

by:markrhyman
ID: 9660266
I have 4 textboxes that I can perform a search  on a TOTAL of 15.

First Name

Last Name

Department

And Employee Number

All form part of my search Independantly.  I use SQL with OR to get the results
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 9660280
Well you can bind the text boxes to the recordset as well

txtFirstName.DataSource = Rs
txtFirstName.DataField = "FirstName"

Then you can place a buttons for navigation and code them something like:

Private Sub cmdFirst_Click()
    Navigate Action.MoveFirst
End Sub

Private Function Navigate(Action As Integer) As Boolean
    Navigate = True
    On Error Resume Next
    With rsCmdMaster
        If .EditMode > adEditNone Then RecCheck
        Select Case Action
            Case 1  '/ Next
                .MoveNext
                If .EOF Then
                    .MovePrevious
                End If
            Case 2  '/ Previous
                .MovePrevious
                If .BOF Then
                    .MoveNext
                End If
            Case 3  '/ First
                .MoveFirst
            Case 4  '/ Last
                .MoveLast
        End Select
        lblRecCt = .AbsolutePosition & " OF " & .RecordCount
    End With
EXIT_FUNCTION:
    Exit Function
ERROR_FUNCTION:
    Navigate = False
    GoTo EXIT_FUNCTION
End Function
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 9660293
For searches you can go on the change event or use a button and you either a find or filter method

Rs.Filter = "([LastName] = '')"                              l
Rs.Filter = "([LastName] = '" & Trim(txtLastName.Text) & "')"  
0
 

Author Comment

by:markrhyman
ID: 9660336
I think it looks Pretty Good.  I need to take into consideration EOF and BOF because it works and I can browse the records even on multiple records found. Except I go EOF at some point hehe :)

0
 

Author Comment

by:markrhyman
ID: 9660346
Well Thanks a lot.  you've been of a great help.  I really appreciate your time.  So to release the Points I just click on Accept near your name right ?
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 9660348
Looks like you can handle it from here.  Let me know if you need more help.

Leon
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 9660354
Correct.  

Leon
0
 

Author Comment

by:markrhyman
ID: 9660365
Oh I see you already have the EOF and BOF in your Sub above but calling

Private Sub cmdFirst_Click()
    Navigate Action.MoveFirst
End Sub

Give me an error that Action is not defined
0
 
LVL 29

Accepted Solution

by:
leonstryker earned 125 total points
ID: 9660384
You need to have a button named cmdFirst

You can change the names based on the ones you give.  Just place the Navigate Action.MoveFirst in the Click event.
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 9660402
Thanks for the Grade.
0
 

Author Comment

by:markrhyman
ID: 9660409
No problem you have been of a great help and I really appreciate it.  I am still strugling with the Button a bit though :) I have the button named exactly as you have

Private Sub cmdFirst_Click()
    Navigate Action.MoveFirst
End Sub

I click and I get variable not defined.

0
 
LVL 29

Expert Comment

by:leonstryker
ID: 9660441
Sorry forgot this:

Public Enum Action
    MoveNext = 1
    MovePrevious = 2
    MoveFirst = 3
    MoveLast = 4
End Enum
0
 

Author Comment

by:markrhyman
ID: 9660457
Nope I get this time

Compile Error

Expected Variable or procedure, Not Module
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 9660475
Instead of Action.MoveFirst  you can just code a number

Private Sub cmdFirst_Click()
    Navigate 3
End Sub


This way you will not need to Type in your code
0
 

Author Comment

by:markrhyman
ID: 9660479
Ok it works.  Great.  Thanks a lot.

Man I had a headache with this....
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 9660482
Ok, take care.

Leon
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

762 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

19 Experts available now in Live!

Get 1:1 Help Now