Hello there Good Day!

I need your expertise in VB6 i have an assignment we made a small program with search button with combobox. I have a code with almost working. My problem is i want my search button enter any fiels to search with the combo box list. I have my list in combo box the fields of my database: Employee ID, Firstname, Middlename, Lastname, homeaddress, emailaddress, contactnumber, birthdate, age. What i want is what ever i select in the combo list i can search in my search input box....

Here's my almost working code:

Private Sub cmdsearch_Click()
    Dim dok As String
    dok = InputBox(" Enter Any Fields to Search")
    Adodc1.Recordset.Find "firstname = '" & dok & "'"
    If Adodc1.Recordset.EOF Then
    MsgBox "No Record Found!", vbInformation
    End If
    
End Sub

But this code only firstname fields will work, the other fields are error. Because of line4 statement..

Recommended Answers

All 13 Replies

only firstname fields will work, the other fields are error. Because of line4 statement..

because you have coded it that way.

you have to select the field names dynamically from combo box at run time.

But in your you have hard coded that to firstname only.

@dibasisdas

How can i select the all fields dynamically in the rum time???

What does the combobox contains ?

Am guessing that your combo-box contains all the fields you can also search for?

The combo box contains all the fields in my database which mentioned in my starter thread.....

For clarification here's my program i made. You can find a search button in my program and a combo box with field list. What i want is when you select the fields in the combo box you can search it in the search inputbox....

download this:

http://www.mediafire.com/?ssqx30h8c68h7uv

try this

Adodc1.Recordset.Find & combo1.list (combo1.listindex) & " = '" & dok & "'"

@debasisdas

Thank you very search the code is very much working. Now i can search multiple fields in my search box selecting in combo box fields list. But can you explain me how this code work specially this condition:

combo1.list (combo1.listindex) & " = '" & dok & "'"

thank you for this code is working:

Adodc1.Recordset.Find  combo1.list (combo1.listindex) & " = '" & dok & "'"

Thank you to those who replied my thread especial to Mr. debasisdas

@debasisdas

By the way the code is absolutely working. But the problem now, is i need to closed or re open the program before i can search it again because when i search it again its says no record found which is in my statement. Or i search with different fields again i want to closed of re open the program otherwise it say no record found. I think i have little bit problem on my statement that i can hardly analyze...

this code is working for multiple field search but the problem is i need to closed or re open the program.

Private Sub cmdsearch_Click()
    Dim dok As String
    dok = InputBox(" Enter Any Fields to Search")
    Adodc1.Recordset.Find Combo1.List(Combo1.ListIndex) & " = '" & dok & "'"
    If Adodc1.Recordset.EOF Then
    MsgBox "No Record Found!", vbInformation
    End If
end sub

Suppose you select Firstname from your combo box and enter Debasis in the input box .

So the the following line

Adodc1.Recordset.Find  combo1.list (combo1.listindex) & " = '" & dok & "'"

becomes
Step1.

Adodc1.Recordset.Find  Firstname & " = '" & Debasis & "'"

Step2.

Adodc1.Recordset.Find  Firstname = 'Debasis'

So you get the record where Firstname = 'Debasis', simple.
And since script is dynamic it will also work for Lastname = 'Das' and so on.

@debasisdas

Thank you master its very will said. My problem now when i search and search it again. First time you search its OK you got what you got what you want. But as what i said when you trying to search and search it again it always says No record found which is in my if statement. I need to closed of re open the program every time i search it again in any fields. I think the problem is in my statement which i can hardly analyze....

Private Sub cmdsearch_Click()
    Dim dok As String
    dok = InputBox(" Enter Any Fields to Search")
    Adodc1.Recordset.Find Combo1.List(Combo1.ListIndex) & " = '" & dok & "'"
    If Adodc1.Recordset.EOF Then
    MsgBox "No Record Found!", vbInformation
    End If
end sub

Thank you guys problem solve with the search button reach in the middle of databa the cannot find the previous entry of my database that's why it jumps to the if statement. Thank you master debasisdas and scudzilla for helping me out. Here's the correct construction of code:

Just differentiate this code with my previous code posted.

Private Sub cmdsearch_Click()
    On Error Resume Next
    Dim dok As String
    dok = InputBox("Enter" & Combo1 & "to Search", "Searching")
    Adodc1.Recordset.MoveFirst
    Adodc1.Recordset.Find Combo1.List(Combo1.ListIndex) & " = '" & dok & "'"
    If Adodc1.Recordset.EOF Then
    MsgBox "No Record Found!", vbInformation
    End If
End Sub

i don't have the code in line number 5 in my previous code i posted.... that code will refresh or of going back the first entry every time you search in entry in database.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.