Results 1 to 6 of 6
  1. #1
    Dan9684 is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Jan 2019
    Posts
    16

    Opening Form at specific record using multiple criteria

    Hi,

    I actually posted on this a while ago but got nowhere, had to drop it, but now I still can't fix it.

    I have a database, on which there is a menu, [frmMenu]. I have linked a table [tblMenuSearch] to this menu, it has fields [GIOSearch] and [MonthSearch] on it only.

    The two fields appear on the [frmMenu] as well as a button. I need to be able to fill in the two fields e.g. [GIOSearch] with a seven digit number i.e. 1234567 and the [MonthSearch] with i.e. 01/12/2018

    When these two are completed, pressing the button needs to bring up a record in another form [frmMain] (filled with data from [tblMain]) which along with those two fields, will bring a lot of associated data.

    Fields in [tblMenuSearch] - [GIOSearch] links to [GIOCode] on [tblMain] and [MonthSearch] links to [MonthEnding] on [tblMain]

    The current code on the button is:

    Private Sub Command5_Click()
    On Error GoTo Err_Command5_Click


    Dim stDocName As String
    Dim stLinkCriteria As String




    stDocName = "frmMain"

    stLinkCriteria = "[GIOCode]=" & Me![GIOSearch] & " And [MonthEnding]=#" & Me![MonthSearch] & "#"
    DoCmd.OpenForm stDocName, , , stLinkCriteria


    Exit_Command5_Click:
    Exit Sub


    Err_Command5_Click:
    MsgBox Err.Description
    Resume Exit_Command5_Click

    End Sub

    Can anyone see where I'm going wrong please?

    Thanks in advance.

  2. #2
    Dan9684 is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Jan 2019
    Posts
    16
    Attached database if that helps.
    Attached Files Attached Files

  3. #3
    Minty is offline VIP
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,004
    It's not working because in your frmMain on open event you are forcing the form to go to a new record.

    You also need to format the date correctly in VBA, change the line below. Read up here for why http://allenbrowne.com/ser-36.html


    Code:
        stLinkCriteria = "[GIOCode]=" & Me.GIOSearch & " And [MonthEnding]=#" & Format(Me.MonthSearch, "yyyy-mm-dd") & "#"
    Also note the use of Me. not Me! as well, this allows intellisense to kick in, and ensures you are referring to the forms objects.
    Finally add

    Code:
    Option Compare Database
    Option Explicit
    To the top of ALL your code modules, it will help with variable typos etc.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  4. #4
    Join Date
    May 2018
    Location
    Living in Scotland UK
    Posts
    1,581
    Hi

    I changed the Search Controls to be Combobox's Looking for the values stored in tblMain.
    I modified the Code on the Search button to use the following :-

    Code:
    Private Sub Search_Click()
    
    
        On Error GoTo Search_Click_Error
    Dim strCrit As String
    If Len(Me.txtGIO & vbNullString) > 0 Then
        strCrit = strCrit & "([GIOCode] = " & Me.txtGIO & ") AND "
      End If
    
    
    If IsDate(Me.txtMonth) Then
      strCrit = strCrit & "([MonthEnding] >= #" & Format(Me.txtMonth, "dd/mm/yyyy") & "#) AND "
    End If
    If strCrit > "" Then
      strCrit = Left(strCrit, Len(strCrit) - 5)
      
    DoCmd.OpenForm "frmSearch", acNormal, , strCrit
    
    
    
    
    End If
        
        On Error GoTo 0
        Exit Sub
    
    
    Search_Click_Error:
    
    
        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Search_Click, line " & Erl & "."
    
    
    End Sub
    Your tblMain has Multiple Repeating Groups which should I believe be normalised.
    Attached Files Attached Files

  5. #5
    Minty is offline VIP
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,004
    @Mike - that date format in the where clause will fail to filter correctly, you have to use either mm/dd/yyyy or preferably yyyy-mm-dd (It's so much easier to determine exactly what the date is that way around visually.)
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  6. #6
    Join Date
    May 2018
    Location
    Living in Scotland UK
    Posts
    1,581
    Hi Minty

    Yes you are right. I overlooked the format of the Date but it should as you say follow the USA Format for Dates as I have been told many times by the Guru's.

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

Similar Threads

  1. Replies: 7
    Last Post: 07-29-2018, 11:58 AM
  2. Opening Form to Specific Record Issue
    By Ganymede in forum Programming
    Replies: 2
    Last Post: 02-27-2016, 10:33 PM
  3. Replies: 5
    Last Post: 01-24-2015, 12:59 AM
  4. Opening Form to a Specific Record
    By PPat in forum Forms
    Replies: 9
    Last Post: 04-24-2013, 08:47 PM
  5. Help with opening form to specific record
    By manic in forum Programming
    Replies: 7
    Last Post: 09-18-2012, 08:44 PM

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