Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search based on two criteria 'Name' and 'Year' in a form.

Status
Not open for further replies.

SheilaAlighieri

Technical User
Nov 16, 2002
68
NL
Hi all,

I would like to make a two criteria search for my form. I was thinking of copying the example in the Northwind sample database. Here it works as follows. When you open the report 'Sales by Year' a form is opened, named 'Sales by Year Dialog'.
On this form you enter two fields which form the criteria for the query used in the Sales by Year report.

Only difference is, I would like to use it for a form, and in my case it won't show dates, but a name and a year "yyyy" respectively. The Field names are 'AccountName' and 'Year'

The result has to show the record of a specific name and year. But when I open the form Access asks for my parameters seperately and doesn't make use of my Sales by Year Dialog form. Can anyone check my code and see where I've gone wrong.

Or is there perhaps an easier way to achieve the same result? I want to make a search field which is based on two criteria. So the user can search quickly for a specific year and account. I use the query with the criteria as the record source for my form.


I copied the following in my Sales by Year Form:


Option Compare Database
Option Explicit
Public blnOpening As Boolean
Private Sub Form_NoData(Cancel As Integer)
' Display a message if user enters criteria for which there are no records.

Dim strMsg As String, strTitle As String
Dim intStyle As Integer

strMsg = "No Data for Date Range"
intStyle = vbOKOnly
strTitle = "Error"

MsgBox strMsg, intStyle, strTitle
Cancel = True

End Sub

Private Sub Form_Open(Cancel As Integer)
' Open Sales by Year Dialog form.
' IsLoaded function (defined in Utility Functions module) determines
' if specified form is open.

blnOpening = True

Dim strFormName As String

strFormName = "Sales by Year Dialog"


' Open form.
DoCmd.OpenForm strFormName, , , , , acDialog

blnOpening = False

End Sub

Private Sub Form_Close()
' Close the Sales by Year Dialog form.

Dim strFormName As String

strFormName = "Sales by Year Dialog"

DoCmd.Close acForm, strFormName

End Sub


This is the code I added to my sales by year dialog form:

Option Compare Database ' Use database order for string comparisons.
Option Explicit ' Requires variables to be declared before they are used.

Private Sub Cancel_Click()
' This code created by Command Button Wizard.
On Error GoTo Err_Cancel_Click

' Close form.
DoCmd.Close

Exit_Cancel_Click:
Exit Sub

Err_Cancel_Click:
MsgBox Err.Description
Resume Exit_Cancel_Click


End Sub


Private Sub OK_Click()
On Error GoTo Err_OK_Click

Dim strMsg As String, strTitle As String
Dim intStyle As Integer

' If Sales by Year form is not being opened for previewing or printing,
' cause an error. (blnOpening variable is true only when report's Open event
' is being executed.)
If Not Forms![Sales By Year].blnOpening Then Err.Raise 0

' Hide form.
Me.Visible = False

Exit_OK_Click:
Exit Sub

Err_OK_Click:
strMsg = "To use this form, you must open the Sales by Year form."
intStyle = vbOKOnly
strTitle = "Open from From"

MsgBox strMsg, intStyle, strTitle
Resume Exit_OK_Click

End Sub

These are the parameters and criteria in my query

Parameter Data Type
Forms![Sales by Year Dialog]!AccountName Value
Forms![Sales by Year Dialog]!Year Value

Criteria
[Forms]![Sales by Year Dialog]![AccountName]
[Forms]![Sales by Year Dialog]![Year]


Thanks,

Sheila
 
Hi Shiela,

If I were u, I would have just create a dynamic query based on your criteria Form and display the results in a form based on the query. Let me know if I have correctly understood your problem here is the idea.

strQRY = " select * from yourtable where accountnamefield="& !forms[Formname][txtboxname].value & " and yearfield = " !forms[Formname][txtbox2name].value

Create a querydef with the above qrystring, use this query result in your result form. use querydef.refresh before opening the reulst form.

if you need help pl. let me know.

regards

Shyam Shyam
cwizshyam@yahoo.com
 
Hi sheila,

I would open a recordsetclone and just do the code till I found the first one which met the ctiteris. Open the recordset as rs and:

rs.movefirst
do while not rs.eof
if rs![field1] = me.text1 then
if rs![field2] = me.text2 then
me.bookmark = rs.bookmark
goto outen:
endif
endif
rs.movenext
loop
msgbox "no find, Kid!"
outen:
rs.close
set rs = nothing
end sub
 
Hi! Thanks for you replies :) I think Shyam got my problem exactly right. But I am not sure how to build the query and where to place the code. I would greatly appreciate some more information. If you have a sample file available, it would be even more wonderful. I will provide you with my mail address in case you have one.

Many thanks,

Sheila
 
Hi Shiela,

Please send me the table or table structure to my below mentioned email address, I can develop and send you the module

Shyam Shyam
cwizshyam@yahoo.com
 
Why not open your dialog form first then once the data has been entered press OK and this hides the dialog box and opens the sales by year form, which is built on the query you already have.

Hope this helps, let me know Rob! [Bigcheeks]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top