Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    oly is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2013
    Posts
    18

    calling an MS access sql query in a button click

    Dear expert;



    Can you please paste the syntax on how to call an sql query in a button click event in MS access 2010 or can we use an embedded approach. if so, how. All help is appreciated.

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,743

  3. #3
    oly is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2013
    Posts
    18
    the link doesnt answer my question. Here is my questions again, I have a dropdown list contain colours and a button and I want a situation where if the button is clicked on, it shows a report comprising information about a colour. Does this have to be done in embedded macro or an event procedure, if so how. I am new to access but I have programmed in asp.net and asp before.

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,743
    You can use vba (event procedure) or a macro. I don't use macros.

    In the On Click event of the button,
    you would have code to Open the report in question, and you would pass the value in the combo(dropdown) as a parameter in the open report command.

    There is a wizard in Access. When you add a dropdown(combobox in Access terms), an automated tool is invoked and asks you questions. Similarly when you add a button, a number of questions/steps are invoked.
    You could say that you want to open a report when the button is clicked.
    It will give you the framework for the code involved.

    In very pseudo code, you would have something like

    YourButton_Click()
    Docmd.OpenReport "yourReportName",,, Me.ComboName.value

    Here is actual code that has a list of reports in a combo box, and an option to print or preview; and a button to Open the selected report in the ViewOption chosen,

    Code:
    Private Sub Command2_Click()
    10  On Error GoTo Err_Command2_Click
        Dim stDocName As String
        Dim iOption As Integer
    20  stDocName = Me.Combo0.Value
        'frame optPreview = 1  (Preview)
        'frame optPrint   = 2  (Normal)
    30  iOption = IIf(Me.Frame3 = 1, acViewPreview, acViewNormal)
    40  'Debug.Print iOption 'for debugging
    
    50  DoCmd.OpenReport stDocName, iOption
    Exit_Command2_Click:
    60  Exit Sub
    Err_Command2_Click:
    70  MsgBox Err.Description
    80  Resume Exit_Command2_Click
    End Sub
    Good luck.

  5. #5
    oly is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2013
    Posts
    18
    I did that but i am getting an error message which says user-defined type not defined. see my syntax below. kindly note, I am using access 2010

    Dim dbsA As Database
    Set dbsA = CurrentDb
    Dim strql As String


    strql = "select * from stores"

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,743
    ?? I'm a little confused.
    Your last post said
    Here is my questions again, I have a dropdown list contain colours and a button and I want a situation where if the button is clicked on, it shows a report comprising information about a colour.
    You can create a query, or you can open a report and base that report on a value selected from a combobox.

    What exactly do you want to do in plain English? and I will help.

  7. #7
    oly is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2013
    Posts
    18
    Hi this is my question once again, I have a dropdown list contain the following items: Red, Blue, Yellow. I want a situation where if the button is clicked on, it shows a report comprising information about the color.

    For instance..here is my table information below. The table is called tbl_colour

    ID Place
    Yellow NewYork
    Red Dallas
    Blue Mexico
    Yellow Toronto
    Yellow China

    So if yellow is picked from the dropdown list, it should show the following report below

    ID Place
    Yellow China
    Yellow Toronto
    Yellow NewYork.

    This is the code I have so far below but it is not working though

    Dim strql As String
    Dim temp As String
    temp = Me.Combo4.Value


    strSQL = "select * from tbl_colour "
    strSQL = strSQL & "where ID = '" & Me.Combo4.Value & "'"
    strSQL = strSQL & "order by place"




    Debug.Print strSQL
    MsgBox strSQL


    DoCmd.OpenReport "dbo_testreports", acViewPreview

    The code is not working though unfortunately, it only displays one item...it is also complaining about the size of the report...how can I modify it

  8. #8
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,743
    Oly,

    I am attaching a sample db that I created before I saw your last post.
    It has 3 tables Animal info, AnimalCapture info and Sighting Info.
    The query that is the data for the Report is based on the 3 tables.
    It has a form with a combo and a button.
    The combo lists the names of some Animals.
    Select an Animal, then click the button to Open the Report that is constrained by the combo selection.

    The report exists and you can open the report separately to see the format and content.

    I created the database in 2010 and saved in 2003 format.
    Attached Files Attached Files

  9. #9
    oly is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2013
    Posts
    18
    one more question...I get this message "The section width is greater than the page width and there are no items in the additional space, so some pages may be blank"....if I want to include an and condition in the where section as well...how do I do so...for example if i want my select to be

    strSQL = "select * from tbl_colour "

    strSQL = strSQL & "where ID = '" & Me.Combo4.Value & "'"
    strSQL = strSQL & "and Place != '" NewYork & "'"
    strSQL = strSQL & "order by place"

    thanks for your help so far

  10. #10
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,743
    That message is in reference to the width of your report -- and has been a pain for years.
    Take you sample report an try to adjust column widths. Did you get a message during design that says in an option - to adjust sizes to fit on a page? I think I saw that when creating a report with 2010.

    I always start my sql fragments with a space to avoid things running together (syntax errors)
    strSQL = "select * from tbl_colour "
    strSQL = strSQL & "where ID = '" & Me.Combo4.Value & "'"
    strSQL = strSQL & "and Place != '" NewYork & "'"
    strSQL = strSQL & "order by place"
    Code:
    strSQL = "select * from tbl_colour "
    strSQL = strSQL & " where ID = '" & Me.Combo4.Value & "'"
    strSQL = strSQL & " and Place != '"NewYork & "'"
    strSQL = strSQL & " order by place"
    
    You had a space before New York???

  11. #11
    oly is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2013
    Posts
    18
    yeah but what if I want to include the strSQL statement in the DoCmd.OpenReport. How do I do so...

  12. #12
    oly is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2013
    Posts
    18
    Also I am still getting a syntax error with the code...see code below


    strSQL = "select * from tbl_colour"strSQL = strSQL & "where ID = '" & Me.Combo4.Value & "'"strSQL = strSQL & "and Place != '" & NewYork & "'"strSQL = strSQL & "order by place"

  13. #13
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,743
    Quote Originally Posted by oly View Post
    yeah but what if I want to include the strSQL statement in the DoCmd.OpenReport. How do I do so...
    What/where do you see that sql fitting? I did find a reference, but I haven't tried it.
    Here's the link
    http://en.allexperts.com/q/Using-MS-...cordsource.htm

    When you post code snippets, you should use [ c o d e ] and [ / c o d e ] tags around the code ** no spaces**

  14. #14
    oly is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2013
    Posts
    18
    Quote Originally Posted by orange View Post
    What/where do you see that sql fitting? I did find a reference, but I haven't tried it.
    Here's the link
    http://en.allexperts.com/q/Using-MS-...cordsource.htm

    When you post code snippets, you should use [ c o d e ] and [ / c o d e ] tags around the code ** no spaces**
    so what is wrong with this syntax below because I keep getting an error message and I dont know why

    Code:
    
    strSQL = "select * from tbl_colour"
    strSQL = strSQL & "where ID = '" & Me.Combo4.Value & "'"
    strSQL = strSQL & "and Place != '" & NewYork & "'"
    strSQL = strSQL & "order by place"
    

  15. #15
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,743
    You could put a debug.print strSQL in your code. That will print the rendered SQL in the immediate window - excellent debugging technique. If you don't see anything obvious, you can copy the sql and paste it into the SQL View in the query designer. The syntax is good is the designer doesn't complain and the query executes.

    If it does fail on syntax, an error will be given.

    Is your field value "New York" or "NewYork"?

    Code:
    strSQL = "select * from tbl_colour" 
    strSQL = strSQL & "Xwhere ID = '" & Me.Combo4.Value & "'" 
    strSQL = strSQL & "Xand Place != 'NewYork'"
     strSQL = strSQL & "Xorder by place"
    I think you need to ensure there is a space where the X appears. Also, your bracketing around New York needs to make New York a string.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 11-18-2012, 10:41 PM
  2. Replies: 37
    Last Post: 09-15-2011, 11:57 AM
  3. Export Excel data to access on a button click
    By Grooz13 in forum Import/Export Data
    Replies: 0
    Last Post: 03-15-2011, 10:02 AM
  4. Calling Stored Proc in MS Access 2007 without creating query?
    By DistillingAccess in forum Programming
    Replies: 1
    Last Post: 08-03-2010, 09:38 AM
  5. Calling Query from different Access DB
    By Jerry8989 in forum Queries
    Replies: 4
    Last Post: 10-02-2009, 08:48 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