+ Reply to Thread
Results 1 to 7 of 7

Help with excel vba form sql cascade comboboxes

Hybrid View

  1. #1
    Registered User
    Join Date
    08-18-2022
    Location
    Brazil
    MS-Off Ver
    office 365
    Posts
    3

    Help with excel vba form sql cascade comboboxes

    Good afternoon, I'm having trouble getting a SQL string that integrates into 2 different comboboxes by pulling data from access, I'm falling into an infinite loop and I'm not able to solve it, can anyone help me?. Here is the code

    RS.Open "SELECT subcriterio_id, Subcriterio FROM tb_subcriterio WHERE criterio_id = " & cmb_criterio.Text & " ORDER BY Subcriterio", connection, adOpenKeyset, adLockReadOnly
            If RS.RecordCount > 0 Then
                Do Until RS.EOF
                    cmb_subcriterio.AddItem RS!subcriterio_id, Subcriterio
                    RS.MoveNext
                Loop
            End If
    criterio.PNG
    subcriterios.PNG
    design.PNG
    design2.PNG
    rela??es.PNG

  2. #2
    Forum Contributor
    Join Date
    02-25-2022
    Location
    Dallas, Texas
    MS-Off Ver
    Office 365
    Posts
    174

    Re: Help with excel vba form sql cascade comboboxes

    I think you want this:

    sql = ""
    sql = sql & "SELECT subcriterio_id, " & vbCrLf
    sql = sql & "       Subcriterio " & vbCrLf
    sql = sql & "FROM   tb_subcriterio AS sc " & vbCrLf
    sql = sql & "       INNER JOIN tb_criterios c " & vbCrLf
    sql = sql & "               ON sc.criterio_id = " & cmb_criterio.Text & vbCrLf
    sql = sql & "ORDER  BY Subcriterio"
    Last edited by mogul; 08-18-2022 at 12:26 PM.

  3. #3
    Registered User
    Join Date
    08-18-2022
    Location
    Brazil
    MS-Off Ver
    office 365
    Posts
    3

    Re: Help with excel vba form sql cascade comboboxes

    well thanks for your reply but i want to change this part "SELECT subcriterio_id, Subcriterio FROM tb_subcriterio WHERE criterio_id = " & cmb_criterio.Text & " ORDER BY Subcriterio" to mke it work i think this sql isn't correct cause when i execute it in the excel form i fall into a inifnite loop
    Last edited by valossa515; 08-18-2022 at 12:19 PM. Reason: grammar error

  4. #4
    Forum Contributor
    Join Date
    02-25-2022
    Location
    Dallas, Texas
    MS-Off Ver
    Office 365
    Posts
    174

    Re: Help with excel vba form sql cascade comboboxes

    Updated SQL. What happens when execute in Access? It needs to work in Access first.

    For Access

    SELECT subcriterio_id,
           Subcriterio
    FROM   tb_subcriterio AS sc
           INNER JOIN tb_criterios c
                   ON sc.criterio_id = c.criterio_id
           WHERE sc.criterio_id = 1 
    ORDER  BY Subcriterio
    For VBA

    sql = ""
    sql = sql & "SELECT subcriterio_id, " & vbCrLf
    sql = sql & "       Subcriterio " & vbCrLf
    sql = sql & "FROM   tb_subcriterio AS sc " & vbCrLf
    sql = sql & "       INNER JOIN tb_criterios c " & vbCrLf
    sql = sql & "               ON sc.criterio_id = c.criterio_id " & vbCrLf
    sql = sql & "WHERE  sc.criterio_id = 1 " & vbCrLf
    sql = sql & "ORDER  BY Subcriterio"
    
    Debug.Print sql
    Last edited by mogul; 08-18-2022 at 12:52 PM.

  5. #5
    Registered User
    Join Date
    08-18-2022
    Location
    Brazil
    MS-Off Ver
    office 365
    Posts
    3

    Re: Help with excel vba form sql cascade comboboxes

    ok WHERE sc.criterio_id = 1 " & vbCrL works great now how to change it to get my combobox text? cause when i try to replace 1 by my cmb_criterio.text the script breaks
    I tried this but not work
    sql = sql & "WHERE sc.criterio_id = " & cmb_criterio.Text & " " & vbCrLf

    this is my excel form

    cmb.PNG
    Last edited by valossa515; 08-18-2022 at 01:33 PM.

  6. #6
    Forum Contributor
    Join Date
    02-25-2022
    Location
    Dallas, Texas
    MS-Off Ver
    Office 365
    Posts
    174

    Re: Help with excel vba form sql cascade comboboxes

    Add a Debug.Print sql after the last query statement and see the value of sql in the Immediate window.

    sql = sql & "WHERE sc.criterio_id = " & cmb_criterio.Text & " " & vbCrLf

    It should be

    sql = sql & "WHERE sc.criterio_id = " & cmb_criterio & vbCrLf

  7. #7
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,719

    Re: Help with excel vba form sql cascade comboboxes

    crossposted: https://www.excelforum.com/excel-pro...omboboxes.html

    Rule 03: Cross-posting Without Telling Us

    Your post does not comply with Rule 3 of our Forum RULES. Do not cross-post your question on multiple forums without telling us about your threads on other forums.

    Post a link to any other forums where you have asked the same question.

    Cross-posting is when you post the same question in other forums on the web. The last thing you want to do is waste people's time working on an issue you have already resolved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser) to the cross-post.

    Read this to understand why we ask you to do this.

    I have added the crosspost reference for you today. Please comply with this and all our rules in the future
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

+ 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. Cascade drop down list excel linked
    By mak81 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-11-2023, 04:04 AM
  2. [SOLVED] VBA loop through form control comboboxes
    By lynnsong986 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 02-05-2020, 12:36 PM
  3. [SOLVED] On form, I need multiple comboboxes to pull different info
    By argentraven in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-31-2016, 04:51 PM
  4. User form dependant comboboxes
    By Vrhovni Bog in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-17-2013, 05:04 AM
  5. User Form with repetitive comboboxes
    By 00Able in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 01-22-2011, 03:08 PM
  6. Excel form comboboxes to pulling data from access
    By bob7777 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-10-2008, 08:45 PM
  7. Excel auto cascade Info
    By daisosasen01 in forum Excel - New Users/Basics
    Replies: 0
    Last Post: 08-22-2006, 01:50 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