Change query from textbox on form

D

DebbieG

I have a form based on this query:


SELECT Students.LastSerDT, OtherInfo.Served, OtherInfo.HSGradYr,
OtherInfo.ActivePart, OtherInfo.Served, Students.SSN, [LastNM] & ", " &
[FirstNM] & " " & [MI] AS Name, Students.LastNM, Students.FirstNM,
Students.MI, Students.DOB, Students.GenderCD, Students.EthnicityCD,
Students.EligibilityCD, Students.UBInitiative, Students.NCESSchID,
Students.ProjEntryDT, Students.ProjReEntDT, Students.LastSerDT,
Students.Reason, Students.PartCD, Students.PartLV, Students.NeedCD,
Students.AssessCD, Students.PSAT, Students.PLAN, Students.EnterGradeLV,
Students.EndGradeLV, Students.EntryGPAScale, Students.CumGPAEntry,
Students.EndGPAScale, Students.HSGPA1, Students.HSGPA2, Students.CollExamCD,
OtherInfo.NCESSchNM, OtherInfo.MiddleNM, OtherInfo.CurrentGradeLV,
OtherInfo.CurrentHSNM, Students.PicturePath
FROM Students LEFT JOIN OtherInfo ON Students.SSN = OtherInfo.SSN
WHERE (((Students.LastSerDT) Is Null)) OR (((OtherInfo.Served)=Yes)) OR
((([Forms]![frmStudents]![CheckCriteria])=Yes))
ORDER BY Students.LastNM, Students.FirstNM;

If [Forms]![frmStudents]![CheckCriteria]=No then it shows records WHERE
Students.LastSerDT Is Null OR OtherInfo.Served=Yes. (This is the default
when the form is opened.) If [Forms]![frmStudents]![CheckCriteria]=Yes then
it shows every record. It works just like I wanted.
When I click the CheckCriteria checkbox this is called:

Private Sub CheckCriteria_Click()
'if checkbox = yes then shows all students, else shows current/active
Me.Requery
Me.ComboStudent.Requery
ShowName = ComboStudent.Column(2)
End Sub

The ComboStudent combobox gets its information from the same query so that
it shows the correct records.

However, now the user wants the option of showing students with a particular
HSGradYr. I have added a textbox called txtGradYear to my form for the user
to enter a year, but I don't know where to go from here.

I'm basically wanting this one query to act in 3 different ways.

1. WHERE Students.LastSerDT Is Null OR OtherInfo.Served=Yes

2. WHERE [Forms]![frmStudents]![CheckCriteria]=Yes

3. WHERE OtherInfo.HSGradYr=[forms]![frmStudents]![txtGradYear]

And I want the ComboStudent to display the appropriate entries for each
WHERE.

I've got it working the first 2 ways, but I cannot figure out how to get the
3rd way to work. I tried:

WHERE (Students.LastSerDT Is Null OR OtherInfo.Served=Yes) OR
([Forms]![frmStudents]![CheckCriteria]=Yes) OR
(OtherInfo.HSGradYr=[forms]![frmStudents]![txtGradYear])

but I didn't get JUST the records where
OtherInfo.HSGradYr=[forms]![frmStudents]![txtGradYear] which makes sense.

Can anyone help me get what I need?

Thanks in advance,
Debbie
 
G

Guest

WHERE (((Students.LastSerDT Is Null OR
OtherInfo.Served=Yes) OR
([Forms]![frmStudents]![CheckCriteria]=Yes)) AND
(OtherInfo.HSGradYr=[forms]![frmStudents]![txtGradYear]))

or maybe is because how you define HSGradYr, is a string
or a date? if it is a date, then you will need to convert
txtGradYear to a valid date.

-----Original Message-----
I have a form based on this query:


