Results 1 to 3 of 3
  1. #1
    sonics is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2015
    Posts
    2

    Unhappy Report Print Based on Search Form

    Hi all
    I have search form based on Allen Brown and report print command based on Allen Brown also. Everything works fine except the report print does not work what I want. I want to add more Criteria in the Print Command button in the search form with EmployeeID, Department and date ranges. One EmployeeID work in different department and dates. How do I print report based on only the current records displaying in the search form. The code I used below print all the records related to EmployeeID.
    Private Sub cmdPrint_Click()
    Dim strWhere As String

    If Me.Dirty Then 'Save any edits.
    Me.Dirty = False
    End If

    If Me.NewRecord Then 'Check there is a record to print


    MsgBox "Select a record to print"
    Else
    strWhere = "[ID] = " & Me.[ID]
    DoCmd.OpenReport "MyReport", acViewPreview, , strWhere
    End If
    End Sub

    I always appreciate all your helps.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    you can have many controls to pick from , then the vb will filter the ones used and open the report:
    Code:
    Public Sub btnReport_Click()
    Dim sSql As String, sWhere As String
    
        'the query is built depending on the various filters the user picks...
    If Not IsNull(cboState) Then sWhere = sWhere & " and [state]='" & cboState & "'"
    If Not IsNull(txtName) Then sWhere = sWhere & " and [Name]='" & txtName & "'"
    If Not IsNull(chkContact) Then sWhere = sWhere & " and [Contact]=" & chkContact.Value
    
        'remove 1st 'AND'
    sWhere = Mid(sWhere, 4)
    
        'open the query or report here!
    DOCMD.OPENREPORT "rMyReport",,sWhere
    
    End Sub

  3. #3
    sonics is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2015
    Posts
    2
    I followed your codes but I was not lucky. The search form I use continuous form, record source based on the crosstab query. Please help me I just start the vba.
    thank you

    Public Sub cmdPrint_Click()
    Dim sSql As String, strWhere As String
    Const conjetdate = "\#mm\/dd\/yyyy\#"
    sSql = "SELECT* FROM crosstabqry"

    If Not IsNull(txtEmployeeID) Then strWhere = strWhere & " and[EmployeeID]= '" & txtEmployeeID & "'"
    If Not IsNull(cboShift) Then strWhere = strWhere & " and[Shift]= '" & cboShift & “’”
    strWhere = Mid(strWhere, 4)
    DoCmd.OpenReport "CrosstabReport", acViewPreview, , strWhere
    -------------------------------------------------------------------------------------------------------------------------- -----------
    Private Sub cmdSearch_Click()
    Dim strWhere As String
    Dim lngLen As Long
    Const conjetdate = "\#mm\/dd\/yyyy\#"

    If Not IsNull(Me.txt EmployeeID) Then
    strWhere = strWhere & " ([EmployeeID] = " & Me.txtEmployeeID & ") AND "
    End If
    If Not IsNull(Me.txtStartDate) Then
    strWhere = strWhere & " ([Date] >= " & Format(Me.txtStartDate, conjetdate) & ") AND "
    End If
    If Not IsNull(Me.txtEndDate) Then
    strWhere = strWhere & " ([Date] < " & Format(Me.txtEndDate + 1, conjetdate) & ") AND "
    End If

    If Not IsNull(Me.cboShift) Then
    strWhere = strWhere & " ([Shift] = " & Me.cboShift & " ) AND "
    End If
    lngLen = Len(strWhere) - 5
    If lngLen <= 0 Then
    MsgBox "No Criteria", vbInformation, "Nothing to do."
    Else
    strWhere = Left$(strWhere, lngLen)
    Me.Filter = strWhere
    Me.FilterOn = True
    End If
    End Sub

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 08-06-2015, 03:26 PM
  2. Print specific report based on form check boxes
    By Eddy Sincere in forum Reports
    Replies: 3
    Last Post: 05-13-2015, 08:07 PM
  3. Replies: 1
    Last Post: 02-21-2015, 11:35 PM
  4. Replies: 3
    Last Post: 03-11-2013, 05:11 PM
  5. Replies: 2
    Last Post: 05-06-2012, 11:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums