PC Review


Reply
Thread Tools Rate Thread

Criteria in DoCmd.OpenForm

 
 
=?Utf-8?B?SmFzb24=?=
Guest
Posts: n/a
 
      5th Oct 2004
I have a form with one combo box which allows the user to select a project
number. When they click on okay it opens up a detail screen in single form
mode that lets them scroll through records related to the project they chose.
It works great. Then I have a project summary Continuous Form that displays
the status of different parts of the project. I created a form with 3 combo
boxes to select different viewing options. However, this form doesn't work
at all. I need it to open the summary form displaying only the records
related to the options they chose. I am sure the problem is with my criteria
in the WHERE portion of the DoCmd.OpenForm method. Also, if the user does
not select one of the values, then I would like the form to ignore that
criteria when opening the form so as to NOT limit the records relating to the
other criteria. Here is my code. Thanks:

Private Sub cmdOk_Click()
Dim strProject As String
Dim strStatus As String
Dim strPass As String

' to avoid error on assigning null values
If IsNull(Me.cmbProject) Then
strProject = ""
Else
strProject = Me.cmbProject
End If

If IsNull(Me.cmbStatus) Then
strStatus = ""
Else
strStatus = Me.cmbStatus
End If

If IsNull(Me.cmbPass) Then
strPass = ""
Else
strPass = Me.cmbPass
End If

DoCmd.OpenForm "frmVolumeSummary", , , "Project = '" & strProject & "'" & _
"VolumeStatus = '" & strStatus & "'" & "PassStatus = '" & strPass & "'"

DoCmd.Close acForm, Me.Name
End Sub


 
Reply With Quote
 
 
 
 
=?Utf-8?B?SmFzb24=?=
Guest
Posts: n/a
 
      5th Oct 2004
Thanks for any that were looking into this for me. I was able to come up
with a solution using as guidance Allen Browne's date range tips. I did not
realize that I would need to set up a variable for the where statement and
then run through multiple branching statements for the different criteria.
Here is the finished code for any interested. It works great.

Private Sub cmdOk_Click()
Dim strWhere As String

If IsNull(Me.cmbProject) Then
If IsNull(Me.cmbStatus) Then
If IsNull(Me.cmbPass) Then 'all three are null
strWhere = ""
Else 'Pass is only one chosen
strWhere = "PassStatus = '" & Me.cmbPass & "'"
End If
Else
If IsNull(Me.cmbPass) Then 'Status is only one chosen
strWhere = "VolumeStatus = '" & Me.cmbStatus & "'"
Else 'Status and Pass chosen
strWhere = "VolumeStatus = '" & Me.cmbStatus & "'" & " AND " & _
"PassStatus = '" & Me.cmbPass & "'"
End If
End If
Else
If IsNull(Me.cmbStatus) Then
If IsNull(Me.cmbPass) Then 'Project only one chosen
strWhere = "Project = '" & Me.cmbProject & "'"
Else 'Project and Pass chosen
strWhere = "Project = '" & Me.cmbProject & "'" & _
" AND " & "PassStatus = '" & Me.cmbPass & "'"
End If
Else
If IsNull(Me.cmbPass) Then 'Project and Status chosen
strWhere = "Project = '" & Me.cmbProject & "'" & _
" AND " & "VolumeStatus = '" & Me.cmbStatus & "'"
Else 'All are chosen
strWhere = "Project = '" & Me.cmbProject & "'" & _
" AND " & "VolumeStatus = '" & Me.cmbStatus & "'" & _
" AND " & "PassStatus = '" & Me.cmbPass & "'"
End If
End If
End If

DoCmd.OpenForm "frmVolumeSummary", , , strWhere

DoCmd.Close acForm, Me.Name
End Sub
 
Reply With Quote
 
 
 
 
Dirk Goldgar
Guest
Posts: n/a
 
      11th Oct 2004
"Jason" <(E-Mail Removed)> wrote in message
news:740E3AEC-2909-4A29-BB3B-(E-Mail Removed)
> I have a form with one combo box which allows the user to select a
> project number. When they click on okay it opens up a detail screen
> in single form mode that lets them scroll through records related to
> the project they chose. It works great. Then I have a project
> summary Continuous Form that displays the status of different parts
> of the project. I created a form with 3 combo boxes to select
> different viewing options. However, this form doesn't work at all.
> I need it to open the summary form displaying only the records
> related to the options they chose. I am sure the problem is with my
> criteria in the WHERE portion of the DoCmd.OpenForm method. Also, if
> the user does not select one of the values, then I would like the
> form to ignore that criteria when opening the form so as to NOT limit
> the records relating to the other criteria. Here is my code. Thanks:
>
> Private Sub cmdOk_Click()
> Dim strProject As String
> Dim strStatus As String
> Dim strPass As String
>
> ' to avoid error on assigning null values
> If IsNull(Me.cmbProject) Then
> strProject = ""
> Else
> strProject = Me.cmbProject
> End If
>
> If IsNull(Me.cmbStatus) Then
> strStatus = ""
> Else
> strStatus = Me.cmbStatus
> End If
>
> If IsNull(Me.cmbPass) Then
> strPass = ""
> Else
> strPass = Me.cmbPass
> End If
>
> DoCmd.OpenForm "frmVolumeSummary", , , "Project = '" & strProject &
> "'" & _ "VolumeStatus = '" & strStatus & "'" & "PassStatus = '" &
> strPass & "'"
>
> DoCmd.Close acForm, Me.Name
> End Sub


You need to include conjunctions in the WhereCondition argument you
build. Try it like this:

'----- start of revised code -----
Private Sub cmdOk_Click()

Dim strWhere As String

' to avoid error on assigning null values
If Not IsNull(Me.cmbProject) Then
strWhere = strWhere & _
" AND Project = '" & Me.cmbProject & "'"
End If

If Not IsNull(Me.cmbStatus) Then
strWhere = strWhere & _
" AND VolumeStatus = '" & Me.cmbStatus & "'"
End If

If Not IsNull(Me.cmbPass) Then
strWhere = strWhere & _
" AND PassStatus = '" & Me.cmbPass & "'"
End If

If Len(strWhere) > 0 Then
strWhere = Mid$(strWhere, 6)
End If

DoCmd.OpenForm "frmVolumeSummary", _
WhereCondition:=strWhere

DoCmd.Close acForm, Me.Name

End Sub
'----- end of revised code -----

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Counting Cells with multiple criteria.One criteria supporting wild =?Utf-8?B?QXpoYXIgU2FsZWVt?= Microsoft Excel Worksheet Functions 0 12th Jan 2005 08:51 AM
data type mismatch in criteria expression when no criteria specified JR Microsoft Access Queries 1 27th Jul 2004 03:47 AM
Re: count instances of one criteria withing results of another criteria Jason Morin Microsoft Excel Worksheet Functions 0 6th May 2004 08:59 PM
Re: have input box. Need criteria entered to refer to cond. format criteria Bob Phillips Microsoft Excel Programming 0 1st Mar 2004 09:17 PM
HELP! Change color of a control if criteria meets criteria in an unbound box Aileen Microsoft Access Forms 1 26th Sep 2003 07:49 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:43 AM.