Results 1 to 12 of 12
  1. #1
    scor1pion is offline Novice
    Windows 10 Access 2016
    Join Date
    Jun 2016
    Posts
    22

    Question VBA Insert query using form selections

    I'm trying to perform an INSERT query through VBA.



    I make my choices through a form. (The form remains open through the process)

    Click image for larger version. 

Name:	Query Report Form.jpg 
Views:	14 
Size:	24.6 KB 
ID:	45615

    The query works correctly when I run as a standalone query. However, when I try to run through VBA, nothing happens, not even an error message or a pop up warning.

    Click image for larger version. 

Name:	ReverUserIDsForAccessLogTQ.jpg 
Views:	14 
Size:	85.9 KB 
ID:	45616

    Cannot get the INSERT to ReverUserAccessLogT query to work through VBA

    Any ideas?

    Code:
    Private Sub GenerateGroupReports_Click()
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim MyFileName As String
    Dim temp As String
    Dim sFolder As String
        
        ' Open the select folder prompt
        With Application.FileDialog(msoFileDialogFolderPicker)
            .Title = "Choose your save location"
            .ButtonName = "OK"
            If .Show = -1 Then ' if OK is pressed assign folder name to sFolder variable
                sFolder = .SelectedItems(1)
            Else
                Exit Sub
            End If
        End With
    
    Set db = CurrentDb()
    
    Set rs = db.OpenRecordset("SELECT DISTINCT ManagerName, Prod From GroupManagerForReportQ", dbOpenDynaset)
    
    Do While Not rs.EOF
        temp = rs("ManagerName")
        MyFileName = "Group Access to " & rs("Prod") & " Resources by Manager Report - " & rs("ManagerName") & ".PDF"
        
        DoCmd.OpenReport "GroupManagerR", acViewReport, , "[ManagerName]='" & temp & "'", acHidden
        DoCmd.OutputTo acOutputReport, "GroupManagerR", acFormatPDF, sFolder & "\" & MyFileName
        DoCmd.Close acReport, "GroupManagerR"
    
        rs.MoveNext
    Loop
    
            DoCmd.SetWarnings False
            
            ' This works INSERT INTO ReverReportDetailsT
            
            DoCmd.RunSQL "INSERT INTO ReverReportDetailsT ( MgrSerNum, SystemID, ProductionID, ReportSelectionID ) " & _
                    "SELECT DISTINCT GroupManagerForReportQ.MgrSerNum, GroupManagerForReportQ.SystemID, " & _
                    "GroupManagerForReportQ.Production, ""2"" AS Expr1 " & _
                    "FROM GroupManagerForReportQ;"
                    
            DoCmd.SetWarnings True  ' I turned warnings back on to see if I would get any messages, nothing.
            
            ' Neither of the below work (Yes, I remove the comment tags)
            ' I've commented out the options I've tried that don't work.  Keeping here so I know what I've already tried
                    
            'DoCmd.RunSQL "INSERT INTO ReverUserAccessLogT ( MgrSerNum, EmployeeID, UserID, SystemID, ProductionID ) " & _
            '        "SELECT DISTINCT MgrSerNum, EmployeeID, UserIDPK, SystemID, Production " & _
            '        "FROM ReverUserIDsForAccessLogTQ;"
            
            'DoCmd.RunSQL "INSERT INTO ReverUserAccessLogT ( MgrSerNum, EmployeeID, UserID, SystemID, ProductionID ) " & _
            '        "VALUES (""" & MgrSerNum & """, EmployeeID, UserIDPK, """ & SystemID & """, """ & Production & """)" & _
            '        " FROM ReverUserIDsForAccessLogTQ;"
            
            'DoCmd.RunSQL "INSERT INTO ReverUserAccessLogT ( MgrSerNum, EmployeeID, UserID, SystemID, ProductionID ) " & _
            '        "VALUES (""" & MgrSerNum & """, """ & EmployeeID & """, """ & UserIDPK & """, """ & SystemID & """, """ & Production & """)" & _
            '        " FROM ReverUserIDsForAccessLogTQ;"
            
            'DoCmd.OpenQuery "ReverUserIDsForAccessLogTQ"
    
    Set rs = Nothing
    Set db = Nothing
    
    End Sub

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    If fields are number type, don't use delimiters. I prefer apostrophe to doubled quote marks - easier to read.
    Code:
    "VALUES ('" & MgrSerNum & "', " & EmployeeID & ", " & UserIDPK & ", " & SystemID & ", " & Production & ")" & _
    Are these references to fields or controls of form? Why use VBA and not just data entry to bound form?

    I usually use CurrentDb.Execute then don't need SetWarnings.

    Why do you need Eval() function in query?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    I don't immediatly spot any problems with your code. I've made some changes, can you run it and report what, if any, errors you get?

    Code:
    Private Sub GenerateGroupReports_Click()
    On Error GoTo ErrHandler
    
        Dim db As DAO.Database
        Dim rs As DAO.Recordset
        Dim MyFileName As String
        Dim temp As String
        Dim sFolder As String
        Dim qry As String
        
        ' Open the select folder prompt
        With Application.FileDialog(msoFileDialogFolderPicker)
            .Title = "Choose your save location"
            .ButtonName = "OK"
            If .Show = -1 Then ' if OK is pressed assign folder name to sFolder variable
                sFolder = .SelectedItems(1)
            Else
                Exit Sub
            End If
        End With
        
        Set db = CurrentDb()
        Set rs = db.OpenRecordset("SELECT DISTINCT ManagerName, Prod From GroupManagerForReportQ", dbOpenDynaset)
        
        If Not (rs.BOF And rs.EOF) Then 'make sure your results actually returned something or you'll get an error
            rs.MoveFirst
            Do While Not rs.EOF
                temp = rs("ManagerName")
                MyFileName = "Group Access to " & rs("Prod") & " Resources by Manager Report - " & rs("ManagerName") & ".PDF"
                
                DoCmd.OpenReport "GroupManagerR", acViewReport, , "[ManagerName]='" & temp & "'", acHidden
                DoCmd.OutputTo acOutputReport, "GroupManagerR", acFormatPDF, sFolder & "\" & MyFileName
                DoCmd.Close acReport, "GroupManagerR"
            
                rs.MoveNext
            Loop
        End If
        
        rs.Close
        
        'DoCmd.SetWarnings False
        
        ' This works INSERT INTO ReverReportDetailsT
        
        'DoCmd.RunSQL "INSERT INTO ReverReportDetailsT ( MgrSerNum, SystemID, ProductionID, ReportSelectionID ) " & _
        '        "SELECT DISTINCT GroupManagerForReportQ.MgrSerNum, GroupManagerForReportQ.SystemID, " & _
        '        "GroupManagerForReportQ.Production, ""2"" AS Expr1 " & _
        '        "FROM GroupManagerForReportQ;"
                
                
        'use db.Execute rather than RunSQL so you don't have to risk turning warnings off and on
        qry = "INSERT INTO ReverReportDetailsT ( MgrSerNum, SystemID, ProductionID, ReportSelectionID ) " & _
                "SELECT DISTINCT GroupManagerForReportQ.MgrSerNum, GroupManagerForReportQ.SystemID, " & _
                "GroupManagerForReportQ.Production, ""2"" AS Expr1 " & _
                "FROM GroupManagerForReportQ;"
        db.Execute qry, dbFailOnError
                
        'DoCmd.SetWarnings True  ' I turned warnings back on to see if I would get any messages, nothing.
        
        ' Neither of the below work (Yes, I remove the comment tags)
        ' I've commented out the options I've tried that don't work.  Keeping here so I know what I've already tried
        
        qry = "INSERT INTO ReverUserAccessLogT ( MgrSerNum, EmployeeID, UserID, SystemID, ProductionID ) " & _
                "SELECT DISTINCT MgrSerNum, EmployeeID, UserIDPK, SystemID, Production " & _
                "FROM ReverUserIDsForAccessLogTQ;"
        db.Execute qry, dbFailOnError
        
        qry = "INSERT INTO ReverUserAccessLogT ( MgrSerNum, EmployeeID, UserID, SystemID, ProductionID ) " & _
                "VALUES (""" & MgrSerNum & """, EmployeeID, UserIDPK, """ & SystemID & """, """ & Production & """)" & _
                " FROM ReverUserIDsForAccessLogTQ;"
        db.Execute qry, dbFailOnError
        
        qry = "INSERT INTO ReverUserAccessLogT ( MgrSerNum, EmployeeID, UserID, SystemID, ProductionID ) " & _
                "VALUES (""" & MgrSerNum & """, """ & EmployeeID & """, """ & UserIDPK & """, """ & SystemID & """, """ & Production & """)" & _
                " FROM ReverUserIDsForAccessLogTQ;"
        db.Execute qry, dbFailOnError
        
        'DoCmd.OpenQuery "ReverUserIDsForAccessLogTQ"
        
    ExitHandler:
        Set rs = Nothing
        Set db = Nothing
        Exit Sub
        
    ErrHandler:
        MsgBox Err.Description & vbCrLf & vbCrLf & "SQL:" & vbCrLf & qry, , "Error #" & Err.Number
        Resume ExitHandler
        
    End Sub

  4. #4
    scor1pion is offline Novice
    Windows 10 Access 2016
    Join Date
    Jun 2016
    Posts
    22
    Quote Originally Posted by June7 View Post
    If fields are number type, don't use delimiters. I prefer apostrophe to doubled quote marks - easier to read.
    Code:
    "VALUES ('" & MgrSerNum & "', " & EmployeeID & ", " & UserIDPK & ", " & SystemID & ", " & Production & ")" & _
    Are these references to fields or controls of form? Why use VBA and not just data entry to bound form?

    I usually use CurrentDb.Execute then don't need SetWarnings.

    Why do you need Eval() function in query?
    Are these references to fields or controls of form? Why use VBA and not just data entry to bound form?
    System ID and Production are fields on the form. Yes form remains open.

    MgSerNum = Text
    EmployeeID = Number
    UserIDPK = Number
    SystemID = Number, however its from a text box on the form
    Production = Number, however its from the radio button selection box on the form


    I find that if I don't use Eval() function when getting data from a form in a query used in VBA they don't work. I have tried with and without, no difference in outcome.


    I also just tried the CurrentDb.Execute Didn't work either, again, not even a warning or error message.

  5. #5
    scor1pion is offline Novice
    Windows 10 Access 2016
    Join Date
    Jun 2016
    Posts
    22
    Quote Originally Posted by kd2017 View Post
    I don't immediatly spot any problems with your code. I've made some changes, can you run it and report what, if any, errors you get?

    Code:
    Private Sub GenerateGroupReports_Click()
    On Error GoTo ErrHandler
    
        Dim db As DAO.Database
        Dim rs As DAO.Recordset
        Dim MyFileName As String
        Dim temp As String
        Dim sFolder As String
        Dim qry As String
        
        ' Open the select folder prompt
        With Application.FileDialog(msoFileDialogFolderPicker)
            .Title = "Choose your save location"
            .ButtonName = "OK"
            If .Show = -1 Then ' if OK is pressed assign folder name to sFolder variable
                sFolder = .SelectedItems(1)
            Else
                Exit Sub
            End If
        End With
        
        Set db = CurrentDb()
        Set rs = db.OpenRecordset("SELECT DISTINCT ManagerName, Prod From GroupManagerForReportQ", dbOpenDynaset)
        
        If Not (rs.BOF And rs.EOF) Then 'make sure your results actually returned something or you'll get an error
            rs.MoveFirst
            Do While Not rs.EOF
                temp = rs("ManagerName")
                MyFileName = "Group Access to " & rs("Prod") & " Resources by Manager Report - " & rs("ManagerName") & ".PDF"
                
                DoCmd.OpenReport "GroupManagerR", acViewReport, , "[ManagerName]='" & temp & "'", acHidden
                DoCmd.OutputTo acOutputReport, "GroupManagerR", acFormatPDF, sFolder & "\" & MyFileName
                DoCmd.Close acReport, "GroupManagerR"
            
                rs.MoveNext
            Loop
        End If
        
        rs.Close
        
        'DoCmd.SetWarnings False
        
        ' This works INSERT INTO ReverReportDetailsT
        
        'DoCmd.RunSQL "INSERT INTO ReverReportDetailsT ( MgrSerNum, SystemID, ProductionID, ReportSelectionID ) " & _
        '        "SELECT DISTINCT GroupManagerForReportQ.MgrSerNum, GroupManagerForReportQ.SystemID, " & _
        '        "GroupManagerForReportQ.Production, ""2"" AS Expr1 " & _
        '        "FROM GroupManagerForReportQ;"
                
                
        'use db.Execute rather than RunSQL so you don't have to risk turning warnings off and on
        qry = "INSERT INTO ReverReportDetailsT ( MgrSerNum, SystemID, ProductionID, ReportSelectionID ) " & _
                "SELECT DISTINCT GroupManagerForReportQ.MgrSerNum, GroupManagerForReportQ.SystemID, " & _
                "GroupManagerForReportQ.Production, ""2"" AS Expr1 " & _
                "FROM GroupManagerForReportQ;"
        db.Execute qry, dbFailOnError
                
        'DoCmd.SetWarnings True  ' I turned warnings back on to see if I would get any messages, nothing.
        
        ' Neither of the below work (Yes, I remove the comment tags)
        ' I've commented out the options I've tried that don't work.  Keeping here so I know what I've already tried
        
        qry = "INSERT INTO ReverUserAccessLogT ( MgrSerNum, EmployeeID, UserID, SystemID, ProductionID ) " & _
                "SELECT DISTINCT MgrSerNum, EmployeeID, UserIDPK, SystemID, Production " & _
                "FROM ReverUserIDsForAccessLogTQ;"
        db.Execute qry, dbFailOnError
        
        qry = "INSERT INTO ReverUserAccessLogT ( MgrSerNum, EmployeeID, UserID, SystemID, ProductionID ) " & _
                "VALUES (""" & MgrSerNum & """, EmployeeID, UserIDPK, """ & SystemID & """, """ & Production & """)" & _
                " FROM ReverUserIDsForAccessLogTQ;"
        db.Execute qry, dbFailOnError
        
        qry = "INSERT INTO ReverUserAccessLogT ( MgrSerNum, EmployeeID, UserID, SystemID, ProductionID ) " & _
                "VALUES (""" & MgrSerNum & """, """ & EmployeeID & """, """ & UserIDPK & """, """ & SystemID & """, """ & Production & """)" & _
                " FROM ReverUserIDsForAccessLogTQ;"
        db.Execute qry, dbFailOnError
        
        'DoCmd.OpenQuery "ReverUserIDsForAccessLogTQ"
        
    ExitHandler:
        Set rs = Nothing
        Set db = Nothing
        Exit Sub
        
    ErrHandler:
        MsgBox Err.Description & vbCrLf & vbCrLf & "SQL:" & vbCrLf & qry, , "Error #" & Err.Number
        Resume ExitHandler
        
    End Sub

    All of this works, I have no errors with this section
    Code:
    Set db = CurrentDb()
    
    Set rs = db.OpenRecordset("SELECT DISTINCT ManagerName, Prod From GroupManagerForReportQ", dbOpenDynaset)
    
    Do While Not rs.EOF
        temp = rs("ManagerName")
        MyFileName = "Group Access to " & rs("Prod") & " Resources by Manager Report - " & rs("ManagerName") & ".PDF"
        
        DoCmd.OpenReport "GroupManagerR", acViewReport, , "[ManagerName]='" & temp & "'", acHidden
        DoCmd.OutputTo acOutputReport, "GroupManagerR", acFormatPDF, sFolder & "\" & MyFileName
        DoCmd.Close acReport, "GroupManagerR"
    
        rs.MoveNext
    Loop
    
            DoCmd.SetWarnings False
            
            ' This works INSERT INTO ReverReportDetailsT
            
            DoCmd.RunSQL "INSERT INTO ReverReportDetailsT ( MgrSerNum, SystemID, ProductionID, ReportSelectionID ) " & _
                    "SELECT DISTINCT GroupManagerForReportQ.MgrSerNum, GroupManagerForReportQ.SystemID, " & _
                    "GroupManagerForReportQ.Production, ""2"" AS Expr1 " & _
                    "FROM GroupManagerForReportQ;"

    This is the code not working
    Code:
            DoCmd.RunSQL "INSERT INTO ReverUserAccessLogT ( MgrSerNum, EmployeeID, UserID, SystemID, ProductionID ) " & _
                    "SELECT DISTINCT MgrSerNum, EmployeeID, UserIDPK, SystemID, Production " & _
                    "FROM ReverUserIDsForAccessLogTQ;"

  6. #6
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Quote Originally Posted by scor1pion View Post
    I find that if I don't use Eval() function when getting data from a form in a query used in VBA they don't work. I have tried with and without, no difference in outcome.


    I also just tried the CurrentDb.Execute Didn't work either, again, not even a warning or error message.
    Before anything I would validate all the input forms. Something like:
    Code:
    If Not IsNumeric(Me.textbox) Then Err.Raise 1001, , "The textbox ain't right. Go back and try again... ya jabroni "
    Test for all possible dumb user input, including empty textboxes.

    Have you tried converting your textbox values to numbers (longs) like this instead of eval?:
    Code:
    CLng(Me.textbox)

  7. #7
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Quote Originally Posted by scor1pion View Post
    All of this works, I have no errors with this section
    Code:
    Set db = CurrentDb()
    
    Set rs = db.OpenRecordset("SELECT DISTINCT ManagerName, Prod From GroupManagerForReportQ", dbOpenDynaset)
    
    Do While Not rs.EOF
        temp = rs("ManagerName")
        MyFileName = "Group Access to " & rs("Prod") & " Resources by Manager Report - " & rs("ManagerName") & ".PDF"
        
        DoCmd.OpenReport "GroupManagerR", acViewReport, , "[ManagerName]='" & temp & "'", acHidden
        DoCmd.OutputTo acOutputReport, "GroupManagerR", acFormatPDF, sFolder & "\" & MyFileName
        DoCmd.Close acReport, "GroupManagerR"
    
        rs.MoveNext
    Loop
    
            DoCmd.SetWarnings False
            
            ' This works INSERT INTO ReverReportDetailsT
            
            DoCmd.RunSQL "INSERT INTO ReverReportDetailsT ( MgrSerNum, SystemID, ProductionID, ReportSelectionID ) " & _
                    "SELECT DISTINCT GroupManagerForReportQ.MgrSerNum, GroupManagerForReportQ.SystemID, " & _
                    "GroupManagerForReportQ.Production, ""2"" AS Expr1 " & _
                    "FROM GroupManagerForReportQ;"

    This is the code not working
    Code:
            DoCmd.RunSQL "INSERT INTO ReverUserAccessLogT ( MgrSerNum, EmployeeID, UserID, SystemID, ProductionID ) " & _
                    "SELECT DISTINCT MgrSerNum, EmployeeID, UserIDPK, SystemID, Production " & _
                    "FROM ReverUserIDsForAccessLogTQ;"
    Okay. Try the code I posted and let me know what happens.

  8. #8
    scor1pion is offline Novice
    Windows 10 Access 2016
    Join Date
    Jun 2016
    Posts
    22
    Quote Originally Posted by kd2017 View Post
    Okay. Try the code I posted and let me know what happens.
    Just tried, same results as my code. Reports generate, but table does not get updated. No error or warning messages/pop ups.

  9. #9
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Quote Originally Posted by scor1pion View Post
    System ID and Production are fields on the form. Yes form remains open.

    MgSerNum = Text
    EmployeeID = Number
    UserIDPK = Number
    SystemID = Number, however its from a text box on the form
    Production = Number, however its from the radio button selection box on the form.
    When to referring to the controls on the form where the code is being called use Me.TextboxName like this:
    Code:
    qry = "SELECT * FROM sometable WHERE EmployeeID = " & Me.EmployeeID
    As June mentions don't use quotes around number fields.

  10. #10
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,933
    What does the Select Distinct on its own produce?
    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

  11. #11
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Quote Originally Posted by scor1pion View Post
    Just tried, same results as my code. Reports generate, but table does not get updated. No error or warning messages/pop ups.
    You may want to post your database in a zip file for review.

  12. #12
    scor1pion is offline Novice
    Windows 10 Access 2016
    Join Date
    Jun 2016
    Posts
    22
    Hi All, thank you for your assistance. It boils to down to my own stupidity...like most things.

    I had the code in the wrong section...so wasn't even being called to run.

    Once I noticed and put it in the correct section I did encounter an error. All I had to do was switch the actual query from and Append query to a Select query and now all works good.

    Thank You All for your time!!!

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

Similar Threads

  1. insert query from form
    By helpityhelphelphelp in forum Queries
    Replies: 1
    Last Post: 05-18-2016, 07:35 PM
  2. Replies: 1
    Last Post: 03-23-2015, 10:13 AM
  3. INSERT query: insert new data only
    By drh in forum Access
    Replies: 2
    Last Post: 04-04-2014, 05:31 PM
  4. Query with Where (Multiple Selections)
    By braveali in forum Access
    Replies: 3
    Last Post: 03-05-2014, 05:17 AM
  5. Replies: 2
    Last Post: 01-24-2014, 02:26 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