Results 1 to 12 of 12
  1. #1
    steve_odea is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Jul 2016
    Posts
    10

    Passing a Option Group value from a (Sub-)Form into a Select Query

    Hi There, I know that this is a common thread and I have managed to determine the syntax etc, but I am still having difficulty in passing the value of an Option Group into a select query and wonder if somebody might be able to shine some light on what is happening.

    I have a Form which contains an option group with two values (1 & 2), which has a default value of 1 and is called [Frame54].

    I have fed the value of the Option Group into a the criteria line of a select query using the following syntax:

    =[Forms]![frmMergeWorkMenu]![Frame54]

    The query runs fine, but only if the corresponding form is open, which kind makes sense, but the problem I have is that the open version of my form exists as a sub-form in a navigation menu, so when I run the query with the navigation menu open, I am prompted for a parameter value for [Forms]![frmMergeWorkMenu]![Frame54].

    My question is, do I need to reference [Frame54] through the navigation menu (I suspect I do)? If so what is the syntax I should be using?

    I have tried the following, but to no avail:

    =[Forms]![frmMain].[Form]![NavigationSubform]![frmMergeWorkMenu]![Frame54]
    and


    =[Forms]![frmMain]![NavigationSubform].[Form]![frmMergeWorkMenu]![Frame54]

    but neither work and I am struggling to find any reference material regarding subform reference online, so any suggestions would be very welcome.

    S

  2. #2
    steve_odea is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Jul 2016
    Posts
    10
    I have figured it out, the correct syntax is:

    =[Forms]![frmMain]![NavigationSubform].[Form]![Frame54]

    The problem I have now is that my VBA that runs off this Recordset no longer works and it falls over when the VBA code tries to Open the Recordset:

    Code:
        Dim db As Database
        Dim rs As Recordset    
    
    
        Set db = CurrentDb
        Set rs = db.OpenRecordset("qryMergeEzineCompleteMerges", dbOpenDynamic, dbSeeChanges)
    The VBA Code now no longer proceed past this last line, with the error message being Run-time error '3001': Invalid Argument

    Does anyone have any idea why the inclusion of the subform reference above might've cause my VBA to halt?

  3. #3
    steve_odea is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Jul 2016
    Posts
    10
    So through my investigations I have discovered that my VBA code won't work as it doesn't recognise the parameter that I introduced into my query i.e. it doesn't recognise [Forms]![frmMain]![NavigationSubform].[Form]![Frame54]

    So does anyone know what VBA method/code I should use in order to introduce this parameter into my code?

  4. #4
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Bring the SQL into VBA;
    Dim strSQL As String
    strSQL="Select.... WHERE fieldname=" & Me!Frame54 & ";"

    If this is running from the main form then change it to Me!NavigationSubform!Frame54

    Then in the Set rs change the query name to strSQL (no quotes).

  5. #5
    steve_odea is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Jul 2016
    Posts
    10
    Thank for this, do I need to reference a specific library to put sql in my code?

  6. #6
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    You aren't putting SQL into the code. The SQL is inside a string so is treated like a bunch of characters.

  7. #7
    steve_odea is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Jul 2016
    Posts
    10
    So this doesn't work I'm afraid. I am getting an error stating that Me is an invalid keyword.

    Here is the code as it stands, can you tell me if I've understood your advise correctly?

    Code:
    Dim db As Database
        Dim rs As Recordset
        Dim strSQL as String
        
        strSQL = "SELECT qryMergeCYCompletesWithDetail.Email, qryMergeCYCompletesWithDetail.EzineSent, IIf(IsNull([EzineSent]),2,1) AS [Option] 
    From qryMergeCYCompletesWithDetail 
    WHERE ((IIf(IsNull([EzineSent]),2,1))= " & Me![NavigationSubform]![Frame64] & ";"
                   
        Set db = CurrentDb
        Set rs = db.OpenRecordset("qryMergeEzineCompleteMerges")

  8. #8
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    If Me is invalid that means that this code is running as a stand-alone routine, not as part of a form. My mistake. Change it to Forms!formname!....

    In post # 1 it was called "Frame54", in post # 7 it is called "Frame64"...?

  9. #9
    steve_odea is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Jul 2016
    Posts
    10
    OK So I have the syntax correct and that is working fine, but the rest of my code is based upon the recordset that I am opening i.e.

    Set rs = db.OpenRecordset("qryMergeEzineCompleteMerges", dbOpenDynaset, dbSeeChanges)

    which does not include the filter defined in the string...

    How do I link the sqlStr that I have just defined to the remainder of the code?

  10. #10
    steve_odea is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Jul 2016
    Posts
    10
    For example the following VBA refers to the recordset rs and not strSQL

    Code:
            StudentID = Format(rs![NUIG Student Number], "00000000")
            RecipientMail = rs![Personal Email]
            RecipientName = rs![First Name]
            NUIGMail = rs![NUIG Email]
            SaveAsName = FPath & "RSEzine-" & StudentID & ".docx"

  11. #11
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Set rs = db.OpenRecordset(strSQL, dbOpenDynaset, dbSeeChanges)

  12. #12
    steve_odea is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Jul 2016
    Posts
    10
    Thanks Very much, you're a legend!

    I'm new to VBA so still really trying to get my head around the syntax

    Best wishes

    S

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

Similar Threads

  1. Replies: 9
    Last Post: 01-31-2017, 05:13 PM
  2. Replies: 6
    Last Post: 06-09-2015, 09:51 AM
  3. Option Group Query
    By SeaTigr in forum Queries
    Replies: 13
    Last Post: 04-23-2012, 12:49 PM
  4. Replies: 11
    Last Post: 01-25-2012, 09:46 AM
  5. option group in form
    By mawa4492 in forum Forms
    Replies: 1
    Last Post: 08-05-2009, 02:49 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