• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 74446
  • Last Modified:

VB6 ADO, MS Access 2000. Dropdown list, select record, this becomes current record

Using VB6, MS Access 2000, ADO.  Used VB Data Form Wizard, Access, linked to the database NE.mdb, used "Single Record" option, ADO Data Control, selected a table called "Students", all records in table.  I am using an ADO Data Control because I need to update the fields from this form, so I want them to be bound to the database table.

PROBLEM: I have no way of searching for a certain Student, except to click through each record one at a time.  I would like to have a dropdown menu on the Name field, be able to select the correct name, then have this record become the current record.

There are many fields so would like to avoid using many lines of code to re-populate each field line by line.  I just need it to jump to the record (random access) rather than having to click through each sequentially.

I tried "Creating a Simple Datacombo Application" but it would not allow me to enter a Listfield in the properties of the DataCombo.

   
0
ssswmmcihm
Asked:
ssswmmcihm
  • 7
  • 2
  • 2
  • +2
1 Solution
 
MYLimCommented:
You have to add 1 combobox and insert all student name into combobox item.
then adodc1.find combobox.text
0
 
MYLimCommented:
'Please add 1 ComboBox and 1 CommandButton to text...
Private sub form_load()
call loaddatatocombo
end sub

Private sub CmdSearch_click()
adodc1.recordset.find combo1.text
end sub

Private sub LoadDataToCombo()
On error goto ErrFound
Dim Cnn As ADODB.Connection
Dim Rs As ADODB.Recordset
Dim DbPath as string

Set Cnn = New ADODB.Connection
Cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DbPath & ";User Id=admin;Password=;"

Set Rs = New ADODB.Recordset
Rs.CursorLocation = adUseClient
Rs.Open "Select StudentName from Students order by students asc", Cnn, adOpenStatic, adLockReadOnly

if Rs.recordcount then
 msgbox "Empty Recordset,Connection will be Close"
 rs.close
 set rs=  nothing

  Cnn.close
 set Cnn = nothing
 
 exit sub
end if

rs.movefirst
do while not rs.eof
      combo1.additem rs.fields(0).value
      rs.movenext
loop


 rs.close
 set rs=  nothing

  Cnn.close
 set Cnn = nothing
 
 exit sub
ErrFound:
msgbox "Err description : " & Err.Description
if rs.state=adstateOpen Then
  rs.close
  set rs = nothing
end if
 
if Cnn.state = adstateOpen then
  Cnn.close
  set Cnn = nothing
end if
End sub
0
 
ssswmmcihmAuthor Commented:
I tried this suggestion.  It appears to load all the NAME values into a combo box.
However, picking a name in the combo box does change the current record to the selected NAME's record, which is what I am looking for.  In other words, I am trying to give the user a drop-down list of names to choose from on the form, so that when they highlight and click on that name, the current record becomes the record they chose.

I do not want to use a search button.  The search button that you provided does not work, it gives a runtime error 3001.







0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
MYLimCommented:
ok,wait a minute...
0
 
MYLimCommented:
'it seem combo box have no event after you have select item from list.
'Maybe other control will help you.all i can do will like below...
'Press Enter after you have select item from the list.

Private Sub Combo1_KeyDown(KeyCode As Integer, Shift As Integer)
Dim St As String
St = Adodc1.Recordset.Fields(0).Name & "='" & Combo1.Text & "'"
Debug.Print St
If KeyCode = vbKeyReturn Then
   Adodc1.Recordset.Find St
End If
End Sub

'Why use Enter key to search ?
'if user using mouse scroll button to search data,this will slow down database process.
'anyway ,up to you.
0
 
spongieCommented:
Hi ssswmmcihm. Supposing you have other controls bound to the result set of the ado data control and your populating your combo box by looping through the same result set (recordset) and adding the items to the combo box list.  Put the following code on the click event of your combo box:

    If Not myADOControl.Recordset.BOF And Not myADOControl.Recordset.EOF Then
      myADOControl.Recordset.MoveFirst
      myADOControl.Recordset.Move (myComboBox.ListIndex)
    End If

Hope that helps :0)
0
 
MYLimCommented:
fiend,are you there :)
'I have find out what you want...
'just go to where your Vb6 install folder then search for PRJDE.VBP.Just like below:

C:\Program Files\Microsoft Visual Studio\MSDN\2000JAN\1033\SAMPLES\VB98\DEGuide\PRJDE.VBP

Run the program and click button combo...
0
 
MYLimCommented:
'i am such an idiot,your requirement can be simply fullfil by using click event ...Pls try again

Private Sub Combo1_Click()
On Error GoTo ErrFound
Debug.Print Adodc1.Recordset.Fields(0).Value & "='" & Combo1.Text & "'"
    Adodc1.Recordset.Find Adodc1.Recordset.Fields(0).Name & "='" & Combo1.Text & "'"
Exit Sub
ErrFound:

If Adodc1.Recordset.EOF = True Then
    Adodc1.Recordset.MoveFirst
else
    MsgBox "Error Number : " & Err.Number & Chr(10) + Chr(13) & "Error Description : " & Err.Description, vbInformation, Err.Source
End If
Resume
End Sub
0
 
ssswmmcihmAuthor Commented:
I wasn't able to make it work but I was able to figure out what I needed to do and am posting it here for the benefit of others who may have a similar question.  I do not know if this is the best way to do this, but it works.

The field I am looking to match on is field 5, that is why I changed it from field 0.

Private Sub Combo1_Click()

   'go to the beginning of the recordset
   datPrimaryRS.Recordset.MoveFirst
   'scan through each record until you find the record that matches the combo box value
   Do While Not datPrimaryRS.Recordset.EOF
      datPrimaryRS.Recordset.MoveNext
      If datPrimaryRS.Recordset.Fields(5).Value = Combo1.Text Then Exit Do
   Loop
   
End Sub
0
 
MYLimCommented:
Dear ssswmmcihm:)
I will subject you modify and use my code (Recordset.Find method) because your code will have problem when open large amount of records.
You can also try the Recordset.filters method.
anyway,up to you.
0
 
leenapedenekarCommented:
how to use two criteria in rs.find
say to find field1=text1.text and field2=text2.text
0
 
leenapedenekarCommented:
can someone help
0
 
russbergenCommented:
No way....the find method is not your best answer here.  You should simply use a Do Until EOF loop and exit the loop if you find the record.  For example

Rs.MoveFirst
Do Until Rs.EOF

    If Rs.Fields!Field1 = Text1.Text and Rs.Fields!Field2 = Text1.Text then
    'might want to assign the data to variables here or do whatever code you  need
        Exit Do
    Else
        Rs.MoveNext
    End If

Loop

If Rs.EOF then
    Msgbox "File not found"
End If

If you find the record, you  can code what you need to at that part of the loop.  If there is no match, the computer will inform the user.  This will only happen if it loops thru all of the  records and doesn't find anything.  Good luck!
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 7
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now