1

All,

I am trying to create a search form to have users search for data using 2 parameters. I have a user form with a list box, three text boxes & a find button.

1st text box is txt_sname (Combobox)

2nd text box is txt_sdate

3rd text box is txt_sdate1

list box is lst_main

What I am trying to do here is have the user select a name from the combo box drop down & enter dates for example 12/11/2015 - 01/01/2016. Then I want the query to display in the list box.

The table I have set up which is "Main" has the following fields:

t_Name

t_Date

t_ContactID

t_Score

t_Comments

The query will look at t_Date and and t_Name, if the data matches the parameters from the text boxes it will then display the info on the list box. Can anyone point me in the right direction?

I am using the following to pass data to the table:

Private Sub c_Submit_Click()
Dim db As Database
Dim rec As Recordset

Set db = CurrentDb
Set rec = db.OpenRecordset("Select * from Main")

If IsNull(Me.txt_Name.Value) Or IsNull(Me.txt_Date.Value) Or           IsNull(Me.txt_Contact.Value) Then
MsgBox ("Error! Fill out all the fields!"), vbExclamation
Exit Sub
End If


rec.AddNew
rec("t_Name") = Me.txt_Name.Value
rec("t_Date") = Me.txt_Date
rec("t_ContactID") = Me.txt_Contact
rec("t_Score") = Me.txt_Score
rec("t_Comments") = Me.txt_Comments
rec.Update

Set rec = Nothing
Set db = Nothing

Me.txt_Name = Null
Me.txt_Date = Null
Me.txt_Contact = Null
Me.txt_Score = Null
Me.txt_Comments = Null

Me.Text32.Requery



MsgBox ("Record Added Successfully!")
End Sub

Thank you everyone for the help! Cheers!

1 Answer 1

1

Listboxes have the RowSource property and so can use a table or query. Simply set the RowSource according to values from other textboxes:

Private Sub c_Submit_Click()

    Me.lst_main.RowSource = "SELECT t_Name, t_Date, t_ContactID, t_Score, t_Comments" _
                             & " FROM Main" _ 
                             & " WHERE t_Name = '" & Me.txt_sname & "'" _
                             & " AND t_Date >= #" & Me.txt_sdate & "#" _
                             & " AND t_Date <= #" & Me.txt_sdate1 & "#;"
    Me.lst_main.RowSourceType = "Table/Query"
    Me.lst_main.Requery

End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.