markrhyman
asked on
How to Clear/Reset an ADODC recordset
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.OL EDB.4.0;Da ta Source=X:\Ipecac\Main.mdb; Persist Security Info=False;Jet OLEDB:Database Password=sesco"
.RecordSource = SearchString
End With
Set FormEmployee.TextPrenom.Da taSource = FormEmployee.AdodcEmployee
FormEmployee.TextPrenom.Da taField = "FirstName"
Set FormEmployee.TextNom.DataS ource = FormEmployee.AdodcEmployee
FormEmployee.TextNom.DataF ield = "LastName"
Set FormEmployee.TextAdresse.D ataSource = FormEmployee.AdodcEmployee
FormEmployee.TextAdresse.D ataField = "Address"
Set FormEmployee.TextVille.Dat aSource = FormEmployee.AdodcEmployee
FormEmployee.TextVille.Dat aField = "Ville"
Set FormEmployee.TextCodePosta le.DataSou rce = FormEmployee.AdodcEmployee
FormEmployee.TextCodePosta le.DataFie ld = "CodePostal"
Set FormEmployee.TextDeparteme nt.DataSou rce = FormEmployee.AdodcEmployee
FormEmployee.TextDeparteme nt.DataFie ld = "Department"
Set FormEmployee.TextDateEmbau che.DataSo urce = FormEmployee.AdodcEmployee
FormEmployee.TextDateEmbau che.DataFi eld = "DateEmployed"
Set FormEmployee.TextNumeroEmp loye.DataS ource = FormEmployee.AdodcEmployee
FormEmployee.TextNumeroEmp loye.DataF ield = "EmployeeNumber"
Set FormEmployee.TextSalaire.D ataSource = FormEmployee.AdodcEmployee
FormEmployee.TextSalaire.D ataField = "Salary"
Set FormEmployee.TextTelephone .DataSourc e = FormEmployee.AdodcEmployee
FormEmployee.TextTelephone .DataField = "Telephone"
Set FormEmployee.TextAutreNume ro.DataSou rce = FormEmployee.AdodcEmployee
FormEmployee.TextAutreNume ro.DataFie ld = "AutreNumero"
Set FormEmployee.TextDateDeNai ssance.Dat aSource = FormEmployee.AdodcEmployee
FormEmployee.TextDateDeNai ssance.Dat aField = "DOB"
Set FormEmployee.TextEmail.Dat aSource = FormEmployee.AdodcEmployee
FormEmployee.TextEmail.Dat aField = "Email"
Set FormEmployee.TextAssurance Sociale.Da taSource = FormEmployee.AdodcEmployee
FormEmployee.TextAssurance Sociale.Da taField = "SIN"
Set FormEmployee.RichTextBoxNo tes.DataSo urce = FormEmployee.AdodcEmployee
FormEmployee.RichTextBoxNo tes.DataFi eld = "Notes"
Set FormEmployee.CheckUtilisat eur.DataSo urce = FormEmployee.AdodcEmployee
FormEmployee.CheckUtilisat eur.DataFi eld = "Utilisateur"
'Set FormEmployee.PicturePhoto. DataSource = FormEmployee.AdodcEmployee
'FormEmployee.PicturePhoto .Picture = LoadPicture(Rs.Fields("Pic ture").Val ue)
'LoadPicture(Rs.Fields("Pi cture").Va lue)
End Sub
Public Sub DeactivateControl()
FormEmployee.AdodcEmployee .Visible = False
Set FormEmployee.TextPrenom.Da taSource = Nothing
Set FormEmployee.TextNom.DataS ource = Nothing
Set FormEmployee.TextAdresse.D ataSource = Nothing
Set FormEmployee.TextVille.Dat aSource = Nothing
Set FormEmployee.TextCodePosta le.DataSou rce = Nothing
Set FormEmployee.TextDeparteme nt.DataSou rce = Nothing
Set FormEmployee.TextDateEmbau che.DataSo urce = Nothing
Set FormEmployee.TextNumeroEmp loye.DataS ource = Nothing
Set FormEmployee.TextSalaire.D ataSource = Nothing
Set FormEmployee.TextTelephone .DataSourc e = Nothing
Set FormEmployee.TextAutreNume ro.DataSou rce = Nothing
Set FormEmployee.TextDateDeNai ssance.Dat aSource = Nothing
Set FormEmployee.TextEmail.Dat aSource = Nothing
Set FormEmployee.TextAssurance Sociale.Da taSource = Nothing
Set FormEmployee.RichTextBoxNo tes.DataSo urce = Nothing
Set FormEmployee.CheckUtilisat eur.DataSo urce = Nothing
' Set FormEmployee.AdodcEmployee .Recordset .ActiveCon nection = Nothing
' Set FormEmployee.AdodcEmployee .Recordset = Nothing
End Sub
************************** ********** ********** ********
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
With FormEmployee.AdodcEmployee
.ConnectionString = "Provider=Microsoft.Jet.OL
.RecordSource = SearchString
End With
Set FormEmployee.TextPrenom.Da
FormEmployee.TextPrenom.Da
Set FormEmployee.TextNom.DataS
FormEmployee.TextNom.DataF
Set FormEmployee.TextAdresse.D
FormEmployee.TextAdresse.D
Set FormEmployee.TextVille.Dat
FormEmployee.TextVille.Dat
Set FormEmployee.TextCodePosta
FormEmployee.TextCodePosta
Set FormEmployee.TextDeparteme
FormEmployee.TextDeparteme
Set FormEmployee.TextDateEmbau
FormEmployee.TextDateEmbau
Set FormEmployee.TextNumeroEmp
FormEmployee.TextNumeroEmp
Set FormEmployee.TextSalaire.D
FormEmployee.TextSalaire.D
Set FormEmployee.TextTelephone
FormEmployee.TextTelephone
Set FormEmployee.TextAutreNume
FormEmployee.TextAutreNume
Set FormEmployee.TextDateDeNai
FormEmployee.TextDateDeNai
Set FormEmployee.TextEmail.Dat
FormEmployee.TextEmail.Dat
Set FormEmployee.TextAssurance
FormEmployee.TextAssurance
Set FormEmployee.RichTextBoxNo
FormEmployee.RichTextBoxNo
Set FormEmployee.CheckUtilisat
FormEmployee.CheckUtilisat
'Set FormEmployee.PicturePhoto.
'FormEmployee.PicturePhoto
'LoadPicture(Rs.Fields("Pi
End Sub
Public Sub DeactivateControl()
FormEmployee.AdodcEmployee
Set FormEmployee.TextPrenom.Da
Set FormEmployee.TextNom.DataS
Set FormEmployee.TextAdresse.D
Set FormEmployee.TextVille.Dat
Set FormEmployee.TextCodePosta
Set FormEmployee.TextDeparteme
Set FormEmployee.TextDateEmbau
Set FormEmployee.TextNumeroEmp
Set FormEmployee.TextSalaire.D
Set FormEmployee.TextTelephone
Set FormEmployee.TextAutreNume
Set FormEmployee.TextDateDeNai
Set FormEmployee.TextEmail.Dat
Set FormEmployee.TextAssurance
Set FormEmployee.RichTextBoxNo
Set FormEmployee.CheckUtilisat
' Set FormEmployee.AdodcEmployee
' Set FormEmployee.AdodcEmployee
End Sub
**************************
ASKER
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(FormEmploy ee)
'Display Related Employee Fields for a search
Call DisplayEmployeeFields
'Display all fields and their appropriate labels
Call RetourChampNormal
If Rs.Fields("Utilisateur").V alue Then
FormEmployee.CheckUtilisat eur.Value = 1
Else
FormEmployee.CheckUtilisat eur.Value = 0
End If
FormEmployee.RichTextBoxNo tes.TextRT F = Rs.Fields("Notes").Value
FormEmployee.PicturePhoto. Picture = LoadPicture(Rs.Fields("Pic ture").Val ue)
FormEmployee.CommandEmploy ee.Enabled = True
FormEmployee.CommandEmploy eeRecherch e.Enabled = False
Else
MsgBox "Aucun Employé Trouvé"
Call ClearAllText(FormEmployee)
FormEmployee.TextPrenom.Se tFocus
End If
*********************
Public Function SearchString() As String
SearchString = "SELECT * FROM EmployeesQuery WHERE Employees.LastName = '" _
& FormEmployee.TextNom.Text & "' OR Employees.FirstName = '" _
& FormEmployee.TextPrenom.Te xt & "' OR Employees.Department = '" _
& FormEmployee.TextDeparteme nt.Text & "' OR Employees.EmployeeNumber = '" _
& FormEmployee.TextNumeroEmp loye.Text & "';"
End Function
Here is the code behind the search button
**************************
strSQL = SearchString()
Set Rs = Cn.Execute(strSQL)
FormEmployee.AdodcEmployee
If Not (Rs.BOF And Rs.EOF) Then
Call EnableTextBoxes(FormEmploy
'Display Related Employee Fields for a search
Call DisplayEmployeeFields
'Display all fields and their appropriate labels
Call RetourChampNormal
If Rs.Fields("Utilisateur").V
FormEmployee.CheckUtilisat
Else
FormEmployee.CheckUtilisat
End If
FormEmployee.RichTextBoxNo
FormEmployee.PicturePhoto.
FormEmployee.CommandEmploy
FormEmployee.CommandEmploy
Else
MsgBox "Aucun Employé Trouvé"
Call ClearAllText(FormEmployee)
FormEmployee.TextPrenom.Se
End If
*********************
Public Function SearchString() As String
SearchString = "SELECT * FROM EmployeesQuery WHERE Employees.LastName = '" _
& FormEmployee.TextNom.Text & "' OR Employees.FirstName = '" _
& FormEmployee.TextPrenom.Te
& FormEmployee.TextDeparteme
& FormEmployee.TextNumeroEmp
End Function
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
Leon
ASKER
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 :)
Anyway if you say ado is better let's go, I'm Ready to roll :)
ASKER
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
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
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
ASKER
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?
ASKER
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
Global Rs As ADODB.Recordset
Global Cn As ADODB.Connection
Set Cn = New ADODB.Connection
Set Rs = New ADODB.Recordset
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.
ASKER
To Bind the Grid to the recordset I would use something like DataGrid1.RecordSource = Recordset?
I cannot bind the Grid.
I cannot bind the Grid.
Your connection string should look something like this:
strConn = "Provider=Microsoft.Jet.OL EDB.4.0;Pe rsist Security Info=False;Data Source=" & strFilePath & ";"
With Cn
.CursorLocation = adUseClient
.Open strConn
End With
strConn = "Provider=Microsoft.Jet.OL
With Cn
.CursorLocation = adUseClient
.Open strConn
End With
I tshould be
Set DataGrid1.DataSource = Recordset?
Set DataGrid1.DataSource = Recordset?
Or more like
Set Rs = New ADODB.Recordset
Rs.Open strSQl, Cn, adOpenKeyset, adLockOptimistic
Set DataGrid1.DataSource = Rs
Set Rs = New ADODB.Recordset
Rs.Open strSQl, Cn, adOpenKeyset, adLockOptimistic
Set DataGrid1.DataSource = Rs
ASKER
The connection is as you said
ConnStr = "Provider=Microsoft.Jet.OL EDB.4.0;Da ta Source=X:\Ipecac\Main.mdb; Jet OLEDB:Database Password=sesco"
With Cn
.CursorLocation = adUseClient
.Open ConnStr
End With
And This
Set FormEmployee.DataGrid1.Dat aSource = Rs
Does Work. I get all my information in the grid pertaining to the Search Criteria.
ConnStr = "Provider=Microsoft.Jet.OL
With Cn
.CursorLocation = adUseClient
.Open ConnStr
End With
And This
Set FormEmployee.DataGrid1.Dat
Does Work. I get all my information in the grid pertaining to the Search Criteria.
ASKER
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?
Great, now to do the searches, how many text boxes do you have to search?
ASKER
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
First Name
Last Name
Department
And Employee Number
All form part of my search Independantly. I use SQL with OR to get the results
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
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
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) & "')"
Rs.Filter = "([LastName] = '')" l
Rs.Filter = "([LastName] = '" & Trim(txtLastName.Text) & "')"
ASKER
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 :)
ASKER
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 ?
Looks like you can handle it from here. Let me know if you need more help.
Leon
Leon
Correct.
Leon
Leon
ASKER
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
Private Sub cmdFirst_Click()
Navigate Action.MoveFirst
End Sub
Give me an error that Action is not defined
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thanks for the Grade.
ASKER
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.
Private Sub cmdFirst_Click()
Navigate Action.MoveFirst
End Sub
I click and I get variable not defined.
Sorry forgot this:
Public Enum Action
MoveNext = 1
MovePrevious = 2
MoveFirst = 3
MoveLast = 4
End Enum
Public Enum Action
MoveNext = 1
MovePrevious = 2
MoveFirst = 3
MoveLast = 4
End Enum
ASKER
Nope I get this time
Compile Error
Expected Variable or procedure, Not Module
Compile Error
Expected Variable or procedure, Not Module
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
Private Sub cmdFirst_Click()
Navigate 3
End Sub
This way you will not need to Type in your code
ASKER
Ok it works. Great. Thanks a lot.
Man I had a headache with this....
Man I had a headache with this....
Ok, take care.
Leon
Leon
In the code behind the Button you should add:
something like this
FormEmployee.AdodcEmployee
FormEmployee.AdodcEmployee