Results 1 to 13 of 13
  1. #1
    geraldk is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2011
    Posts
    49

    Running a Select statement in VBA


    I have this select statement that I want to run in a form then pass the results when I open a report:

    "SELECT Max(tblPosition.POSID) AS MaxOfPOSID" & _
    "FROM tblPosition" & _
    "GROUP BY tblPosition.EMPID" & _
    "HAVING (((tblPosition.EMPID)=[Forms]![frmEmployee]![txtEMPID]));"

    Right now I have the report opening to match the employee ID that I see on the form.

    stDocName = "rptEmployee"
    stLinkCriteria = "[EmpID]=" & Me![txtEMPID] <- would like to add another criteria to it for the POSID from the select statement above.
    DoCmd.OpenReport stDocName, , acViewPreview, stLinkCriteria

    Thank you,

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,523
    You can open a recordset on that SQL, or switch to a DMax().
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    geraldk is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2011
    Posts
    49
    You mean open a recordset when I open the report?

  4. #4
    Paul H's Avatar
    Paul H is offline Expert
    Windows XP Access 2010 32bit
    Join Date
    Sep 2011
    Location
    Richmond, VA
    Posts
    591
    Couldn't you store the results of your select statement in a text box on your form then refer to it in strCriteria?

    stLinkCriteria = "[EmpID]=" & Me![txtEMPID] & " and [POSID] = " & me.txtMaxPosID

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,523
    Quote Originally Posted by geraldk View Post
    You mean open a recordset when I open the report?
    No, I mean open a recordset on that SQL to get the number, and then use the number in your code to open the report. You might find the DMax simpler.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,523
    Quote Originally Posted by Paul H View Post
    Couldn't you store the results of your select statement in a text box on your form then refer to it in strCriteria?

    stLinkCriteria = "[EmpID]=" & Me![txtEMPID] & " and [POSID] = " & me.txtMaxPosID
    Sure, but how are you going to get the results to the textbox?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    geraldk is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2011
    Posts
    49
    I made a list box on the form, then I need to highlight the first value in the list box. Made a separate query to get values that I am looking for. Now is list box I have the values

    9
    5
    4
    3

    but I need to select 9 to pass on to the report.

    Found some code here: http://www.access-programmers.co.uk/...d.php?t=138069

    Need to see if I can get it work for me.

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,523
    Try

    stLinkCriteria = "[EmpID]=" & Me![txtEMPID] & " And POSID = " & DMax("POSID", "tblPosition", "EMPID = " & [Forms]![frmEmployee]![txtEMPID])
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    Paul H's Avatar
    Paul H is offline Expert
    Windows XP Access 2010 32bit
    Join Date
    Sep 2011
    Location
    Richmond, VA
    Posts
    591
    I was going to use the DMax as the Control Source for the text box. It amounts to the same thing I think.

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,523
    That would work. When you said "store the results of your select statement in a text box" I thought you would have tried to make the SQL statement the Control Source, which wouldn't work.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    geraldk is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2011
    Posts
    49
    Here is the code I am working with:

    j = 0
    i = 0
    RecordCount01 = Me.lstMaxPosID.ListCount
    Do Until i = Me.lstMaxPosID.ListCount
    i = i + 1
    If Me.lstMaxPosID.Selected(i) Then j = i
    Loop
    If j = 0 Then
    Me.lstMaxPosID.Selected(i) = True
    End If
    strLinkPos = Me.lstMaxPosID.value


    So if I these values in list box (come from query and sort from the top value that I need)

    4
    5
    9
    6

    I need it highligt 4 and give me the value back as 4

  12. #12
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,523
    Did you try what I posted above? That's a convoluted way of getting the max (or min, if your desires have changed).
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  13. #13
    geraldk is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2011
    Posts
    49
    Found the code to make this work as I needed:

    Me.lstMaxPosID = Me.lstMaxPosID.ItemData(0)

    Just like to thank everyone who helped out me here.

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

Similar Threads

  1. running select query in form delete event
    By suki360 in forum Programming
    Replies: 0
    Last Post: 03-11-2011, 10:11 AM
  2. Need a select statement
    By gahawy in forum Queries
    Replies: 5
    Last Post: 01-15-2011, 04:02 PM
  3. Select query for running totals
    By asawadude in forum Queries
    Replies: 2
    Last Post: 10-07-2010, 03:41 PM
  4. select statement
    By jellybeannn in forum Access
    Replies: 5
    Last Post: 08-13-2010, 05:21 AM
  5. Select statement syntax?
    By ksmith in forum Programming
    Replies: 3
    Last Post: 06-24-2010, 09:21 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