Results 1 to 9 of 9
  1. #1
    cff_moiseszaragoza is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Posts
    6

    Select

    Hey i am new to access programming

    I am having problem with a select
    my code looks like

    Private Sub LoadPDF_Click()

    sql = " SELECT id FROM PROJECT WHERE city=1 and state = 1 "
    CurrentDb.Execute (sql)

    End Sub

    When i run the query it works but not from my access file

  2. #2
    John_G is offline VIP
    Windows XP Access 2000
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    That's correct, and it is because you cannot use Select queries with Docmd.Execute - DoCmd.Execute can only be used with action queries like Update..., Delete.. and Insert...

    How do you want to use the query, i.e. what do you want to do with the records it selects?

    John

  3. #3
    cff_moiseszaragoza is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Posts
    6
    Thanks for letting me know
    Well i want to get 2 values out the id and a count
    Then i want to add x to the count so i can
    run a update

  4. #4
    John_G is offline VIP
    Windows XP Access 2000
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    ID and Count are not really the same thing. "Count" implies you want to know how many records there are (in table PROJECT?) that meet certain criteria, in your case city=1 and state = 1. But what is ID? ID is normally an identifier in each individual record, so while a count will tell you how many records there are, the list of ID's tells you which ones they are.

    You cannot get both from the same query (not directly, anyway) - so can you give more detail on what it is you want to do?

    John

  5. #5
    cff_moiseszaragoza is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Posts
    6
    i know that id and count are 2 different things

    but my problem is how do i get a select

    sql = " SELECT id, field_that_holds_documet_count_as_val FROM PROJECT WHERE city=1 and state = 1 "
    CurrentDb.Execute (sql)

    My Process

    Get values from query
    id = id
    count = field_that_holds_documet_count_as_val + more value
    Update table Set field_that_holds_documet_count_as_val = 'count' Where id = id

  6. #6
    John_G is offline VIP
    Windows XP Access 2000
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    How many records are there where city = 1 and state = 1?
    If the answer to that is only 1, then you don't need a select at all - you can do the update directly:

    currentdb.execute "Update Project set field_that_holds_documet_count_as_val = " & _
    field_that_holds_documet_count_as_val + " & x & _
    " where city = 1 and state = 1"

    Now, if there is more than one record where city = 1 and state = 1, then you have to refine your where clause to identify which record you want to update. If you want to update all the records, the statement I showed you above will do that, just as I wrote it.

    John

  7. #7
    cff_moiseszaragoza is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Posts
    6
    it only returns 1 record but i need to get a value.
    There is a value in that that table that i need to add to then update

  8. #8
    cff_moiseszaragoza is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Posts
    6
    let me go back to the original question
    How do you I get data from a Select statement

  9. #9
    John_G is offline VIP
    Windows XP Access 2000
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    First question:
    "There is a value in that that table that i need to add to then update "

    I showed you that with this suggestion:

    currentdb.execute "Update Project set field_that_holds_documet_count_as_val = " & _
    field_that_holds_documet_count_as_val + " & x & _
    " where city = 1 and state = 1"

    Putting this in generic terms:

    SQL = "Update [Table_Name] set [Field_Name] = Value where conditions"
    currentdb.execute SQL

    That will update 0, 1 or many records in the table Table_Name, depending on what is in the conditions.

    Second question:

    To use data retrieved by Select statement, you put it in to a recordset:

    Dim SQL as string, rst as RecordSet

    SQL = SELECT id, Count_Field FROM PROJECT WHERE city=1 and state = 1 "
    currentdb.openrecordset(SQL)
    if not rst.BOF then ' Needed to prevent an error if no records were retrieved
    rst.edit
    rst!Count_field = rst!Count_Field + x
    rst.update
    endif
    rst.close

    If the Select statement retrieves more than one record, only the first one is updated using this bit of code.

    HTH

    John

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

Similar Threads

  1. Select Into VBA
    By snoopy2003 in forum Programming
    Replies: 3
    Last Post: 08-17-2011, 09:20 AM
  2. Replies: 8
    Last Post: 04-21-2011, 05:29 PM
  3. Replies: 4
    Last Post: 03-17-2011, 06:17 AM
  4. Replies: 1
    Last Post: 10-22-2010, 10:11 AM
  5. Select MAX ( ) +1
    By marco in forum Access
    Replies: 2
    Last Post: 05-05-2010, 03:51 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