Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2012
    Posts
    15

    Pass a variable from a form to a select query

    Hi,
    I need some help. I have a form (frmSelectManager) which uses a combo box to select a number. After selecting the number, you click a command button and it runs Private Sub cbuOK_Click() where the number selected is stored as a variable vMID (dim vMID as integer).
    I then I want to open a query using the variable vMID as the criteria for a field named ManagerID (note: the form remains open during this operation). I have tried [forms]![frmSelectManager]![vMID] in the criteria box, but it does not work. Can someone tell me what I am doing wrong?

    If it helps the sql is:
    SELECT tblPerformanceData.ManagerID, tblPerformanceData.DataTypeCode,
    FROM tblPerformanceData
    WHERE ((([tblPerformanceData].[managerid])=[forms]![frmSelectManager]![vmid]));

    Have I messed up with the brackets or exclamation point?




    Thanks.
    Gene

  2. #2
    davegri's Avatar
    davegri is offline Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Remove that comma after tblPerformanceData.DataTypeCode in the first line.

  3. #3
    Join Date
    Jan 2012
    Posts
    15
    Sorry for the delay in responding. Thank you for checking that out. The comma is just an editing error when I copied text for this post. I did confirm that it still did not work.
    Gene

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Use the combobox. Vmid is not part of the form.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Gene,
    You cannot use a local form variable as a parameter in a query. You can directly reference the control itself as suggested by Welshgasman in the previous post or you can use a tempvar or a custom VBA function that uses a global variable (so basically you declare vMID as Public in a standard module and have a function that returns its value):
    Code:
    Option Explicit
    Public vMID as Long
    Public Function fnMid() as Long
    fnMid=vMID 'the value of vMID is set in the click event of the button just as you have it now.
    End Function
    The SQL becomes:
    SELECT tblPerformanceData.ManagerID, tblPerformanceData.DataTypeCode,
    FROM tblPerformanceData
    WHERE [tblPerformanceData].[managerid]=fnMID();

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  6. #6
    Join Date
    Jan 2012
    Posts
    15
    Again, apologies for late response. Thank you both. Both solutions work. I appreciate the vba code that Gicu wrote out which makes it easy to insert it into my code, and Welshgasman's comment about using the combobox clarified my thinking about what a query could or couldn't use as criteria. I consider this thread solved and will try to figure out how to mark it as such.
    Gene

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

Similar Threads

  1. Pass a variable to a Pass Through Query
    By violeta_barajas in forum Access
    Replies: 2
    Last Post: 01-26-2017, 07:59 PM
  2. Replies: 5
    Last Post: 04-27-2015, 02:40 PM
  3. Replies: 5
    Last Post: 05-18-2012, 07:31 AM
  4. Replies: 15
    Last Post: 04-21-2011, 02:50 PM
  5. VBA Pass-through Query with Variable
    By smaumau in forum Programming
    Replies: 0
    Last Post: 12-06-2010, 09:10 AM

Tags for this Thread

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