Results 1 to 5 of 5
  1. #1
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    284

    Creating a Pass-Through Query Using Values From UI Controls

    Greetings...

    Today I am trying to create a pass-through query using variables which derive their values from controls on my userform.

    I am able to get the below to work which derives its values from a local SQL table
    Code:
    INSERT INTO tri.TktRqsts (ReqID, EmpID, EmpName, RsnForRqst, RqstdBy, RqstDate)
    SELECT tri.TmpTktRqsts.ReqID, tri.TmpTktRqsts.EmpID, tri.TmpTktRqsts.EmpName,
                tri.TmpTktRqsts.RsnForRqst, tri.TmpTktRqsts.RqstdBy, tri.TmpTktRqsts.RqstDate
    FROM tri.TmpTktRqsts;
    But when I try the same idea using variables what I get is the literal string instead of the string value


    fwiw - what I am doing is running the code in Access to set the variable values then running the pass-through query outside of the Access VBA Code.

    This is the PThrough query that sends the literal string values - (which makes me think I'm close...but)
    Code:
    INSERT INTO tri.TktEmailTmp (EmpID, EmpName, TempAgency)
    VALUES('" & EmpId & "', '" & EmpName & "', '" & TAgncy & "');
    As always - Thank you very much...
    Last edited by RunTime91; 10-24-2018 at 06:46 AM. Reason: Changed Local Access to Local SQL

  2. #2
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    284
    I think I have solved this one - But would still like some advice & guidance if anyone see's anything wrong with my solution.

    And I do have a question which I will post at the end...

    Apparently, I need to create a QDef to accomplish this.

    Below is the QDef and the Function it runs
    Code:
    EmpId = Me!CmboAgentList.Column(1)
    EmpName = Me!CmboAgentList.Column(0)
            
    If Me.CmboAgentList.Column(2) = "" Then
      TAgncy = "FTE"
        Else
      TAgncy = "TEMP"
    End If
    Dim QDef As DAO.QueryDef
    Dim StrSQL As String
    Dim Boo As String
    
    Boo = "TktEmailTmp"
    StrSQL = "INSERT INTO tri.TktEmailTmp (EmpID, EmpName, TempAgency) " & _
                  "VALUES('" & EmpId & "', '" & EmpName & "', '" & TAgncy & "')"
     
    QDefTktBuild Boo, StrSQL
     
    DoCmd.OpenQuery ("TktEmailTmp")
    Code:
    Function QDefTktBuild(strQueryName As String, strQDefSQL As String)
    
    On Error GoTo ErrorHandler
       
        Dim qdfPassThrough As QueryDef
            
            If QryExists(strQueryName) = True Then
                CurrentDb.QueryDefs.Delete strQueryName
                GoTo BuildItNow
            Else
    BuildItNow:
               Set qdfPassThrough = CurrentDb.CreateQueryDef(strQueryName)
                With qdfPassThrough
                    .Connect = strConnection
                     .SQL = strQDefSQL
                     .ODBCTimeout = 60
                    .ReturnsRecords = False
                End With
            End If
        
    Exit Function
    ErrorHandler:
       'It's another error, display message
       MsgBox Err.Description
    
    End Function
    My question lies with using DoCmd.OpenQuery - In this case I do not want to 'Open' the query per say, I just want to 'Run' it
    But DoCmd.Run or DoCmd.Execute do not exist - Any Idea's

    Thank You...

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    You could try:

    CurrentDb.Execute "TktEmailTmp", dbFailOnError

    I personally wouldn't delete/recreate the query, I'd have it saved and just change its SQL.

    Edit: I should add I've used OpenQuery to execute an action query too, it works fine.
    Last edited by pbaldy; 10-24-2018 at 02:03 PM. Reason: fix spelling goof
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    284
    Paul - You're always out there for us rooks - And I certainly appreciate it

    Thank You, Sir

  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,641
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 11
    Last Post: 03-24-2016, 06:12 PM
  2. controls don't pass procedures consistently
    By martingaleh in forum Forms
    Replies: 3
    Last Post: 10-08-2015, 01:19 PM
  3. Replies: 12
    Last Post: 05-05-2014, 09:23 PM
  4. Replies: 13
    Last Post: 05-14-2013, 06:01 PM
  5. Replies: 3
    Last Post: 11-26-2010, 12:38 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