SELECT Students.LastSerDT, OtherInfo.Served, OtherInfo.HSGradYr,
OtherInfo.ActivePart, OtherInfo.Served, Students.SSN, [LastNM] & ", " &
[FirstNM] & " " & [MI] AS Name, Students.LastNM, Students.FirstNM,
Students.MI, Students.DOB, Students.GenderCD, Students.EthnicityCD,
Students.EligibilityCD, Students.UBInitiative, Students.NCESSchID,
Students.ProjEntryDT, Students.ProjReEntDT, Students.LastSerDT,
Students.Reason, Students.PartCD, Students.PartLV, Students.NeedCD,
Students.AssessCD, Students.PSAT, Students.PLAN, Students.EnterGradeLV,
Students.EndGradeLV, Students.EntryGPAScale, Students.CumGPAEntry,
Students.EndGPAScale, Students.HSGPA1, Students.HSGPA2, Students.CollExamCD,
OtherInfo.NCESSchNM, OtherInfo.MiddleNM, OtherInfo.CurrentGradeLV,
OtherInfo.CurrentHSNM, Students.PicturePath
FROM Students LEFT JOIN OtherInfo ON Students.SSN = OtherInfo.SSN
WHERE (((Students.LastSerDT) Is Null)) OR (((OtherInfo.Served)=Yes)) OR
((([Forms]![frmStudents]![CheckCriteria])=Yes))
ORDER BY Students.LastNM, Students.FirstNM;

If [Forms]![frmStudents]![CheckCriteria]=No then it shows records WHERE
Students.LastSerDT Is Null OR OtherInfo.Served=Yes. (This is the default
when the form is opened.) If [Forms]![frmStudents]! [CheckCriteria]=Yes then
it shows every record. It works just like I wanted.
When I click the CheckCriteria checkbox this is called:

Private Sub CheckCriteria_Click()
'if checkbox = yes then shows all students, else shows current/active
Me.Requery
Me.ComboStudent.Requery
ShowName = ComboStudent.Column(2)
End Sub

The ComboStudent combobox gets its information from the same query so that
it shows the correct records.

However, now the user wants the option of showing students with a particular
HSGradYr. I have added a textbox called txtGradYear to my form for the user
to enter a year, but I don't know where to go from here.

I'm basically wanting this one query to act in 3 different ways.

1. WHERE Students.LastSerDT Is Null OR OtherInfo.Served=Yes

2. WHERE [Forms]![frmStudents]![CheckCriteria]=Yes

3. WHERE OtherInfo.HSGradYr=[forms]![frmStudents]! [txtGradYear]

And I want the ComboStudent to display the appropriate entries for each
WHERE.

I've got it working the first 2 ways, but I cannot figure out how to get the
3rd way to work. I tried:

WHERE (Students.LastSerDT Is Null OR OtherInfo.Served=Yes) OR
([Forms]![frmStudents]![CheckCriteria]=Yes) OR
(OtherInfo.HSGradYr=[forms]![frmStudents]![txtGradYear])

but I didn't get JUST the records where
OtherInfo.HSGradYr=[forms]![frmStudents]![txtGradYear] which makes sense.

Can anyone help me get what I need?

Thanks in advance,
Debbie


.
 
D

DebbieG

Thanks for your response. But that is not working. I tried just typing in
a year in the query and running the query and that didn't get just the
HSGradYr.


WHERE (((Students.LastSerDT Is Null OR
OtherInfo.Served=Yes) OR
([Forms]![frmStudents]![CheckCriteria]=Yes)) AND
(OtherInfo.HSGradYr=[forms]![frmStudents]![txtGradYear]))

or maybe is because how you define HSGradYr, is a string
or a date? if it is a date, then you will need to convert
txtGradYear to a valid date.

-----Original Message-----
I have a form based on this query:


SELECT Students.LastSerDT, OtherInfo.Served, OtherInfo.HSGradYr,
OtherInfo.ActivePart, OtherInfo.Served, Students.SSN, [LastNM] & ", " &
[FirstNM] & " " & [MI] AS Name, Students.LastNM, Students.FirstNM,
Students.MI, Students.DOB, Students.GenderCD, Students.EthnicityCD,
Students.EligibilityCD, Students.UBInitiative, Students.NCESSchID,
Students.ProjEntryDT, Students.ProjReEntDT, Students.LastSerDT,
Students.Reason, Students.PartCD, Students.PartLV, Students.NeedCD,
Students.AssessCD, Students.PSAT, Students.PLAN, Students.EnterGradeLV,
Students.EndGradeLV, Students.EntryGPAScale, Students.CumGPAEntry,
Students.EndGPAScale, Students.HSGPA1, Students.HSGPA2, Students.CollExamCD,
OtherInfo.NCESSchNM, OtherInfo.MiddleNM, OtherInfo.CurrentGradeLV,
OtherInfo.CurrentHSNM, Students.PicturePath
FROM Students LEFT JOIN OtherInfo ON Students.SSN = OtherInfo.SSN
WHERE (((Students.LastSerDT) Is Null)) OR (((OtherInfo.Served)=Yes)) OR
((([Forms]![frmStudents]![CheckCriteria])=Yes))
ORDER BY Students.LastNM, Students.FirstNM;

