Results 1 to 6 of 6
  1. #1
    msp4422 is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Feb 2013
    Posts
    7

    Basic: Executing simple sql query to extract a value in vba

    Hi I am trying to execute a sql to fetch a value which will be source to a text box. Could you help me on how to write and execute sql's in vba?? tnx in advance.

    I tried below but it is giving 3163: the field is too small to accept the value..........

    Dim SS As String


    DoCmd.SS = "SELECT qryAgreement_Data.[Unique ID] FROM qryAgreement_Data WHERE qryAgreement_Data.[Unique ID]=311;"



    Me.Agreement_ID.Value = SS

    thank you.

  2. #2
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I think you made up the SS command there. You would have to open a recordset on that SQL to get the value. You may find DLookup() simpler:

    http://access.mvps.org/access/general/gen0018.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    If you are just getting the value from 1 field, it might be easier using a DLookup() function, but I have to ask if you are specifying the unique ID in the where clause why would you need the query to give you the unique ID field's value? Wouldn't it be 311?

    "SELECT qryAgreement_Data.[Unique ID] FROM qryAgreement_Data WHERE qryAgreement_Data.[Unique ID]=311;"

  4. #4
    msp4422 is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Feb 2013
    Posts
    7
    @[jzwp11]
    I wrote that just as a sample query.. My main query is something different.. I wanted to know how we can execute a sql query like min, max, distinct functions and then use that value to assign another textbox. Could you show small example code on the same. I am new to MS access and VBA so it will be great if you guys can give me a source to learn the basics. i tried in net but couldn't find any good link. Thanks for your help and patience.

  5. #5
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    I typically use ADO (ActiveX Data Object) recordsets to get data via a query in code. Here is a code snippet:


    Code:
     
    Dim cnn1 As ADODB.Connection
    Set cnn1 = CurrentProject.Connection
    Dim myrecset As New ADODB.Recordset
    myrecset.ActiveConnection = cnn1
    
    
    mySQL= "SELECT ..."
    
    myrecset.Open mySQL
    
    
    If myrecset.BOF And myrecset.EOF Then
        MsgBox "no records returned"
    Else
    Do Until myrecset.EOF
    
    'do stuff here in the loop
    
    
    myrecset.MoveNext
    Loop
    End if
    The code snippet above sets up the objects and opens a recordset and then loops through the recordset. Obviously, you would do something within the loop.

    This might be more than you need, and as I and Paul (pbaldy) mentioned, if you just want a couple of values from a record it would be easier using a DLookup() function.

  6. #6
    JrMontgom is offline Competent Performer
    Windows Vista Access 2010 32bit
    Join Date
    Sep 2012
    Location
    Vero Beach, FL USA
    Posts
    124

    Creating SQL string to use in VBA

    Quote Originally Posted by msp4422 View Post
    Hi I am trying to execute a sql to fetch a value which will be source to a text box. Could you help me on how to write and execute sql's in vba?? tnx in advance.

    I tried below but it is giving 3163: the field is too small to accept the value..........

    Dim SS As String


    DoCmd.SS = "SELECT qryAgreement_Data.[Unique ID] FROM qryAgreement_Data WHERE qryAgreement_Data.[Unique ID]=311;"



    Me.Agreement_ID.Value = SS

    thank you.


    Being very lazy and a poor typist I usually create the query with real values to test it and when I am happy with the results ask the Query Designer to show me the SQL commands I then cut/paste the SQL commands and replace the "real" data with the variable name I have chosen. The trick is that string variables need to be surrounded by single or double quotes, numerical data need no quotes around them and dates need "#" in the string containing the SQL commands. In your example and the answer from another user replace MySql with your string.

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

Similar Threads

  1. Create Custom box before executing query
    By mailinsiddu in forum Access
    Replies: 1
    Last Post: 01-09-2012, 11:14 AM
  2. Executing Query to Excel?
    By objNoob in forum Programming
    Replies: 1
    Last Post: 03-17-2010, 04:59 PM
  3. Privileges problems whe executing a query
    By admaldo in forum Security
    Replies: 0
    Last Post: 04-27-2006, 07:22 AM
  4. Replies: 0
    Last Post: 04-24-2006, 06:48 AM
  5. Replies: 0
    Last Post: 04-21-2006, 06:55 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