Search a record from a query and display it on the single form

  • Thread starter Thread starter Ac
  • Start date Start date
A

Ac

Hi,

I have a form contains a sub form and a search button. The sub form is a
single form displaying a record from a query one at time.

If I want to search a record from the query and display the retrieve the
record on the same single form using the search button. How can I do it?

Thanks
 
Hi,
sounds like you want to be able to filter the main form.
Put a combo on the main form that allows a user to choose from a list the
record to display in the subform.
On the combo's after update event you could use a filter
If Not IsNull(Me.cboComboName) Then
Me.Filter = "YourTableName.ID = " & me.cboComboName & ""
Me.FilterOn = True
End If

You should be able to do this using the wizard to put a combo on the main
form.
Choose the 3rd option - Find a record on my form based on the value I
selected in my combo box

Jeanette Cunningham
 
Thank you for your reply.

I should create three combo boxes, cboName, cboYear and cboLocation. The
user has to select the name and year first because the clients are repeated
after a year. How can I write combining information code?
 
With the extra info supplied in your next post, I would suggest to ignore my
previous advice.
When you click the search button, make it open a search form. Have a look at
the search form on http://www.allenbrowne.com/ser-62.html
After the user makes their choice from the search form, you can use the
criteria to set the recordsource for your subform to display that record.
Post back if you get stuck.

Jeanette Cunningham
 
Hi Jeanette,

I created a search button on the footer of the ClientEditForm which is the
single form to display client's info one at time. The search button will open
a search form and there are three places that user can input their search
info. on it, cboClientName, txtEffectiveDate and cboLocation. The code behind
the cmdFind button (on search form) will help to find the record and display
it back on the ClientEditForm form. Now the ClientEditForm form is not a
subform (least time I used this form as a sub from) . Should I change this
form back to sub form?

The code got the error message: Sytax error. in query expression "Select *
from qFeeSchedule Where ClientName = "071CA" (071CA is one of the test data
in the query).

I tried two different ways (see on the bottom of the code), they do not
work. Please help me for the code. Thanks!
 
I would keep ClientEditForm as a form and not a subform.
If the expression
"Select * from qFeeSchedule Where ClientName = "071CA"
is in the VBA code behind the form, it needs to be written out a bit
differently