If [Forms]![frmStudents]![CheckCriteria]=No then it shows records WHERE
Students.LastSerDT Is Null OR OtherInfo.Served=Yes. (This is the default
when the form is opened.) If [Forms]![frmStudents]! [CheckCriteria]=Yes then
it shows every record. It works just like I wanted.
When I click the CheckCriteria checkbox this is called:

Private Sub CheckCriteria_Click()
'if checkbox = yes then shows all students, else shows current/active
Me.Requery
Me.ComboStudent.Requery
ShowName = ComboStudent.Column(2)
End Sub

The ComboStudent combobox gets its information from the same query so that
it shows the correct records.

However, now the user wants the option of showing students with a particular
HSGradYr. I have added a textbox called txtGradYear to my form for the user
to enter a year, but I don't know where to go from here.

I'm basically wanting this one query to act in 3 different ways.

1. WHERE Students.LastSerDT Is Null OR OtherInfo.Served=Yes

2. WHERE [Forms]![frmStudents]![CheckCriteria]=Yes

3. WHERE OtherInfo.HSGradYr=[forms]![frmStudents]! [txtGradYear]

And I want the ComboStudent to display the appropriate entries for each
WHERE.

I've got it working the first 2 ways, but I cannot figure out how to get the
3rd way to work. I tried:

WHERE (Students.LastSerDT Is Null OR OtherInfo.Served=Yes) OR
([Forms]![frmStudents]![CheckCriteria]=Yes) OR
(OtherInfo.HSGradYr=[forms]![frmStudents]![txtGradYear])

but I didn't get JUST the records where
OtherInfo.HSGradYr=[forms]![frmStudents]![txtGradYear] which makes sense.

Can anyone help me get what I need?

Thanks in advance,
Debbie


.
 
S

Salad

DebbieG said:
I have a form based on this query:


SELECT Students.LastSerDT, OtherInfo.Served, OtherInfo.HSGradYr,
OtherInfo.ActivePart, OtherInfo.Served, Students.SSN, [LastNM] & ", " &
[FirstNM] & " " & [MI] AS Name, Students.LastNM, Students.FirstNM,
Students.MI, Students.DOB, Students.GenderCD, Students.EthnicityCD,
Students.EligibilityCD, Students.UBInitiative, Students.NCESSchID,
Students.ProjEntryDT, Students.ProjReEntDT, Students.LastSerDT,
Students.Reason, Students.PartCD, Students.PartLV, Students.NeedCD,
Students.AssessCD, Students.PSAT, Students.PLAN, Students.EnterGradeLV,
Students.EndGradeLV, Students.EntryGPAScale, Students.CumGPAEntry,
Students.EndGPAScale, Students.HSGPA1, Students.HSGPA2, Students.CollExamCD,
OtherInfo.NCESSchNM, OtherInfo.MiddleNM, OtherInfo.CurrentGradeLV,
OtherInfo.CurrentHSNM, Students.PicturePath
FROM Students LEFT JOIN OtherInfo ON Students.SSN = OtherInfo.SSN
WHERE (((Students.LastSerDT) Is Null)) OR (((OtherInfo.Served)=Yes)) OR
((([Forms]![frmStudents]![CheckCriteria])=Yes))
ORDER BY Students.LastNM, Students.FirstNM;

If [Forms]![frmStudents]![CheckCriteria]=No then it shows records WHERE
Students.LastSerDT Is Null OR OtherInfo.Served=Yes. (This is the default
when the form is opened.) If [Forms]![frmStudents]![CheckCriteria]=Yes then
it shows every record. It works just like I wanted.
When I click the CheckCriteria checkbox this is called:

Private Sub CheckCriteria_Click()
'if checkbox = yes then shows all students, else shows current/active
Me.Requery
Me.ComboStudent.Requery
ShowName = ComboStudent.Column(2)
End Sub

