Results 1 to 13 of 13
  1. #1
    sukhjinder's Avatar
    sukhjinder is offline Beginner
    Windows 10 Access 2016
    Join Date
    May 2016
    Posts
    43

    Post Splitting sql in vba

    Sir,




    I have made three option group as a search criterion. No1 and No 2 group is clear and simple with option. for No3 i am using a combo box with dropdown from table 3. On search click it shows the result displayed in the list box based upon the rowsource using all the three criteria. Further the No 3 table is having the records linked other options No1 and No 2. Table no 3 have two column autoid and feild1. In the feild1 column i created an id =1 feild=all so that if i search it shows all the items.

    I want that if me.cbooptionselect3=1 then
    me.lstbox.rowsource=select feild1,feild2......... & _
    where feild3=" & me.cbooptionselect3 & "
    sort by -------

    is there is any command or rule so that i split the sql into parts like
    if me.cbooptionselect3=1 then
    me.lstbox.rowsource=select feild1,feild2.........
    ABCD
    sort by -------

    PRIVATE SUB ABCD
    abcd=where feild3>52 and feild3<98
    END SUB

    I wand to use abcd number of times in the VBA.

    Request advice on this

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Suggest you just describe your need in plain, simple English --not quasi-code.

    You might consider the Select Case construct

  3. #3
    sukhjinder's Avatar
    sukhjinder is offline Beginner
    Windows 10 Access 2016
    Join Date
    May 2016
    Posts
    43
    Private Sub cmdsearch_Click()

    Select Case True
    Case Me.cboskill.Value = 1
    Me.lstbox.RowSource = "SELECT tbl_employe.empID_PK, tbl_level.level, tbl_employe.name, tbl_skill.skill, tbl_office.office, tbl_level.levelID_PK, tbl_skill.skillID_PK, tbl_office.officeID_PK " & _
    "FROM tbl_skill INNER JOIN (tbl_office INNER JOIN (tbl_level INNER JOIN tbl_employe ON tbl_level.levelID_PK = tbl_employe.levelID_FK) ON tbl_office.officeID_PK = tbl_employe.officeID_FK) ON tbl_skill.skillID_PK = tbl_employe.skillID_FK;"
    Case Me.cboskill.Value <> 1
    Me.lstbox.RowSource = "SELECT tbl_employe.empID_PK, tbl_level.level, tbl_employe.name, tbl_skill.skill, tbl_office.office, tbl_level.levelID_PK, tbl_skill.skillID_PK, tbl_office.officeID_PK" & _
    "FROM tbl_skill INNER JOIN (tbl_office INNER JOIN (tbl_level INNER JOIN tbl_employe ON tbl_level.levelID_PK = tbl_employe.levelID_FK) ON tbl_office.officeID_PK = tbl_employe.officeID_FK) ON tbl_skill.skillID_PK = tbl_employe.skillID_FK" & _
    "WHERE ((tbl_skill.skillID_PK)=" & Me.cboskill.Value & ");"
    Case Else
    MsgBox "ALL THE BEST"
    End Select
    End Sub



    This code is not working where the combo box value is not equal to 1.dborg.zip

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Tell us in plain English what your database is about. What exactly is the business/purpose of this database?
    As someone who doesn't know you or the database, how am I suppose to interpret your form?
    What exactly do you want the form user to do?
    What should be selected?
    What is the expected result?
    What is the actual result?
    How do expected and actual differ?

  5. #5
    sukhjinder's Avatar
    sukhjinder is offline Beginner
    Windows 10 Access 2016
    Join Date
    May 2016
    Posts
    43
    Quote Originally Posted by orange View Post
    Tell us in plain English what your database is about. What exactly is the business/purpose of this database?
    As someone who doesn't know you or the database, how am I suppose to interpret your form?
    What exactly do you want the form user to do?
    What should be selected?
    What is the expected result?
    What is the actual result?
    How do expected and actual differ?
    i am using SQL statement as the record source of the list box.
    I want that there is a print button on the from on selecting it will open the report.
    I am expecting that the newly opened report should have the same record source as the list box of the opened form.
    How to pass the parameter of form record source into report record source?.

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Hmm?So you have a form with a recordsource, and on that form is a listbox.
    See this for listbox rowsource info.

    Do you plan to select something(s) in the list box and then click the report form?
    If so, then the Click event will have to include some code to update/create/modify the record source of the report with the listbox selected item(s).
    Do not count on the report's record source to do sorting or grouping--reports have their own sort and group options.

  7. #7
    sukhjinder's Avatar
    sukhjinder is offline Beginner
    Windows 10 Access 2016
    Join Date
    May 2016
    Posts
    43
    No i have not plan to select something in the list box. i just want that all the items in the list box will move in the report by selecting button print on the form.

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Build a report and apply the 3 parameters to report when opening. Review http://allenbrowne.com/ser-62.html

    The second SQL statement needs a space at the end of each continued line.
    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.

  9. #9
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,407
    Code:
    Private Sub cmdsearch_Click()
    
    Select Case True
        Case Me.cboskill.Value = 1
            Me.lstbox.RowSource = "SELECT tbl_employe.empID_PK, tbl_level.level, tbl_employe.name, tbl_skill.skill, tbl_office.office, tbl_level.levelID_PK, tbl_skill.skillID_PK, tbl_office.officeID_PK " & _
            "FROM tbl_skill INNER JOIN (tbl_office INNER JOIN (tbl_level INNER JOIN tbl_employe ON tbl_level.levelID_PK = tbl_employe.levelID_FK) ON tbl_office.officeID_PK = tbl_employe.officeID_FK) ON tbl_skill.skillID_PK = tbl_employe.skillID_FK;"
        Case Me.cboskill.Value <> 1
            Me.lstbox.RowSource = "SELECT tbl_employe.empID_PK, tbl_level.level, tbl_employe.name, tbl_skill.skill, tbl_office.office, tbl_level.levelID_PK, tbl_skill.skillID_PK, tbl_office.officeID_PK " & _
            "FROM tbl_skill INNER JOIN (tbl_office INNER JOIN (tbl_level INNER JOIN tbl_employe ON tbl_level.levelID_PK = tbl_employe.levelID_FK) ON tbl_office.officeID_PK = tbl_employe.officeID_FK) ON tbl_skill.skillID_PK = tbl_employe.skillID_FK " & _
            "WHERE ((tbl_skill.skillID_PK)=" & Me.cboskill.Value & ");"
        Case Else
            MsgBox "ALL THE BEST"
    End Select
    End Sub
    Add the spaces at the red...

  10. #10
    sukhjinder's Avatar
    sukhjinder is offline Beginner
    Windows 10 Access 2016
    Join Date
    May 2016
    Posts
    43
    Quote Originally Posted by June7 View Post
    Build a report and apply the 3 parameters to report when opening.
    the report is working fine as a separate identity. I want to pass three argument as tempVar from list box of form to open a report where the rowsource of report after opening will be similar to that of forms!listbox.

  11. #11
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    "No i have not plan to select something in the list box. i just want that all the items in the list box will move in the report by selecting button print on the form."

    The report would have RecordSource that is the same unfiltered query or SQL statement as the listbox. Then apply same filter criteria to the report when it opens.

    Unless the listbox is used to make selection(s) that will be applied to report filter, it really has no relevance to this issue and is simply confusing anyone reading your question.

    I've never utilized TempVars. I also never use dynamic parameterized queries. Method described in Allen Browne link is my preference.
    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.

  12. #12
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    sukhjinder,

    Why tempVars? If you are selecting from the listbox on a form, could you not take those selections and modify the SQL for the reports record source? Perhaps OpenArgs?
    Perhaps you could post a copy of the database.

  13. #13
    sukhjinder's Avatar
    sukhjinder is offline Beginner
    Windows 10 Access 2016
    Join Date
    May 2016
    Posts
    43
    Quote Originally Posted by June7 View Post
    "No i have not plan to select something in the list box. i just want that all the items in the list box will move in the report by selecting button print on the form."

    The report would have RecordSource that is the same unfiltered query or SQL statement as the listbox. Then apply same filter criteria to the report when it opens.

    Unless the listbox is used to make selection(s) that will be applied to report filter, it really has no relevance to this issue and is simply confusing anyone reading your question.

    I've never utilized TempVars. I also never use dynamic parameterized queries. Method described in Allen Browne link is my preference.


    Thanks a lot you can mark it as solved query.

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

Similar Threads

  1. Splitting Database.
    By Esmatullaharifi in forum Access
    Replies: 2
    Last Post: 12-29-2014, 12:57 AM
  2. Splitting Database
    By Shadab Kamal in forum Database Design
    Replies: 2
    Last Post: 11-17-2014, 03:51 PM
  3. splitting db
    By slimjen in forum Database Design
    Replies: 1
    Last Post: 10-09-2013, 07:22 AM
  4. Importing after splitting DB
    By akshatagarwal93 in forum Import/Export Data
    Replies: 2
    Last Post: 08-08-2012, 01:48 AM
  5. What is after Splitting!!
    By watzmann in forum Access
    Replies: 3
    Last Post: 09-07-2010, 07:31 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