Results 1 to 7 of 7

Thread: Simple Access code queries [resolved]

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2004
    Posts
    58

    Simple Access code queries [resolved]

    Hi,

    I need help with some of the most basic database coding in the world, (haven't done any for 4 years....ish). I am trying to write some VBA in MS Access, and have two main, (initial), questions.

    1. How do you run an SQL SELECT statement from VBA Access? I can run a simple update, but my select goes pair shaped...see code below:
    2. How do I get the results out of the returned object\recordset?

    Any and all help will be very much appreciated.

    Cheers

    Simple Update that works fine
    VB Code:
    1. Dim conDatabase As ADODB.Connection
    2. Dim strSQL As String
    3.  
    4.     Set conDatabase = CurrentProject.Connection
    5.  
    6.     strSQL = "UPDATE SIMs SET Status = 'Active'"
    7.     conDatabase.Execute strSQL
    8.  
    9.     MsgBox "All status' have been set to ""Active""."
    10.  
    11.     conDatabase.Close
    12.     Set conDatabase = Nothing

    But this SELECT statement returns -1 in the recordset size
    VB Code:
    1. Dim conDatabase As ADODB.Connection
    2. Dim strSQL As String
    3. Dim rst As Recordset
    4.  
    5.     Set conDatabase = CurrentProject.Connection
    6.  
    7.     strSQL = "SELECT * FROM SIMs"
    8.     Set rst = conDatabase.Execute(strSQL)
    9.     For i = 1 To i = rst.RecordCount
    10.         result = rst.GetString
    11.         MsgBox (result)
    12.     Next i
    13.    
    14.     conDatabase.Close
    15.     Set conDatabase = Nothing
    Last edited by mr_tango; Apr 14th, 2004 at 08:36 AM.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    instead of conDatabase.Execute try conDatabase.OpenRecordset (you may need to set some extra parameters).

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2004
    Posts
    58
    As suggested, I am trying out the OpenRecordSet command, but I am having a few problems. I have altered the
    VB Code:
    1. Set rst = conDatabase.Execute(strSQL)
    to

    VB Code:
    1. Set rst = conDatabase.OpenRecordSet("SELECT * FROM SIMs")

    and I am now getting an error message stating that my parameters are either too long, conflict with each other or are of the wrong type. I have checked the MSDN website and it says that it should be used thus:

    Set recordset = object.OpenRecordset(source, type, options, lockedits)

    where

    source
    A String specifying the source of the records for the new Recordset. The source can be a table name, a query name, or an SQL statement that returns records. For table-type Recordset objects in Microsoft Jet databases, the source can only be a table name.

    and the other two parameters are optional. Can anyone see what I am doing wrong?

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2004
    Posts
    58
    I have also tried out an MSDN example, (altering the SQL), but Access doesn't recognise the "Database" in the first DIM statement......any ideas on either this one or the above issue?

    VB Code:
    1. Sub selectAll()
    2.     Dim dbs As Database, rst As Recordset
    3.     Dim strSQL As String
    4.  
    5.     ' Return reference to current database.
    6.     Set dbs = CurrentDb
    7.     strSQL = "SELECT * FROM SIMs"
    8.     Set rst = dbs.OpenRecordset(strSQL)
    9.     rst.MoveLast
    10.     Debug.Print rst.RecordCount
    11.     rst.Close
    12.     Set dbs = Nothing
    13. End Sub

  5. #5
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    Hello;

    check out this current thread:

    http://www.vbforums.com/showthread.p...hreadid=286325

    There is code there for ADO that uses a command object. I have never had a problem when executing my queries this way. I also use it to do updates and inserts. With regards to the record count property, ADO does not support server side record counts (atleast 2.5, the one I am working with)....so you need to do:

    VB Code:
    1. conn.CursorLocation = adUseClient 'conn being a connection object

    ADO can do a lot - once you get going you'll see its power!!
    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2004
    Posts
    58
    EXCELLENT!!!! It all works perfectly! Thanks for the help, it was very much needed and is very much appreciated

    The working solution.......basically copied from the link above

    VB Code:
    1. Sub selectAll()
    2. Dim conDatabase As ADODB.Connection
    3. Dim rs As New ADODB.Recordset
    4. Dim cmdCommand As New ADODB.Command
    5. Dim sql As String
    6. Dim valueString As String
    7.  
    8.     valueString = ""
    9.     Set conDatabase = CurrentProject.Connection   'IS THIS CORRECT
    10.     Set cmdCommand.ActiveConnection = conDatabase 'IS THIS CORRECT?
    11.     cmdCommand.CommandType = adCmdText
    12.  
    13.     sql = "SELECT * FROM SIMs"
    14.     cmdCommand.CommandText = sql
    15.  
    16.     Set rs = cmdCommand.Execute
    17.  
    18.     Do While Not rs.EOF
    19.         valueString = ""
    20.        
    21.         valueString = rs.Fields("Network_Code").Value
    22.         valueString = valueString + ", " + rs.Fields("SIM_No").Value
    23.         MsgBox (valueString)
    24.         rs.MoveNext
    25.     Loop
    26.  
    27. End Sub
    Last edited by mr_tango; Apr 14th, 2004 at 08:40 AM.

  7. #7
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    VB Code:
    1. Sub selectAll()
    2. Dim conDatabase As ADODB.Connection
    3. Dim rs As New ADODB.Recordset
    4. Dim cmdCommand As New ADODB.Command
    5. Dim sql As String
    6. Dim valueString As String
    7.  
    8.     valueString = ""
    9.     Set conDatabase = CurrentProject.Connection   'IS THIS CORRECT <-----NO - it should be ADODB.Connection
    10.  
    11. ' you also need to pass a connection string to the connection object and open it first before this code will work
    12.     Set cmdCommand.ActiveConnection = conDatabase 'IS THIS CORRECT?
    13.     cmdCommand.CommandType = adCmdText
    14.  
    15.     sql = "SELECT * FROM SIMs"
    16.     cmdCommand.CommandText = sql
    17.  
    18.     Set rs = cmdCommand.Execute
    19.  
    20.     Do While Not rs.EOF
    21.         valueString = ""
    22.        
    23.         valueString = rs.Fields("Network_Code").Value
    24.         valueString = valueString + ", " + rs.Fields("SIM_No").Value
    25.         MsgBox (valueString)
    26.         rs.MoveNext
    27.     Loop
    28.  
    29. End Sub


    check out this link for connection strings and opening connection object:

    http://www.able-consulting.com/ADO_Conn.htm

    cheers
    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width