The ComboStudent combobox gets its information from the same query so that
it shows the correct records.

However, now the user wants the option of showing students with a particular
HSGradYr. I have added a textbox called txtGradYear to my form for the user
to enter a year, but I don't know where to go from here.

I'm basically wanting this one query to act in 3 different ways.

1. WHERE Students.LastSerDT Is Null OR OtherInfo.Served=Yes

2. WHERE [Forms]![frmStudents]![CheckCriteria]=Yes

3. WHERE OtherInfo.HSGradYr=[forms]![frmStudents]![txtGradYear]

And I want the ComboStudent to display the appropriate entries for each
WHERE.

I've got it working the first 2 ways, but I cannot figure out how to get the
3rd way to work. I tried:

WHERE (Students.LastSerDT Is Null OR OtherInfo.Served=Yes) OR
([Forms]![frmStudents]![CheckCriteria]=Yes) OR
(OtherInfo.HSGradYr=[forms]![frmStudents]![txtGradYear])

but I didn't get JUST the records where
OtherInfo.HSGradYr=[forms]![frmStudents]![txtGradYear] which makes sense.

Can anyone help me get what I need?

Thanks in advance,
Debbie
Sometimes what I might do is create a rowsource for a combo without a
filter. Ex:
Select EmpID, EmployeeID from Employees;

I may have a invisible field, I'll call it RowSourceString, on the form
to hold am text value string. When I open the form I enter some code in
the OnOpen event similar to the following

Sub Form_OnOpen
'get the combo boxes rowsource. has no filter or orderby
Me.RowSourceString = Me.ComboboxName.RowSource

'remove the semi-colon if it exists since a ; terminates
'a SQL statement.
If right(Me.RowSourceString,1) = ";" Then
Me.RowSourceString = Left(Me.RowSourceString,Len(Me.RowSourceString)-1)
Endif
End Sub

Now whenever values change I can put something in the afterupdate event
of that field to create the filter. Let's say the code field has changed.
Sub Code_AfterUpdate
Dim strWhere As String

'create the filter. Remember...quotes around
'text fields, # around dates, nothing around numbers
StrWHere = "Where Code = " & Me.Code
'you can add more filtering to this string

'now update the combo box rowsource
Me.ComboBoxName.RowSource = Me.RowSourceString & _
strWhere & " Order By Whatever"
End Sub
 
D

DebbieG

DebbieG said:
I have a form based on this query:


SELECT Students.LastSerDT, OtherInfo.Served, OtherInfo.HSGradYr,
OtherInfo.ActivePart, OtherInfo.Served, Students.SSN, [LastNM] & ", " &
[FirstNM] & " " & [MI] AS Name, Students.LastNM, Students.FirstNM,
Students.MI, Students.DOB, Students.GenderCD, Students.EthnicityCD,
Students.EligibilityCD, Students.UBInitiative, Students.NCESSchID,
Students.ProjEntryDT, Students.ProjReEntDT, Students.LastSerDT,
Students.Reason, Students.PartCD, Students.PartLV, Students.NeedCD,
Students.AssessCD, Students.PSAT, Students.PLAN, Students.EnterGradeLV,
Students.EndGradeLV, Students.EntryGPAScale, Students.CumGPAEntry,
Students.EndGPAScale, Students.HSGPA1, Students.HSGPA2, Students.CollExamCD,
OtherInfo.NCESSchNM, OtherInfo.MiddleNM, OtherInfo.CurrentGradeLV,
OtherInfo.CurrentHSNM, Students.PicturePath
FROM Students LEFT JOIN OtherInfo ON Students.SSN = OtherInfo.SSN
WHERE (((Students.LastSerDT) Is Null)) OR (((OtherInfo.Served)=Yes)) OR
((([Forms]![frmStudents]![CheckCriteria])=Yes))
ORDER BY Students.LastNM, Students.FirstNM;

If [Forms]![frmStudents]![CheckCriteria]=No then it shows records WHERE
Students.LastSerDT Is Null OR OtherInfo.Served=Yes. (This is the default
when the form is opened.) If [Forms]![frmStudents]![CheckCriteria]=Yes then
it shows every record. It works just like I wanted.
When I click the CheckCriteria checkbox this is called:

