Results 1 to 5 of 5

Thread: Use a SQL statement to open form

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    Question Use a SQL statement to open form

    I'm trying to write a small SQL query that will allow me to open a form only with that person records. This is in Access 2003. The error I'm getting is a syntax error in the query

    VB Code:
    1. ' Dim variables used
    2.                Dim stDocName As String
    3.                Dim stLinkCriteria As String
    4.                ' Form name is 1486
    5.                 stDocName = "1486"
    6.                ' Find out who is selected in the Employee combo box and make SUPER (text box on 1486) equal to that person (so it will filter records by the person)
    7.                 stLinkCriteria = "[SUPER]=" & "'" & Me.cboEmployee & "'"
    8.                 ' Create query that selects only the records of that person
    9.                 sSQL = "SELECT * FROM 1486 WHERE stLinkCriteria"
    10.  
    11.                 ' Open the form
    12.                 DoCmd.OpenForm stDocName, , , sSQL

  2. #2
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Use a SQL statement to open form

    I'm presuming that 1486 is the name of the form.. you cannot perform SQL against a form.. if 1486 is the table then what does the value in cboEmployee return.. is this a string value or numeric?
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    Re: Use a SQL statement to open form

    Quote Originally Posted by dannymking
    I'm presuming that 1486 is the name of the form.. you cannot perform SQL against a form.. if 1486 is the table then what does the value in cboEmployee return.. is this a string value or numeric?
    It returns a string (John Doe), then I want the form to open with only the records that have the SUPER (feild name) of John Doe..

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Use a SQL statement to open form

    Try putting the value of the variable into the string, rather than the variable name
    VB Code:
    1. sSQL = "SELECT * FROM 1486 WHERE " & stLinkCriteria

  5. #5
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Use a SQL statement to open form

    Your error is here..

    Code:
    ' Open the form
    DoCmd.OpenForm stDocName, , , sSQL
    You are trying to open the form using the full SQL as the open argument.. Try just sending "[Super]=" & stLinkCriteria

    Or open the form in design view set the recordsource to the SQL, save and close it and then open the form normally
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width