"Select * From qFeeSchedule Where qFeeSchedule.ClientName = """ &
me.txtClientName & """"

txtClientName is the name of the textbox control for the ClientName field

If the expression is from a saved query, I don't know what is happening.
Where are you using the expression {"Select * from qFeeSchedule Where
ClientName = "071CA" }?

Jeanette Cunningham
 
Sorry last time the code did not attached in the back.

On the Row Source property of the cboClientName, I used SELECT DISTINCT
qFeeSchedule.ClientName FROM qFeeSchedule ORDER BY [ClientName]; so that the
user can select the client name from the combo box without typing it wrong.
Thank you!


Private Sub cmdFind_Click()

Dim stDocName As String
Dim year, where1 As String

stDocName = "ClientEditForm"


year = Trim(Right([Me.txtEffectiveDate], 4))
year = "[EffectiveDate] Between #1/1/" & year & "# And #12/31/" & year

where1 = ""

If Not IsNull(cboClientName) Then
where1 = "Where ClientName= """ + cboClientName + """"
End If

If Not IsNull(txtEffectiveDate) Then
If Len(where1) = 0 Then
where1 = "Where EffectiveDate = """ + year + """"
Else
where1 = where1 + ("AND EffectiveDate =""" + year + """")
End If
End If


If Not IsNull(cboLocation) Then
If Len(where1) = 0 Then
where1 = "Where ClientLocation = """ + (cboLocation) + """"
Else

where1 = where1 + ("AND ClientLocation =""" + cboLocation + """")
End If
End If


If Len(where1) > 0 Then

where1 = "Select * from qFeeSchedule " + where1

Forms!ClientEditForm.Form.RecordSource = where1
DoCmd.OpenForm stDocName, , , where1

'Forms!ClientEditForm.Form.RecordSource = where1
'Forms!ClientEditForm.Requery

End If

End Sub
 
The code looks fine. I am assuming that it now works the way you want it to.

Jeanette Cunningham

Ac said:
Sorry last time the code did not attached in the back.

On the Row Source property of the cboClientName, I used SELECT DISTINCT
qFeeSchedule.ClientName FROM qFeeSchedule ORDER BY [ClientName]; so that
the
user can select the client name from the combo box without typing it
wrong.
Thank you!


Private Sub cmdFind_Click()

Dim stDocName As String
Dim year, where1 As String

stDocName = "ClientEditForm"


year = Trim(Right([Me.txtEffectiveDate], 4))
year = "[EffectiveDate] Between #1/1/" & year & "# And #12/31/" & year

where1 = ""

If Not IsNull(cboClientName) Then
where1 = "Where ClientName= """ + cboClientName + """"
End If

If Not IsNull(txtEffectiveDate) Then
If Len(where1) = 0 Then
where1 = "Where EffectiveDate = """ + year + """"
Else
where1 = where1 + ("AND EffectiveDate =""" + year + """")
End If
End If


If Not IsNull(cboLocation) Then
If Len(where1) = 0 Then
where1 = "Where ClientLocation = """ + (cboLocation) + """"
Else

where1 = where1 + ("AND ClientLocation =""" + cboLocation +
"""")
End If
End If


If Len(where1) > 0 Then

where1 = "Select * from qFeeSchedule " + where1

Forms!ClientEditForm.Form.RecordSource = where1
DoCmd.OpenForm stDocName, , , where1

'Forms!ClientEditForm.Form.RecordSource = where1
'Forms!ClientEditForm.Requery

End If

End Sub



Jeanette Cunningham said:
I would keep ClientEditForm as a form and not a subform.
If the expression
"Select * from qFeeSchedule Where ClientName = "071CA"
is in the VBA code behind the form, it needs to be written out a bit
differently

"Select * From qFeeSchedule Where qFeeSchedule.ClientName = """ &
me.txtClientName & """"

txtClientName is the name of the textbox control for the ClientName field

If the expression is from a saved query, I don't know what is happening.
Where are you using the expression {"Select * from qFeeSchedule Where
ClientName = "071CA" }?

Jeanette Cunningham
 
Hi Jeanette,

With the code I posted to you yesterday, I still got the error message:
Sytax error. in query expression "Select * from qFeeSchedule Where ClientName
= "071CA" (071CA is one of the test data in the query). What is wrong?



Jeanette Cunningham said:
The code looks fine. I am assuming that it now works the way you want it to.

Jeanette Cunningham

Ac said:
Sorry last time the code did not attached in the back.

On the Row Source property of the cboClientName, I used SELECT DISTINCT
qFeeSchedule.ClientName FROM qFeeSchedule ORDER BY [ClientName]; so that
the
user can select the client name from the combo box without typing it
wrong.
Thank you!


Private Sub cmdFind_Click()

Dim stDocName As String
Dim year, where1 As String

stDocName = "ClientEditForm"


year = Trim(Right([Me.txtEffectiveDate], 4))
year = "[EffectiveDate] Between #1/1/" & year & "# And #12/31/" & year

where1 = ""

If Not IsNull(cboClientName) Then
where1 = "Where ClientName= """ + cboClientName + """"
End If

If Not IsNull(txtEffectiveDate) Then
If Len(where1) = 0 Then
where1 = "Where EffectiveDate = """ + year + """"
Else
where1 = where1 + ("AND EffectiveDate =""" + year + """")
End If
End If


If Not IsNull(cboLocation) Then
If Len(where1) = 0 Then
where1 = "Where ClientLocation = """ + (cboLocation) + """"
Else

where1 = where1 + ("AND ClientLocation =""" + cboLocation +
"""")
End If
End If


If Len(where1) > 0 Then

where1 = "Select * from qFeeSchedule " + where1

Forms!ClientEditForm.Form.RecordSource = where1
DoCmd.OpenForm stDocName, , , where1

'Forms!ClientEditForm.Form.RecordSource = where1
'Forms!ClientEditForm.Requery

End If

End Sub



Jeanette Cunningham said:
I would keep ClientEditForm as a form and not a subform.
If the expression
"Select * from qFeeSchedule Where ClientName = "071CA"
is in the VBA code behind the form, it needs to be written out a bit
differently

"Select * From qFeeSchedule Where qFeeSchedule.ClientName = """ &
me.txtClientName & """"

txtClientName is the name of the textbox control for the ClientName field

If the expression is from a saved query, I don't know what is happening.
Where are you using the expression {"Select * from qFeeSchedule Where
ClientName = "071CA" }?

Jeanette Cunningham

Hi Jeanette,

I created a search button on the footer of the ClientEditForm which is
the
single form to display client's info one at time. The search button
will
open
a search form and there are three places that user can input their
search
info. on it, cboClientName, txtEffectiveDate and cboLocation. The code
behind
the cmdFind button (on search form) will help to find the record and
display
it back on the ClientEditForm form. Now the ClientEditForm form is not
a
subform (least time I used this form as a sub from) . Should I change
this
form back to sub form?

The code got the error message: Sytax error. in query expression
"Select *
from qFeeSchedule Where ClientName = "071CA" (071CA is one of the test
data
in the query).

I tried two different ways (see on the bottom of the code), they do not
work. Please help me for the code. Thanks!




:

With the extra info supplied in your next post, I would suggest to
ignore
my
previous advice.
When you click the search button, make it open a search form. Have a
look
at
the search form on http://www.allenbrowne.com/ser-62.html
After the user makes their choice from the search form, you can use
the
criteria to set the recordsource for your subform to display that
record.
Post back if you get stuck.

Jeanette Cunningham




Thank you for your reply.

I should create three combo boxes, cboName, cboYear and cboLocation.
The
user has to select the name and year first because the clients are
repeated
after a year. How can I write combining information code?


:

Hi,
sounds like you want to be able to filter the main form.
Put a combo on the main form that allows a user to choose from a
list
the
record to display in the subform.
On the combo's after update event you could use a filter
If Not IsNull(Me.cboComboName) Then
Me.Filter = "YourTableName.ID = " & me.cboComboName & ""
Me.FilterOn = True
End If

You should be able to do this using the wizard to put a combo on
the
main
form.
Choose the 3rd option - Find a record on my form based on the value
I
selected in my combo box

Jeanette Cunningham



Hi,

I have a form contains a sub form and a search button. The sub
form
is
a
single form displaying a record from a query one at time.

If I want to search a record from the query and display the
retrieve
the
record on the same single form using the search button. How can I
do
it?

Thanks
 
Here is a debugging technique you can use with a query.
Create a copy of qFeeSchedule so you have a backup, while testing
qFeeSchedule.
Open qFeeSchedule in design view and type "071CA" in the criteria row for
ClientName, switch to design view and check if it returns any records.
If no records, then this is a clue to check out further.
For example you could look at the join in qFeeSchedule, if it is an inner
join maybe you need to change it to an outer join.

Jeanette Cunningham


Ac said:
Hi Jeanette,

With the code I posted to you yesterday, I still got the error message:
Sytax error. in query expression "Select * from qFeeSchedule Where
ClientName
= "071CA" (071CA is one of the test data in the query). What is wrong?



Jeanette Cunningham said:
The code looks fine. I am assuming that it now works the way you want it
to.

Jeanette Cunningham

Ac said:
Sorry last time the code did not attached in the back.

On the Row Source property of the cboClientName, I used SELECT DISTINCT
qFeeSchedule.ClientName FROM qFeeSchedule ORDER BY [ClientName]; so
that
the
user can select the client name from the combo box without typing it
wrong.
Thank you!


Private Sub cmdFind_Click()

Dim stDocName As String
Dim year, where1 As String

stDocName = "ClientEditForm"


year = Trim(Right([Me.txtEffectiveDate], 4))
year = "[EffectiveDate] Between #1/1/" & year & "# And #12/31/" &
year

where1 = ""

If Not IsNull(cboClientName) Then
where1 = "Where ClientName= """ + cboClientName + """"
End If

If Not IsNull(txtEffectiveDate) Then
If Len(where1) = 0 Then
where1 = "Where EffectiveDate = """ + year + """"
Else
where1 = where1 + ("AND EffectiveDate =""" + year + """")
End If
End If


If Not IsNull(cboLocation) Then
If Len(where1) = 0 Then
where1 = "Where ClientLocation = """ + (cboLocation) + """"
Else

where1 = where1 + ("AND ClientLocation =""" + cboLocation +
"""")
End If
End If


If Len(where1) > 0 Then

where1 = "Select * from qFeeSchedule " + where1

Forms!ClientEditForm.Form.RecordSource = where1
DoCmd.OpenForm stDocName, , , where1

'Forms!ClientEditForm.Form.RecordSource = where1
'Forms!ClientEditForm.Requery

End If

End Sub



:

I would keep ClientEditForm as a form and not a subform.
If the expression
"Select * from qFeeSchedule Where ClientName = "071CA"
is in the VBA code behind the form, it needs to be written out a bit
differently

"Select * From qFeeSchedule Where qFeeSchedule.ClientName = """ &
me.txtClientName & """"

txtClientName is the name of the textbox control for the ClientName
field

If the expression is from a saved query, I don't know what is
happening.
Where are you using the expression {"Select * from qFeeSchedule Where
ClientName = "071CA" }?

Jeanette Cunningham

Hi Jeanette,

I created a search button on the footer of the ClientEditForm which
is
the
single form to display client's info one at time. The search button
will
open
a search form and there are three places that user can input their
search
info. on it, cboClientName, txtEffectiveDate and cboLocation. The
code
behind
the cmdFind button (on search form) will help to find the record and
display
it back on the ClientEditForm form. Now the ClientEditForm form is
not
a
subform (least time I used this form as a sub from) . Should I
change
this
form back to sub form?

The code got the error message: Sytax error. in query expression
"Select *
from qFeeSchedule Where ClientName = "071CA" (071CA is one of the
test
data
in the query).

I tried two different ways (see on the bottom of the code), they do
not
work. Please help me for the code. Thanks!




:

With the extra info supplied in your next post, I would suggest to
ignore
my
previous advice.
When you click the search button, make it open a search form. Have
a
look
at
the search form on http://www.allenbrowne.com/ser-62.html
After the user makes their choice from the search form, you can use
the
criteria to set the recordsource for your subform to display that
record.
Post back if you get stuck.

Jeanette Cunningham




Thank you for your reply.

I should create three combo boxes, cboName, cboYear and
cboLocation.
The
user has to select the name and year first because the clients
are
repeated
after a year. How can I write combining information code?


:

Hi,
sounds like you want to be able to filter the main form.
Put a combo on the main form that allows a user to choose from a
list
the
record to display in the subform.
On the combo's after update event you could use a filter
If Not IsNull(Me.cboComboName) Then
Me.Filter = "YourTableName.ID = " & me.cboComboName & ""
Me.FilterOn = True
End If

You should be able to do this using the wizard to put a combo on
the
main
form.
Choose the 3rd option - Find a record on my form based on the
value
I
selected in my combo box

Jeanette Cunningham



Hi,

I have a form contains a sub form and a search button. The sub
form
is
a
single form displaying a record from a query one at time.

If I want to search a record from the query and display the
retrieve
the
record on the same single form using the search button. How
can I
do
it?

Thanks
 
I used Forms!ClientEditForm.Form.RecordSource = where1
Forms!ClientEditForm.Requery

instead of DoCmd.OpenForm stDocName, , , where1

If I only search the ClientName, the program retrieved the right record; but
if I fill in the txtEffectiveDate, it did not retrive anything. I changed the
code year as below:
year = "[EffectiveDate] Between #1/1/" & year & "# And #12/31/" & year & "#"

it still did not work, Could you help me again? Thanks a lot!

Jeanette Cunningham said:
Here is a debugging technique you can use with a query.
Create a copy of qFeeSchedule so you have a backup, while testing
qFeeSchedule.
Open qFeeSchedule in design view and type "071CA" in the criteria row for
ClientName, switch to design view and check if it returns any records.
If no records, then this is a clue to check out further.
For example you could look at the join in qFeeSchedule, if it is an inner
join maybe you need to change it to an outer join.

Jeanette Cunningham


Ac said:
Hi Jeanette,

With the code I posted to you yesterday, I still got the error message:
Sytax error. in query expression "Select * from qFeeSchedule Where
ClientName
= "071CA" (071CA is one of the test data in the query). What is wrong?



Jeanette Cunningham said:
The code looks fine. I am assuming that it now works the way you want it
to.

Jeanette Cunningham

Sorry last time the code did not attached in the back.

On the Row Source property of the cboClientName, I used SELECT DISTINCT
qFeeSchedule.ClientName FROM qFeeSchedule ORDER BY [ClientName]; so
that
the
user can select the client name from the combo box without typing it
wrong.
Thank you!


Private Sub cmdFind_Click()

Dim stDocName As String
Dim year, where1 As String

stDocName = "ClientEditForm"


year = Trim(Right([Me.txtEffectiveDate], 4))
year = "[EffectiveDate] Between #1/1/" & year & "# And #12/31/" &
year

where1 = ""

If Not IsNull(cboClientName) Then
where1 = "Where ClientName= """ + cboClientName + """"
End If

If Not IsNull(txtEffectiveDate) Then
If Len(where1) = 0 Then
where1 = "Where EffectiveDate = """ + year + """"
Else
where1 = where1 + ("AND EffectiveDate =""" + year + """")
End If
End If


If Not IsNull(cboLocation) Then
If Len(where1) = 0 Then
where1 = "Where ClientLocation = """ + (cboLocation) + """"
Else

where1 = where1 + ("AND ClientLocation =""" + cboLocation +
"""")
End If
End If


If Len(where1) > 0 Then

where1 = "Select * from qFeeSchedule " + where1

Forms!ClientEditForm.Form.RecordSource = where1
DoCmd.OpenForm stDocName, , , where1

'Forms!ClientEditForm.Form.RecordSource = where1
'Forms!ClientEditForm.Requery

End If

End Sub



:

I would keep ClientEditForm as a form and not a subform.
If the expression
"Select * from qFeeSchedule Where ClientName = "071CA"
is in the VBA code behind the form, it needs to be written out a bit
differently

"Select * From qFeeSchedule Where qFeeSchedule.ClientName = """ &
me.txtClientName & """"

txtClientName is the name of the textbox control for the ClientName
field

If the expression is from a saved query, I don't know what is
happening.
Where are you using the expression {"Select * from qFeeSchedule Where
ClientName = "071CA" }?

Jeanette Cunningham

Hi Jeanette,

I created a search button on the footer of the ClientEditForm which
is
the
single form to display client's info one at time. The search button
will
open
a search form and there are three places that user can input their
search
info. on it, cboClientName, txtEffectiveDate and cboLocation. The
code
behind
the cmdFind button (on search form) will help to find the record and
display
it back on the ClientEditForm form. Now the ClientEditForm form is
not
a
subform (least time I used this form as a sub from) . Should I
change
this
form back to sub form?

The code got the error message: Sytax error. in query expression
"Select *
from qFeeSchedule Where ClientName = "071CA" (071CA is one of the
test
data
in the query).

I tried two different ways (see on the bottom of the code), they do
not
work. Please help me for the code. Thanks!




:

With the extra info supplied in your next post, I would suggest to
ignore
my
previous advice.
When you click the search button, make it open a search form. Have
a
look
at
the search form on http://www.allenbrowne.com/ser-62.html
After the user makes their choice from the search form, you can use
the
criteria to set the recordsource for your subform to display that
record.
Post back if you get stuck.

Jeanette Cunningham




Thank you for your reply.

I should create three combo boxes, cboName, cboYear and
cboLocation.
The
user has to select the name and year first because the clients
are
repeated
after a year. How can I write combining information code?


:

Hi,
sounds like you want to be able to filter the main form.
Put a combo on the main form that allows a user to choose from a
list
the
record to display in the subform.
On the combo's after update event you could use a filter
If Not IsNull(Me.cboComboName) Then
Me.Filter = "YourTableName.ID = " & me.cboComboName & ""
Me.FilterOn = True
End If

You should be able to do this using the wizard to put a combo on
the
main
form.
Choose the 3rd option - Find a record on my form based on the
value
I
selected in my combo box

Jeanette Cunningham



Hi,

I have a form contains a sub form and a search button. The sub
form
is
a
single form displaying a record from a query one at time.

If I want to search a record from the query and display the
retrieve
the
record on the same single form using the search button. How
can I
do
it?

Thanks
 
To summarise:
If just use ClientName the query works OK.
Get the query working with both ClientName and Location, then we will look
at date.

Dates are a bit more tricky because Access is very particular about how you
write dates in queries.

In the criteria row for a date field (query in design view) you can write
something like this:
Between #1/1/2005# and #12/31/2006#
and access can handle the above.
It will just error out it you write Between #1/1/" & year & "# And #12/31/"
& year &

I am thinking that we have a problem with the way that the Effective Date is
stored in your table.
We need to fix this to make the query work.
In your table which stores the Effective Date, what is the data type for the
date field?

I also suggest that you download the sample search form with code from
http://www.allenbrowne.com/ser-62.html
This is an excellent sample that shows to do code for all data types
including dates.

Jeanette Cunningham

Ac said:
I used Forms!ClientEditForm.Form.RecordSource = where1
Forms!ClientEditForm.Requery

instead of DoCmd.OpenForm stDocName, , , where1

If I only search the ClientName, the program retrieved the right record;
but
if I fill in the txtEffectiveDate, it did not retrive anything. I changed
the
code year as below:
year = "[EffectiveDate] Between #1/1/" & year & "# And #12/31/" & year &
"#"

it still did not work, Could you help me again? Thanks a lot!

Jeanette Cunningham said:
Here is a debugging technique you can use with a query.
Create a copy of qFeeSchedule so you have a backup, while testing
qFeeSchedule.
Open qFeeSchedule in design view and type "071CA" in the criteria row for
ClientName, switch to design view and check if it returns any records.
If no records, then this is a clue to check out further.
For example you could look at the join in qFeeSchedule, if it is an inner
join maybe you need to change it to an outer join.

Jeanette Cunningham


Ac said:
Hi Jeanette,

With the code I posted to you yesterday, I still got the error message:
Sytax error. in query expression "Select * from qFeeSchedule Where
ClientName
= "071CA" (071CA is one of the test data in the query). What is wrong?



:

The code looks fine. I am assuming that it now works the way you want
it
to.

Jeanette Cunningham

Sorry last time the code did not attached in the back.

On the Row Source property of the cboClientName, I used SELECT
DISTINCT
qFeeSchedule.ClientName FROM qFeeSchedule ORDER BY [ClientName]; so
that
the
user can select the client name from the combo box without typing it
wrong.
Thank you!


Private Sub cmdFind_Click()

Dim stDocName As String
Dim year, where1 As String

stDocName = "ClientEditForm"


year = Trim(Right([Me.txtEffectiveDate], 4))
year = "[EffectiveDate] Between #1/1/" & year & "# And #12/31/" &
year

where1 = ""

If Not IsNull(cboClientName) Then
where1 = "Where ClientName= """ + cboClientName + """"
End If

If Not IsNull(txtEffectiveDate) Then
If Len(where1) = 0 Then
where1 = "Where EffectiveDate = """ + year + """"
Else
where1 = where1 + ("AND EffectiveDate =""" + year + """")
End If
End If


If Not IsNull(cboLocation) Then
If Len(where1) = 0 Then
where1 = "Where ClientLocation = """ + (cboLocation) +
""""
Else

where1 = where1 + ("AND ClientLocation =""" + cboLocation
+
"""")
End If
End If


If Len(where1) > 0 Then

where1 = "Select * from qFeeSchedule " + where1

Forms!ClientEditForm.Form.RecordSource = where1
DoCmd.OpenForm stDocName, , , where1

'Forms!ClientEditForm.Form.RecordSource = where1
'Forms!ClientEditForm.Requery

End If

End Sub



:

I would keep ClientEditForm as a form and not a subform.
If the expression
"Select * from qFeeSchedule Where ClientName = "071CA"
is in the VBA code behind the form, it needs to be written out a
bit
differently

"Select * From qFeeSchedule Where qFeeSchedule.ClientName = """
&
me.txtClientName & """"

txtClientName is the name of the textbox control for the ClientName
field

If the expression is from a saved query, I don't know what is
happening.
Where are you using the expression {"Select * from qFeeSchedule
Where
ClientName = "071CA" }?

Jeanette Cunningham

Hi Jeanette,

I created a search button on the footer of the ClientEditForm
which
is
the
single form to display client's info one at time. The search
button
will
open
a search form and there are three places that user can input
their
search
info. on it, cboClientName, txtEffectiveDate and cboLocation. The
code
behind
the cmdFind button (on search form) will help to find the record
and
display
it back on the ClientEditForm form. Now the ClientEditForm form
is
not
a
subform (least time I used this form as a sub from) . Should I
change
this
form back to sub form?

The code got the error message: Sytax error. in query expression
"Select *
from qFeeSchedule Where ClientName = "071CA" (071CA is one of
the
test
data
in the query).

I tried two different ways (see on the bottom of the code), they
do
not
work. Please help me for the code. Thanks!




:

With the extra info supplied in your next post, I would suggest
to
ignore
my
previous advice.
When you click the search button, make it open a search form.
Have
a
look
at
the search form on http://www.allenbrowne.com/ser-62.html
After the user makes their choice from the search form, you can
use
the
criteria to set the recordsource for your subform to display
that
record.
Post back if you get stuck.

Jeanette Cunningham




Thank you for your reply.

I should create three combo boxes, cboName, cboYear and
cboLocation.
The
user has to select the name and year first because the clients
are
repeated
after a year. How can I write combining information code?


:

Hi,
sounds like you want to be able to filter the main form.
Put a combo on the main form that allows a user to choose
from a
list
the
record to display in the subform.
On the combo's after update event you could use a filter
If Not IsNull(Me.cboComboName) Then
Me.Filter = "YourTableName.ID = " & me.cboComboName & ""
Me.FilterOn = True
End If

You should be able to do this using the wizard to put a combo
on
the
main
form.
Choose the 3rd option - Find a record on my form based on the
value
I
selected in my combo box

Jeanette Cunningham



Hi,

I have a form contains a sub form and a search button. The
sub
form
is
a
single form displaying a record from a query one at time.

If I want to search a record from the query and display the
retrieve
the
record on the same single form using the search button. How
can I
do
it?

Thanks
 
Thanks Jeanette,

The data type for Clientname and Location are text and EffectiveDate is Date.
Now I changed the code for EffectiveDate:

year = "([EffectiveDate]>= \#01\/01\/" & year & "\#)" And
"([EffectiveDate]<= \#12\/31\/" & year & "\#) "

The message I got is: Type mismatch. If I chenge the EffectiveDate data type
to text, can the query work? Sorry for so many questions.




Jeanette Cunningham said:
To summarise:
If just use ClientName the query works OK.
Get the query working with both ClientName and Location, then we will look
at date.

Dates are a bit more tricky because Access is very particular about how you
write dates in queries.

In the criteria row for a date field (query in design view) you can write
something like this:
Between #1/1/2005# and #12/31/2006#
and access can handle the above.
It will just error out it you write Between #1/1/" & year & "# And #12/31/"
& year &

I am thinking that we have a problem with the way that the Effective Date is
stored in your table.
We need to fix this to make the query work.
In your table which stores the Effective Date, what is the data type for the
date field?

I also suggest that you download the sample search form with code from
http://www.allenbrowne.com/ser-62.html
This is an excellent sample that shows to do code for all data types
including dates.

Jeanette Cunningham

Ac said:
I used Forms!ClientEditForm.Form.RecordSource = where1
Forms!ClientEditForm.Requery

instead of DoCmd.OpenForm stDocName, , , where1

If I only search the ClientName, the program retrieved the right record;
but
if I fill in the txtEffectiveDate, it did not retrive anything. I changed
the
code year as below:
year = "[EffectiveDate] Between #1/1/" & year & "# And #12/31/" & year &
"#"

it still did not work, Could you help me again? Thanks a lot!

Jeanette Cunningham said:
Here is a debugging technique you can use with a query.
Create a copy of qFeeSchedule so you have a backup, while testing
qFeeSchedule.
Open qFeeSchedule in design view and type "071CA" in the criteria row for
ClientName, switch to design view and check if it returns any records.
If no records, then this is a clue to check out further.
For example you could look at the join in qFeeSchedule, if it is an inner
join maybe you need to change it to an outer join.

Jeanette Cunningham


Hi Jeanette,

With the code I posted to you yesterday, I still got the error message:
Sytax error. in query expression "Select * from qFeeSchedule Where
ClientName
= "071CA" (071CA is one of the test data in the query). What is wrong?



:

The code looks fine. I am assuming that it now works the way you want
it
to.

Jeanette Cunningham

Sorry last time the code did not attached in the back.

On the Row Source property of the cboClientName, I used SELECT
DISTINCT
qFeeSchedule.ClientName FROM qFeeSchedule ORDER BY [ClientName]; so
that
the
user can select the client name from the combo box without typing it
wrong.
Thank you!


Private Sub cmdFind_Click()

Dim stDocName As String
Dim year, where1 As String

stDocName = "ClientEditForm"


year = Trim(Right([Me.txtEffectiveDate], 4))
year = "[EffectiveDate] Between #1/1/" & year & "# And #12/31/" &
year

where1 = ""

If Not IsNull(cboClientName) Then
where1 = "Where ClientName= """ + cboClientName + """"
End If

If Not IsNull(txtEffectiveDate) Then
If Len(where1) = 0 Then
where1 = "Where EffectiveDate = """ + year + """"
Else
where1 = where1 + ("AND EffectiveDate =""" + year + """")
End If
End If


If Not IsNull(cboLocation) Then
If Len(where1) = 0 Then
where1 = "Where ClientLocation = """ + (cboLocation) +
""""
Else

where1 = where1 + ("AND ClientLocation =""" + cboLocation
+
"""")
End If
End If


If Len(where1) > 0 Then

where1 = "Select * from qFeeSchedule " + where1

Forms!ClientEditForm.Form.RecordSource = where1
DoCmd.OpenForm stDocName, , , where1

'Forms!ClientEditForm.Form.RecordSource = where1
'Forms!ClientEditForm.Requery

End If

End Sub



:

I would keep ClientEditForm as a form and not a subform.
If the expression
"Select * from qFeeSchedule Where ClientName = "071CA"
is in the VBA code behind the form, it needs to be written out a
bit
differently

"Select * From qFeeSchedule Where qFeeSchedule.ClientName = """
&
me.txtClientName & """"

txtClientName is the name of the textbox control for the ClientName
field

If the expression is from a saved query, I don't know what is
happening.
Where are you using the expression {"Select * from qFeeSchedule
Where
ClientName = "071CA" }?

Jeanette Cunningham

Hi Jeanette,

I created a search button on the footer of the ClientEditForm
which
is
the
single form to display client's info one at time. The search
button
will
open
a search form and there are three places that user can input
their
search
info. on it, cboClientName, txtEffectiveDate and cboLocation. The
code
behind
the cmdFind button (on search form) will help to find the record
and
display
it back on the ClientEditForm form. Now the ClientEditForm form
is
not
a
subform (least time I used this form as a sub from) . Should I
change
this
form back to sub form?

The code got the error message: Sytax error. in query expression
"Select *
from qFeeSchedule Where ClientName = "071CA" (071CA is one of
the
test
data
in the query).

I tried two different ways (see on the bottom of the code), they
do
not
work. Please help me for the code. Thanks!




:

With the extra info supplied in your next post, I would suggest
to
ignore
my
previous advice.
When you click the search button, make it open a search form.
Have
a
look
at
the search form on http://www.allenbrowne.com/ser-62.html
After the user makes their choice from the search form, you can
use
the
criteria to set the recordsource for your subform to display
that
record.
Post back if you get stuck.

Jeanette Cunningham




Thank you for your reply.

I should create three combo boxes, cboName, cboYear and
cboLocation.
The
user has to select the name and year first because the clients
are
repeated
after a year. How can I write combining information code?


:

Hi,
sounds like you want to be able to filter the main form.
Put a combo on the main form that allows a user to choose
from a
list
the
record to display in the subform.
On the combo's after update event you could use a filter
If Not IsNull(Me.cboComboName) Then
Me.Filter = "YourTableName.ID = " & me.cboComboName & ""
Me.FilterOn = True
End If

You should be able to do this using the wizard to put a combo
on
the
main
form.
Choose the 3rd option - Find a record on my form based on the
value
I
selected in my combo box

Jeanette Cunningham



Hi,

I have a form contains a sub form and a search button. The
sub
form
is
a
single form displaying a record from a query one at time.

If I want to search a record from the query and display the
retrieve
the
 
Using & year & is the bit that won't work - it is generating the type
mismatch error.
It would help if we knew how you get the value for year from the form, then
we my be able to set the date up in the way Access requires it :
\#01\/01\/2007\#)" or whatever year you need.

Jeanette Cunningham

Ac said:
Thanks Jeanette,

The data type for Clientname and Location are text and EffectiveDate is
Date.
Now I changed the code for EffectiveDate:

year = "([EffectiveDate]>= \#01\/01\/" & year & "\#)" And
"([EffectiveDate]<= \#12\/31\/" & year & "\#) "

The message I got is: Type mismatch. If I chenge the EffectiveDate data
type
to text, can the query work? Sorry for so many questions.




Jeanette Cunningham said:
To summarise:
If just use ClientName the query works OK.
Get the query working with both ClientName and Location, then we will
look
at date.

Dates are a bit more tricky because Access is very particular about how
you
write dates in queries.

In the criteria row for a date field (query in design view) you can write
something like this:
Between #1/1/2005# and #12/31/2006#
and access can handle the above.
It will just error out it you write Between #1/1/" & year & "# And
#12/31/"
& year &

I am thinking that we have a problem with the way that the Effective Date
is
stored in your table.
We need to fix this to make the query work.
In your table which stores the Effective Date, what is the data type for
the
date field?

I also suggest that you download the sample search form with code from
http://www.allenbrowne.com/ser-62.html
This is an excellent sample that shows to do code for all data types
including dates.

Jeanette Cunningham

Ac said:
I used Forms!ClientEditForm.Form.RecordSource = where1
Forms!ClientEditForm.Requery

instead of DoCmd.OpenForm stDocName, , , where1

If I only search the ClientName, the program retrieved the right
record;
but
if I fill in the txtEffectiveDate, it did not retrive anything. I
changed
the
code year as below:
year = "[EffectiveDate] Between #1/1/" & year & "# And #12/31/" & year
&
"#"

it still did not work, Could you help me again? Thanks a lot!

:

Here is a debugging technique you can use with a query.
Create a copy of qFeeSchedule so you have a backup, while testing
qFeeSchedule.
Open qFeeSchedule in design view and type "071CA" in the criteria row
for
ClientName, switch to design view and check if it returns any records.
If no records, then this is a clue to check out further.
For example you could look at the join in qFeeSchedule, if it is an
inner
join maybe you need to change it to an outer join.

Jeanette Cunningham


Hi Jeanette,

With the code I posted to you yesterday, I still got the error
message:
Sytax error. in query expression "Select * from qFeeSchedule Where
ClientName
= "071CA" (071CA is one of the test data in the query). What is
wrong?



:

The code looks fine. I am assuming that it now works the way you
want
it
to.

Jeanette Cunningham

Sorry last time the code did not attached in the back.

On the Row Source property of the cboClientName, I used SELECT
DISTINCT
qFeeSchedule.ClientName FROM qFeeSchedule ORDER BY [ClientName];
so
that
the
user can select the client name from the combo box without typing
it
wrong.
Thank you!


Private Sub cmdFind_Click()

Dim stDocName As String
Dim year, where1 As String

stDocName = "ClientEditForm"


year = Trim(Right([Me.txtEffectiveDate], 4))
year = "[EffectiveDate] Between #1/1/" & year & "# And
#12/31/" &
year

where1 = ""

If Not IsNull(cboClientName) Then
where1 = "Where ClientName= """ + cboClientName + """"
End If

If Not IsNull(txtEffectiveDate) Then
If Len(where1) = 0 Then
where1 = "Where EffectiveDate = """ + year + """"
Else
where1 = where1 + ("AND EffectiveDate =""" + year + """")
End If
End If


If Not IsNull(cboLocation) Then
If Len(where1) = 0 Then
where1 = "Where ClientLocation = """ + (cboLocation) +
""""
Else

where1 = where1 + ("AND ClientLocation =""" +
cboLocation
+
"""")
End If
End If


If Len(where1) > 0 Then

where1 = "Select * from qFeeSchedule " + where1

Forms!ClientEditForm.Form.RecordSource = where1
DoCmd.OpenForm stDocName, , , where1

'Forms!ClientEditForm.Form.RecordSource = where1
'Forms!ClientEditForm.Requery

End If

End Sub



:

I would keep ClientEditForm as a form and not a subform.
If the expression
"Select * from qFeeSchedule Where ClientName = "071CA"
is in the VBA code behind the form, it needs to be written out a
bit
differently

"Select * From qFeeSchedule Where qFeeSchedule.ClientName =
"""
&
me.txtClientName & """"

txtClientName is the name of the textbox control for the
ClientName
field

If the expression is from a saved query, I don't know what is
happening.
Where are you using the expression {"Select * from qFeeSchedule
Where
ClientName = "071CA" }?

Jeanette Cunningham

Hi Jeanette,

I created a search button on the footer of the ClientEditForm
which
is
the
single form to display client's info one at time. The search
button
will
open
a search form and there are three places that user can input
their
search
info. on it, cboClientName, txtEffectiveDate and cboLocation.
The
code
behind
the cmdFind button (on search form) will help to find the
record
and
display
it back on the ClientEditForm form. Now the ClientEditForm
form
is
not
a
subform (least time I used this form as a sub from) . Should I
change
this
form back to sub form?

The code got the error message: Sytax error. in query
expression
"Select *
from qFeeSchedule Where ClientName = "071CA" (071CA is one of
the
test
data
in the query).

I tried two different ways (see on the bottom of the code),
they
do
not
work. Please help me for the code. Thanks!




:

With the extra info supplied in your next post, I would
suggest
to
ignore
my
previous advice.
When you click the search button, make it open a search form.
Have
a
look
at
the search form on http://www.allenbrowne.com/ser-62.html
After the user makes their choice from the search form, you
can
use
the
criteria to set the recordsource for your subform to display
that
record.
Post back if you get stuck.

Jeanette Cunningham




Thank you for your reply.

I should create three combo boxes, cboName, cboYear and
cboLocation.
The
user has to select the name and year first because the
clients
are
repeated
after a year. How can I write combining information code?


:

Hi,
sounds like you want to be able to filter the main form.
Put a combo on the main form that allows a user to choose
from a
list
the
record to display in the subform.
On the combo's after update event you could use a filter
If Not IsNull(Me.cboComboName) Then
Me.Filter = "YourTableName.ID = " & me.cboComboName &
""
Me.FilterOn = True
End If

You should be able to do this using the wizard to put a
combo
on
the
main
form.
Choose the 3rd option - Find a record on my form based on
the
value
I
selected in my combo box

Jeanette Cunningham



Hi,

I have a form contains a sub form and a search button.
The
sub
form
is
a
single form displaying a record from a query one at
time.

If I want to search a record from the query and display
the
retrieve
the
 
Back
Top