Private Sub CheckCriteria_Click()
'if checkbox = yes then shows all students, else shows current/active
Me.Requery
Me.ComboStudent.Requery
ShowName = ComboStudent.Column(2)
End Sub

The ComboStudent combobox gets its information from the same query so that
it shows the correct records.

However, now the user wants the option of showing students with a particular
HSGradYr. I have added a textbox called txtGradYear to my form for the user
to enter a year, but I don't know where to go from here.

I'm basically wanting this one query to act in 3 different ways.

1. WHERE Students.LastSerDT Is Null OR OtherInfo.Served=Yes

2. WHERE [Forms]![frmStudents]![CheckCriteria]=Yes

3. WHERE OtherInfo.HSGradYr=[forms]![frmStudents]![txtGradYear]

And I want the ComboStudent to display the appropriate entries for each
WHERE.

I've got it working the first 2 ways, but I cannot figure out how to get the
3rd way to work. I tried:

WHERE (Students.LastSerDT Is Null OR OtherInfo.Served=Yes) OR
([Forms]![frmStudents]![CheckCriteria]=Yes) OR
(OtherInfo.HSGradYr=[forms]![frmStudents]![txtGradYear])

but I didn't get JUST the records where
OtherInfo.HSGradYr=[forms]![frmStudents]![txtGradYear] which makes sense.

Can anyone help me get what I need?

Thanks in advance,
Debbie
Sometimes what I might do is create a rowsource for a combo without a
filter. Ex:
Select EmpID, EmployeeID from Employees;

I may have a invisible field, I'll call it RowSourceString, on the form
to hold am text value string. When I open the form I enter some code in
the OnOpen event similar to the following

Sub Form_OnOpen
'get the combo boxes rowsource. has no filter or orderby
Me.RowSourceString = Me.ComboboxName.RowSource

'remove the semi-colon if it exists since a ; terminates
'a SQL statement.
If right(Me.RowSourceString,1) = ";" Then
Me.RowSourceString = Left(Me.RowSourceString,Len(Me.RowSourceString)-1)
Endif
End Sub

Now whenever values change I can put something in the afterupdate event
of that field to create the filter. Let's say the code field has changed.
Sub Code_AfterUpdate
Dim strWhere As String

'create the filter. Remember...quotes around
'text fields, # around dates, nothing around numbers
StrWHere = "Where Code = " & Me.Code
'you can add more filtering to this string

'now update the combo box rowsource
Me.ComboBoxName.RowSource = Me.RowSourceString & _
strWhere & " Order By Whatever"
End Sub
***********************************************************************

Thanks so much for your response. I couldn't get my query to work the way I
wanted but you led me in the right path. This is what I did:

Private Sub txtGradYear_Exit(Cancel As Integer)
Dim x As Variant
x = DCount("*", "OtherInfo", "HSGradYr = " & Me.txtGradYear)
If x = 0 Then
msg1 = "There are no students in this graduation year."
Response = MsgBox(msg1, vbExclamation, conTitle)
DoCmd.GoToControl "txtGradYear"
Exit Sub
End If

Me.CheckCurrent = False 'based on qselStudents
Me.CheckCriteria = False 'based on qselStudents

Forms!frmStudents.RecordSource = "qselStudents2"

Dim strNewSource
strNewSource = "SELECT qselStudents2.SSN, " _
& "Format([SSN],""000-00-0000"") AS Expr1, " _
& "qselStudents2.Name " _
& "FROM qselStudents2;"
Me.ComboStudent.RowSourceType = "Table/Query"
Me.ComboStudent.RowSource = strNewSource

Me.Requery
Me.ComboStudent.Requery
ShowName = ComboStudent.Column(2)
End Sub

Is putting this in _Exit the best way to go with this? If not, what do you
recommend?

Thanks so much,
Debbie
 
S

Salad

DebbieG said:
DebbieG wrote:

I have a form based on this query:


