+ Reply to Thread
Results 1 to 2 of 2

Excel 365 VBA: Filter sql query with form

Hybrid View

  1. #1
    Registered User
    Join Date
    07-30-2021
    Location
    Germany
    MS-Off Ver
    Office 365
    Posts
    1

    Question Excel 365 VBA: Filter sql query with form

    Hi everyone,

    I have a sql query in my vba code.
    My data connection uses ado.
    The query result will be stored into my table.

    Now I want to use a form in order to set the date range and a name filter where I can chose a name or if not all names will be selected, see attached screenshot.
    Field 1 = start date, field 2 = end date and field 3 a list filter.


    form.jpg


    Let's say we have a sql query like:

       Dim cn As ADODB.Connection
        Dim rs As ADODB.Recordset
        Dim strCon As String
        
        
        
    Sub VPA_Report()
        
        Call fClearSheet(1)
    	strCon = "Driver={SQL Server Native Client 11.0}; Server=example;Database=test;User Id=a_user;Password=123456789;"
       
        Set cn = New ADODB.Connection
        Set rs = New ADODB.Recordset
        cn.Open strCon
    
        strSQL = "select emp_last_name, emp_first_name, hire_date from employe order by 1 asc"
                
        rs.Open strSQL, cn
               
        'insert data into worksheet
         
        Table.Range("A12:J12").CopyFromRecordset rs
        MsgBox "query has been done"
        rs.Close
        cn.Close
    
    end sub


    I already created the form but how to use the bind variable to my sql code? And how call this form?
    First the form has to be called that the user can insert a date or select a name.


    Thanks for any advice or example.

  2. #2
    Valued Forum Contributor ranman256's Avatar
    Join Date
    07-29-2012
    Location
    Kentucky
    MS-Off Ver
    Excel 2003
    Posts
    1,178

    Re: Excel 365 VBA: Filter sql query with form

    something like:

    if  isnull(cboBox) then
       strSQL = "select emp_last_name, emp_first_name, hire_date from employe order by 1 asc"
    else
       strSQL = cboBox
    endif
    
    If IsNull(txtStartDate) then
      sWhere = ""
    else
      sWhere = " where [dateFld] between #" & txtStartDate & "# and #" & txtEndDate & "#"
    endif
    
    strSql = strSql & sWhere
    rs.Open strSQL, cn

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. I'd like to create a Form to set criteria for MS Query in Excel 2003
    By charlylynn in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-19-2013, 04:23 AM
  2. Web Query - Change a word in query to form a new query
    By scottymelloty in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-25-2011, 04:13 AM
  3. Create a query/filter in excel
    By losdamianos in forum Excel - New Users/Basics
    Replies: 3
    Last Post: 10-19-2010, 04:25 AM
  4. query/filter form
    By akq125 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-09-2010, 06:58 PM
  5. Filter a Query by a Form win an operator
    By dgaller in forum Excel General
    Replies: 0
    Last Post: 02-22-2009, 05:16 PM
  6. Query a table and return results into my Excel Form
    By johncassell in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-15-2007, 07:41 AM
  7. Replies: 2
    Last Post: 07-30-2005, 01:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1