Wrox Programmer Forums
|
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 6th, 2007, 12:47 PM
Friend of Wrox
 
Join Date: Jan 2007
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to scandalous Send a message via MSN to scandalous
Default Search Form?

I am having trouble finding information on making a Search Form where I can search for a record set. From what I have gathered I need to:

1- make a query with the fields that I am looking to search by,
2- make the form based on fields that I am looking for or criteria I want it to include(making sure its unbound)
3- Write code for the Button that actually does the search. (making sure it opens another form to view the results at the end.)


Does that sound right? A more detailed explanation of how to do this would be very much appreciated.

 
Old February 7th, 2007, 09:06 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Yes. In the query designer in the criteria lines, add code to take parameters from the look up (presumably combo boxes) on your launch form. Like this:

[Forms]![frmMyForm].[cboMyCombo]

Did that help?


mmcdonal
 
Old February 7th, 2007, 10:26 AM
Friend of Wrox
 
Join Date: Jan 2007
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to scandalous Send a message via MSN to scandalous
Default

Yes it helps alot!

So I just want to make sure I do this Correctly.

- I need to place the parameters ie [Forms]![fndQuote].[cboSalesID] in the SalesID Field in the fndQuote Query and the same for the other combo boxes

What if the user doesnt select anything from one of the Combo Boxes?

And how would I show Matching Records? Should I make a DoCmd to open my quote form at the end of the code of the Search Button?

Thanks again!

 
Old February 7th, 2007, 11:10 AM
Friend of Wrox
 
Join Date: Jan 2007
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to scandalous Send a message via MSN to scandalous
Default

Okay I "Borrowed" some code but its in 2003 format. I am making a .mdb (2000)so I am not sure how this will work also I dont know why but i am having problems with the " If IsNothing ". I hope this will address the problem of the user not selecting one of the fields. Let me know your thoughts.

Private Sub cmdSearch_Click()
Dim varWhere As Variant

    ' Initialize to Null
    varWhere = Null

    ' If specified a SalesID value
    If Not IsNothing(Me.cboSalesID) Then
        ' .. build the predicate
        varWhere = "[SalesID] LIKE '" & Me.cboSalesID & "*'"
    End If

    ' Do DepartmentID next
    If Not IsNothing(Me.cboDepartmentID) Then
        ' .. build the predicate
        ' Note: taking advantage of Null propogation
        ' so we don't have to test for any previous predicate
        varWhere = (varWhere + " AND ") & "[DepartmentID] LIKE '" & Me.cboDepartmentID & "*'"
    End If

    ' Do CustomerID next
    If Not IsNothing(Me.cboCustomerID) Then
        ' .. build the predicate
        varWhere = (varWhere + " AND ") & "[CustomerID] LIKE '" & Me.cboCustomerID & "*'"
    End If

    ' Do QuoteStatus next
    If Not IsNothing(Me.cboQuoteStatus) Then
        ' .. build the predicate
        varWhere = (varWhere + " AND ") & "[QuoteStatus] LIKE '" & Me.cboQuoteStatus & "*'"
    End If

    ' Finally, do QuoteID By
    If Not IsNothing(Me.cboQuoteNumber) Then
        ' .. build the predicate
        varWhere = (varWhere + " AND ") & "[QuoteID] = " & Me.cboQuoteNumber
    End If

    ' Check to see that we built a filter
    If IsNothing(varWhere) Then
        MsgBox "You must enter at least one search criteria.", vbInformation, gstrAppTitle
        Exit Sub
    End If

    ' See if any rows with a quick DLookup
    If IsNothing(DLookup("QuoteID", "tblQuoteNumber", varWhere)) Then
        MsgBox "No Companies meet your criteria.", vbInformation, gstrAppTitle
        Exit Sub
    End If

    ' Open Companies filtered
    ' Note: if form already open, this just applies the filter
    DoCmd.OpenForm "frmQuotes", WhereCondition:=varWhere
    ' Done
    DoCmd.Close acForm, Me.Name

End Sub


 
Old February 7th, 2007, 11:38 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

First of all:

If Not IsNothing(Me.cboSalesID) Then

Should be:

If Not IsNull(Me.cboSalesID) Or Me.cboSalesID = "" Then

I use check for both Null and "" since Access is a little flakey with empty fields.

Also,

Dim varWhere As Variant

Should be

Dim varWhere As String

Also, this is okay:

 If Not IsNull(Me.cboSalesID) Or Me.cboSalesID = "" Then
        ' .. build the predicate
        varWhere = "[SalesID] LIKE '" & Me.cboSalesID & "*'"
    End If

But this needs to be changed:

' Do CustomerID next
If Not IsNothing(Me.cboCustomerID) Then
     ' .. build the predicate
     varWhere = (varWhere + " AND ") & "[CustomerID] LIKE '" & Me.cboCustomerID & "*'"
End If

To this:

'Do CustomerID next
If Not IsNull(Me.cboCustomerID) Or Me.cboCustomerID = "" Then
   If IsNull(varWhere) Or varWhere = "" Then
     varWhere = "[CustomerID] = " & Me.cboCustomerID 'assuming this is an integer
   Else
     varWhere = varWhere & " AND [CustomerID] = " & Me.cboCustomerID
   End IF
End If


HTH

mmcdonal
 
Old February 7th, 2007, 04:40 PM
Friend of Wrox
 
Join Date: Jan 2007
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to scandalous Send a message via MSN to scandalous
Default

Sweet it worked like a charm

Your the Man!

 
Old February 8th, 2007, 08:19 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

You may want to look up "Flow Control" in a VBA or VBScript reference for more information on this subject. Glad to have helped.

mmcdonal
 
Old February 8th, 2007, 08:58 AM
Friend of Wrox
 
Join Date: Jan 2007
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to scandalous Send a message via MSN to scandalous
Default

I have 2 Reference books "MS Access 2003 Inside Out" and the WROX "Access 2003 VBA" reference and neither had the term Flow Control in it. I looked on Google and they had some great descriptions. Is there another name for "Flow Control" so that i can look again inthe reference guides?


BTW I cant thank you enough for answering my questions I wish there was more i could do to show my gratitude.

 
Old February 8th, 2007, 09:01 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

No, I think the term is Programming Flow Control. Look for the section that describes conditionals and their use, and maybe it is around there.

Just pass it along.

mmcdonal





Similar Threads
Thread Thread Starter Forum Replies Last Post
Search for a record using a form a_wahab Access 6 September 1st, 2006 12:03 PM
Search Controls on a Form donnie_darko1983 ASP.NET 1.0 and 1.1 Basics 2 April 12th, 2005 05:20 AM
Search in a sub form smang Access 1 January 7th, 2005 08:30 PM
Search Form ksa266 Access VBA 2 December 29th, 2003 06:37 AM
Problem with Search Form DRuiz VB Databases Basics 3 June 6th, 2003 11:16 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.