SELECT Students.LastSerDT, OtherInfo.Served, OtherInfo.HSGradYr,
OtherInfo.ActivePart, OtherInfo.Served, Students.SSN, [LastNM] & ", " &
[FirstNM] & " " & [MI] AS Name, Students.LastNM, Students.FirstNM,
Students.MI, Students.DOB, Students.GenderCD, Students.EthnicityCD,
Students.EligibilityCD, Students.UBInitiative, Students.NCESSchID,
Students.ProjEntryDT, Students.ProjReEntDT, Students.LastSerDT,
Students.Reason, Students.PartCD, Students.PartLV, Students.NeedCD,
Students.AssessCD, Students.PSAT, Students.PLAN, Students.EnterGradeLV,
Students.EndGradeLV, Students.EntryGPAScale, Students.CumGPAEntry,
Students.EndGPAScale, Students.HSGPA1, Students.HSGPA2,
Students.CollExamCD,

OtherInfo.NCESSchNM, OtherInfo.MiddleNM, OtherInfo.CurrentGradeLV,
OtherInfo.CurrentHSNM, Students.PicturePath
FROM Students LEFT JOIN OtherInfo ON Students.SSN = OtherInfo.SSN
WHERE (((Students.LastSerDT) Is Null)) OR (((OtherInfo.Served)=Yes)) OR
((([Forms]![frmStudents]![CheckCriteria])=Yes))
ORDER BY Students.LastNM, Students.FirstNM;

If [Forms]![frmStudents]![CheckCriteria]=No then it shows records WHERE
Students.LastSerDT Is Null OR OtherInfo.Served=Yes. (This is the default
when the form is opened.) If [Forms]![frmStudents]![CheckCriteria]=Yes
then

it shows every record. It works just like I wanted.
When I click the CheckCriteria checkbox this is called:

Private Sub CheckCriteria_Click()
'if checkbox = yes then shows all students, else shows current/active
Me.Requery
Me.ComboStudent.Requery
ShowName = ComboStudent.Column(2)
End Sub

The ComboStudent combobox gets its information from the same query so that
it shows the correct records.

However, now the user wants the option of showing students with a
particular

HSGradYr. I have added a textbox called txtGradYear to my form for the
user

to enter a year, but I don't know where to go from here.

I'm basically wanting this one query to act in 3 different ways.

1. WHERE Students.LastSerDT Is Null OR OtherInfo.Served=Yes

2. WHERE [Forms]![frmStudents]![CheckCriteria]=Yes

3. WHERE OtherInfo.HSGradYr=[forms]![frmStudents]![txtGradYear]

And I want the ComboStudent to display the appropriate entries for each
WHERE.

I've got it working the first 2 ways, but I cannot figure out how to get
the

3rd way to work. I tried:

WHERE (Students.LastSerDT Is Null OR OtherInfo.Served=Yes) OR
([Forms]![frmStudents]![CheckCriteria]=Yes) OR
(OtherInfo.HSGradYr=[forms]![frmStudents]![txtGradYear])

but I didn't get JUST the records where
OtherInfo.HSGradYr=[forms]![frmStudents]![txtGradYear] which makes sense.

Can anyone help me get what I need?

Thanks in advance,
Debbie

Sometimes what I might do is create a rowsource for a combo without a
filter. Ex:
Select EmpID, EmployeeID from Employees;

I may have a invisible field, I'll call it RowSourceString, on the form
to hold am text value string. When I open the form I enter some code in
the OnOpen event similar to the following

Sub Form_OnOpen
'get the combo boxes rowsource. has no filter or orderby
Me.RowSourceString = Me.ComboboxName.RowSource

'remove the semi-colon if it exists since a ; terminates
'a SQL statement.
If right(Me.RowSourceString,1) = ";" Then
Me.RowSourceString = Left(Me.RowSourceString,Len(Me.RowSourceString)-1)
Endif
End Sub

Now whenever values change I can put something in the afterupdate event
of that field to create the filter. Let's say the code field has changed.
Sub Code_AfterUpdate
Dim strWhere As String

'create the filter. Remember...quotes around
'text fields, # around dates, nothing around numbers
StrWHere = "Where Code = " & Me.Code
'you can add more filtering to this string

'now update the combo box rowsource
Me.ComboBoxName.RowSource = Me.RowSourceString & _
strWhere & " Order By Whatever"
End Sub
***********************************************************************

Thanks so much for your response. I couldn't get my query to work the way I
wanted but you led me in the right path. This is what I did:

