Results 1 to 2 of 2
  1. #1
    RangerAlex is offline Novice
    Windows 10 Office 365
    Join Date
    Dec 2021
    Posts
    4

    Insert Into / Append Query using Filtered VBA/SQL as Recordsource

    I wrote this code a while ago and am now picking up this project again; however, I'm not well versed in VBA, but at least then I was good enough to do some basic things. Here is an overview of what I am trying to accomplish:

    I have a primary form and a subform. The primary form has three combo boxes which filter the data on the subform through VBA code. The relevant portion of the code is shown below. My goal is to allow the user to filter the data through the combo boxes and then export the filtered results to a separate table through an append query. This will stage the filtered results to be exported to excel.

    I cannot figure out through VBA how to do this. It cannot be that difficult, but I have not been able to find a clear answer. The trick is I want the results to be filtered based on the combo boxes. All append or INSERT INTO articles I have found show how to do it using hard coded results.



    Any help would be greatly appreciated. I've been stuck on this for about two weeks or so.






    'Primary SQL Code that serves as the subforms recordsource

    Code:
    Private Sub BackdaterSQL()
    
    SQL = "SELECT tblBackdater.BackdaterID, tblBackdater.Backdater_Project0IDfk, tblBackdater.Backdater_Project1IDfk, tblBackdater.Backdater_Project2IDfk, tblBackdater.Backdater_Task, " _
    & "tblBackdater.DaysPriorIDfk_StatDate, tblBackdater.DaysPriorIDfk_DueDate, tblBackdater.DaysPriorIDfk_SnoozeUntil, tblBackdater.Backdater_DaysAhead_ReminderTime, tblBackdater.Backdater_OwnerIDfk, " _
    & "tblBackdater.Backdater_IncludeInExport, lkqDaysPrior.DaysPrior_Number FROM qryBackdater " _
    & WhereStatement & "ORDER BY lkqDaysPrior.DaysPrior_Number ASC;"
    
    Debug.Print SQL
    
    Me.frmEntry_sub.Form.RecordSource = SQL
    Me.frmEntry_sub.Form.Requery
    
    End Sub

    'Checks to see if combo boxes are activated and then combines result into a where clause
    Code:
    Private Sub ComboBoxFilter()
    Dim Part1cbo As String
    Dim Part2cbo As String
    Dim Part3cbo As String
    
    If Nz(Me.[cboProject0], "") <> "" Then
    Part1cbo = "tblBackdater.Backdater_Project0IDfk=" & Me.[cboProject0] & " And "
    Else
    Part1cbo = ""
    End If
    
    If Nz(Me.[cboProject1], "") <> "" Then
    Part2cbo = "tblBackdater.Backdater_Project1IDfk=" & Me.[cboProject1] & " And "
    Else
    Part2cbo = ""
    End If
    
    If Nz(Me.[cboProject2], "") <> "" Then
    Part3cbo = "tblBackdater.Backdater_Project2IDfk=" & Me.[cboProject2] & " And "
    Else
    Part3cbo = ""
    End If
    
    strcboWhere = Part1cbo & Part2cbo & Part3cbo
    
    If Nz(strcboWhere, "") <> "" Then
    strcboWhere = Left(strcboWhere, Len(strcboWhere) - 5)
    Else
    strcboWhere = ""
    End If
    
    WhereStatement = "Where (" & strcboWhere & ")"
    
    Debug.Print WhereStatement
    
    End Sub

  2. #2
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,706
    Unless you have a global variable defined for WhereStatement, one sub has no idea of the other sub's value given to it. Try using a tempvars as indicated, as tempvar values are available to all subs and functions.
    It also appears that you do not have OPTION EXPLICIT as the second line in the form code module. (strcbowhere is not dimmed)

    Quote Originally Posted by RangerAlex View Post
    I wrote this code a while ago and am now picking up this project again; however, I'm not well versed in VBA, but at least then I was good enough to do some basic things. Here is an overview of what I am trying to accomplish:

    I have a primary form and a subform. The primary form has three combo boxes which filter the data on the subform through VBA code. The relevant portion of the code is shown below. My goal is to allow the user to filter the data through the combo boxes and then export the filtered results to a separate table through an append query. This will stage the filtered results to be exported to excel.

    I cannot figure out through VBA how to do this. It cannot be that difficult, but I have not been able to find a clear answer. The trick is I want the results to be filtered based on the combo boxes. All append or INSERT INTO articles I have found show how to do it using hard coded results.

    Any help would be greatly appreciated. I've been stuck on this for about two weeks or so.






    'Primary SQL Code that serves as the subforms recordsource

    Code:
    Private Sub BackdaterSQL()
    
    SQL = "SELECT tblBackdater.BackdaterID, tblBackdater.Backdater_Project0IDfk, tblBackdater.Backdater_Project1IDfk, tblBackdater.Backdater_Project2IDfk, tblBackdater.Backdater_Task, " _
    & "tblBackdater.DaysPriorIDfk_StatDate, tblBackdater.DaysPriorIDfk_DueDate, tblBackdater.DaysPriorIDfk_SnoozeUntil, tblBackdater.Backdater_DaysAhead_ReminderTime, tblBackdater.Backdater_OwnerIDfk, " _
    & "tblBackdater.Backdater_IncludeInExport, lkqDaysPrior.DaysPrior_Number FROM qryBackdater " _
    & Tempvars!tvWhereStatement & "ORDER BY lkqDaysPrior.DaysPrior_Number ASC;"
    
    Debug.Print SQL
    
    Me.frmEntry_sub.Form.RecordSource = SQL
    Me.frmEntry_sub.Form.Requery
    
    End Sub

    'Checks to see if combo boxes are activated and then combines result into a where clause
    Code:
    Private Sub ComboBoxFilter()
    Dim Part1cbo As String
    Dim Part2cbo As String
    Dim Part3cbo As String
    
    If Nz(Me.[cboProject0], "") <> "" Then
    Part1cbo = "tblBackdater.Backdater_Project0IDfk=" & Me.[cboProject0] & " And "
    Else
    Part1cbo = ""
    End If
    
    If Nz(Me.[cboProject1], "") <> "" Then
    Part2cbo = "tblBackdater.Backdater_Project1IDfk=" & Me.[cboProject1] & " And "
    Else
    Part2cbo = ""
    End If
    
    If Nz(Me.[cboProject2], "") <> "" Then
    Part3cbo = "tblBackdater.Backdater_Project2IDfk=" & Me.[cboProject2] & " And "
    Else
    Part3cbo = ""
    End If
    
    strcboWhere = Part1cbo & Part2cbo & Part3cbo
    
    If Nz(strcboWhere, "") <> "" Then
    strcboWhere = Left(strcboWhere, Len(strcboWhere) - 5)
    Else
    strcboWhere = ""
    End If
    
    Tempvars!tvWhereStatement = "Where (" & strcboWhere & ")"
    
    Debug.Print WhereStatement
    
    End Sub

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

Similar Threads

  1. Replies: 3
    Last Post: 05-24-2021, 02:01 AM
  2. Replies: 4
    Last Post: 08-03-2019, 02:40 AM
  3. Replies: 4
    Last Post: 01-17-2016, 02:52 PM
  4. Replies: 9
    Last Post: 03-26-2015, 04:20 PM
  5. Replies: 12
    Last Post: 05-30-2013, 02:02 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