Private Sub txtGradYear_Exit(Cancel As Integer)
Dim x As Variant
x = DCount("*", "OtherInfo", "HSGradYr = " & Me.txtGradYear)
If x = 0 Then
msg1 = "There are no students in this graduation year."
Response = MsgBox(msg1, vbExclamation, conTitle)
DoCmd.GoToControl "txtGradYear"
Exit Sub
End If
You could put the code from the Dim to Endif in the BeforeUpdate event.
I have used code like the following in the past
IF x = 0 then
DoCmd.RunCommand acCmdUndo
SendKeys "{ESC}"
Cancel = True
End If

This will clear out the data that was entered by using the SendKeys. Or
you could simply state
Cancel = True

Then put the rest of the code in the AfterUpdate event. You would want
to check in the AfterUpdate that year is not blank though.

Does qselStudents2 filter on the Year? IOW, does it contain a query to
do the filter you need?

BTW, You don't need to requery if you create a new rowsource.

Oftentimes I do something like
Select qselStudents2.SSN, _
Format([SSN],""000-00-0000"") AS [Soc Sec]
because I turn column headings on. Displaying Expr1 can confuse people.
Just a hint for the future if you turn column headings on

Me.CheckCurrent = False 'based on qselStudents
Me.CheckCriteria = False 'based on qselStudents

Forms!frmStudents.RecordSource = "qselStudents2"

Dim strNewSource
strNewSource = "SELECT qselStudents2.SSN, " _
& "Format([SSN],""000-00-0000"") AS Expr1, " _
& "qselStudents2.Name " _
& "FROM qselStudents2;"
Me.ComboStudent.RowSourceType = "Table/Query"
Me.ComboStudent.RowSource = strNewSource

Me.Requery
Me.ComboStudent.Requery
ShowName = ComboStudent.Column(2)
End Sub

Is putting this in _Exit the best way to go with this? If not, what do you
recommend?

I always do what works. If it works for you then that is fine. I do
most of my error validation in BeforeUpdate and execute pass code in the
AfterUpdate.

Ain't debugging fun?
 
D

DebbieG

Thank you!
Debbie

DebbieG said:
DebbieG wrote:

I have a form based on this query:


SELECT Students.LastSerDT, OtherInfo.Served, OtherInfo.HSGradYr,
OtherInfo.ActivePart, OtherInfo.Served, Students.SSN, [LastNM] & ", " &
[FirstNM] & " " & [MI] AS Name, Students.LastNM, Students.FirstNM,
Students.MI, Students.DOB, Students.GenderCD, Students.EthnicityCD,
Students.EligibilityCD, Students.UBInitiative, Students.NCESSchID,
Students.ProjEntryDT, Students.ProjReEntDT, Students.LastSerDT,
Students.Reason, Students.PartCD, Students.PartLV, Students.NeedCD,
Students.AssessCD, Students.PSAT, Students.PLAN, Students.EnterGradeLV,
Students.EndGradeLV, Students.EntryGPAScale, Students.CumGPAEntry,
Students.EndGPAScale, Students.HSGPA1, Students.HSGPA2,
Students.CollExamCD,

OtherInfo.NCESSchNM, OtherInfo.MiddleNM, OtherInfo.CurrentGradeLV,
OtherInfo.CurrentHSNM, Students.PicturePath
FROM Students LEFT JOIN OtherInfo ON Students.SSN = OtherInfo.SSN
WHERE (((Students.LastSerDT) Is Null)) OR (((OtherInfo.Served)=Yes)) OR
((([Forms]![frmStudents]![CheckCriteria])=Yes))
ORDER BY Students.LastNM, Students.FirstNM;

If [Forms]![frmStudents]![CheckCriteria]=No then it shows records WHERE
Students.LastSerDT Is Null OR OtherInfo.Served=Yes. (This is the default
when the form is opened.) If [Forms]![frmStudents]![CheckCriteria]=Yes
then

it shows every record. It works just like I wanted.
When I click the CheckCriteria checkbox this is called:

Private Sub CheckCriteria_Click()
'if checkbox = yes then shows all students, else shows current/active
Me.Requery
Me.ComboStudent.Requery
ShowName = ComboStudent.Column(2)
End Sub

The ComboStudent combobox gets its information from the same query so that
it shows the correct records.

However, now the user wants the option of showing students with a
particular

HSGradYr. I have added a textbox called txtGradYear to my form for the
user

to enter a year, but I don't know where to go from here.

I'm basically wanting this one query to act in 3 different ways.

1. WHERE Students.LastSerDT Is Null OR OtherInfo.Served=Yes

2. WHERE [Forms]![frmStudents]![CheckCriteria]=Yes

3. WHERE OtherInfo.HSGradYr=[forms]![frmStudents]![txtGradYear]

And I want the ComboStudent to display the appropriate entries for each
WHERE.

I've got it working the first 2 ways, but I cannot figure out how to get
the

3rd way to work. I tried:

WHERE (Students.LastSerDT Is Null OR OtherInfo.Served=Yes) OR
([Forms]![frmStudents]![CheckCriteria]=Yes) OR
(OtherInfo.HSGradYr=[forms]![frmStudents]![txtGradYear])

but I didn't get JUST the records where
OtherInfo.HSGradYr=[forms]![frmStudents]![txtGradYear] which makes sense.

Can anyone help me get what I need?

Thanks in advance,
Debbie

Sometimes what I might do is create a rowsource for a combo without a
filter. Ex:
Select EmpID, EmployeeID from Employees;

I may have a invisible field, I'll call it RowSourceString, on the form
to hold am text value string. When I open the form I enter some code in
the OnOpen event similar to the following

Sub Form_OnOpen
'get the combo boxes rowsource. has no filter or orderby
Me.RowSourceString = Me.ComboboxName.RowSource

'remove the semi-colon if it exists since a ; terminates
'a SQL statement.
If right(Me.RowSourceString,1) = ";" Then
Me.RowSourceString = Left(Me.RowSourceString,Len(Me.RowSourceString)-1)
Endif
End Sub

Now whenever values change I can put something in the afterupdate event
of that field to create the filter. Let's say the code field has changed.
Sub Code_AfterUpdate
Dim strWhere As String

'create the filter. Remember...quotes around
'text fields, # around dates, nothing around numbers
StrWHere = "Where Code = " & Me.Code
'you can add more filtering to this string

'now update the combo box rowsource
Me.ComboBoxName.RowSource = Me.RowSourceString & _
strWhere & " Order By Whatever"
End Sub
***********************************************************************

Thanks so much for your response. I couldn't get my query to work the way I
wanted but you led me in the right path. This is what I did:

Private Sub txtGradYear_Exit(Cancel As Integer)
Dim x As Variant
x = DCount("*", "OtherInfo", "HSGradYr = " & Me.txtGradYear)
If x = 0 Then
msg1 = "There are no students in this graduation year."
Response = MsgBox(msg1, vbExclamation, conTitle)
DoCmd.GoToControl "txtGradYear"
Exit Sub
End If
You could put the code from the Dim to Endif in the BeforeUpdate event.
I have used code like the following in the past
IF x = 0 then
DoCmd.RunCommand acCmdUndo
SendKeys "{ESC}"
Cancel = True
End If

This will clear out the data that was entered by using the SendKeys. Or
you could simply state
Cancel = True

Then put the rest of the code in the AfterUpdate event. You would want
to check in the AfterUpdate that year is not blank though.

Does qselStudents2 filter on the Year? IOW, does it contain a query to
do the filter you need?

BTW, You don't need to requery if you create a new rowsource.

Oftentimes I do something like
Select qselStudents2.SSN, _
Format([SSN],""000-00-0000"") AS [Soc Sec]
because I turn column headings on. Displaying Expr1 can confuse people.
Just a hint for the future if you turn column headings on

Me.CheckCurrent = False 'based on qselStudents
Me.CheckCriteria = False 'based on qselStudents

Forms!frmStudents.RecordSource = "qselStudents2"

Dim strNewSource
strNewSource = "SELECT qselStudents2.SSN, " _
& "Format([SSN],""000-00-0000"") AS Expr1, " _
& "qselStudents2.Name " _
& "FROM qselStudents2;"
Me.ComboStudent.RowSourceType = "Table/Query"
Me.ComboStudent.RowSource = strNewSource

Me.Requery
Me.ComboStudent.Requery
ShowName = ComboStudent.Column(2)
End Sub

Is putting this in _Exit the best way to go with this? If not, what do you
recommend?

I always do what works. If it works for you then that is fine. I do
most of my error validation in BeforeUpdate and execute pass code in the
AfterUpdate.

Ain't debugging fun?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top