+ Reply to Thread
Results 1 to 9 of 9

Executing queries in Access from Excel

Hybrid View

Felipe99 Executing queries in Access... 02-20-2010, 11:10 AM
JeanRage Re: Executing queries in... 02-20-2010, 12:03 PM
Felipe99 Re: Executing queries in... 02-20-2010, 03:13 PM
romperstomper Re: Executing queries in... 02-20-2010, 07:28 PM
royUK Re: Executing queries in... 02-21-2010, 04:48 AM
Richard Schollar Re: Executing queries in... 02-21-2010, 04:49 AM
romperstomper Re: Executing queries in... 02-22-2010, 03:54 AM
DonkeyOte Re: Executing queries in... 02-22-2010, 04:18 AM
Richard Schollar Re: Executing queries in... 02-22-2010, 04:33 AM
  1. #1
    Registered User
    Join Date
    02-20-2010
    Location
    Santiago, Chile
    MS-Off Ver
    Excel 2007
    Posts
    2

    Executing queries in Access from Excel

    Hi,

    I am making a tool that has as a front end an Excel workbook and as a back end an Access DB. I am storing and processing the data at the DB. All the data processing and storing is triggered by buttons in the workbook, therefore I have a set of macros that runs queries in the DB. All the queries that I run are defined at the access data base.

    Here the problem: I run the queries in the Excel macros by their names, and this sometimes works and sometimes not. Bellow a piece of my code.
    --------------------------------------------------------------------------------------------------------------------------
    Public Function EjecutarSQL(c As String) As Boolean
      Dim cn As ADODB.Connection
      Dim Path as String
      Dim StrError As String
      
      EjecutarSQL = True
      On Error GoTo ADO_ERROR
      
      If Not AbrirConexBD(cn) Then
        EjecutarSQL = False
        Exit Function
      End If
      
      cn.Execute c
        
      If Not CerrarConexBD(cn) Then
        EjecutarSQL = False
        Exit Function
      Else
        Exit Function
      End If
    
    ADO_ERROR:
    
      If Err <> 0 Then
        StrError = "Error durante la ejecutión de la consulta " + c + ". Mensage técnico: " +   Err.Description
        MsgBox StrError
        Err.Clear
        EjecutarSQL = False
        MsgBox "La consulta no fue realizada correctamente."
        Exit Function
      End If
    End Function
    --------------------------------------------------------------------------------------------------------------------------
    Public Function AbrirConexBD(cn As ADODB.Connection) As Boolean
      'Función para abrir la conexión a la BD
      Dim Path As String
      Dim StrError As String
      
      AbrirConexBD = True
      On Error GoTo ADO_ERROR
       
      Set cn = New ADODB.Connection
      Path = ActiveWorkbook.Path + "\"
      cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Path + _
                            "bdcc.accdb;Persist Security Info=False"
      cn.Open
    
    ADO_ERROR:
    
      If Err <> 0 Then
        StrError = "Error al abrir la conexión a la BD. Mensage técnico: " + Err.Description
        MsgBox StrError
        Err.Clear
        AbrirConexBD = False
        Exit Function
      End If
      
    End Function
    --------------------------------------------------------------------------------------------------------------------------
    Public Function CerrarConexBD(cn As ADODB.Connection) As Boolean
      'Función para cerrar la conexión a la BD
      Dim StrError As String
      
      CerrarConexBD = True
      On Error GoTo ADO_ERROR
      
      If cn.State <> adStateClosed Then
        cn.Close
      End If
      If Not cn Is Nothing Then Set cn = Nothing
    
    ADO_ERROR:
    
      If Err <> 0 Then
        StrError = "Error al cerrar la conexión a la BD. Mensage técnico: " + Err.Description
        MsgBox StrError
        Err.Clear
        CerrarConexBD = False
        Exit Function
      End If
    End Function
    --------------------------------------------------------------------------------------------------------------------------

    In the function EjecutarSQL "c" is for example an Insert query defined in the DB and named C1, thus when I want to execute this query I write EjecutarSQL("C1") in my code, and not EjecutarSQL("Insert (x1, x2, x3) into ..."). I want to keep running the queries in this way because this gives me flexibility to modify my tool without modifying the code.

    The error I sometimes get is in the line “cn.Execute c”, and the message is invalid SQL syntax.

    Any idea or suggestion is welcomed. Thanks in advance.

    Felipe99.
    Last edited by royUK; 02-21-2010 at 04:48 AM.

  2. #2
    Valued Forum Contributor JeanRage's Avatar
    Join Date
    03-02-2009
    Location
    Nice, France
    MS-Off Ver
    Excel 2003
    Posts
    705

    Re: Executing queries in Access from Excel

    Hi,

    Make sure in VBE Tools References Microsoft ActiveX DataObjects 2.8 or higher is checked...

    HTH

  3. #3
    Registered User
    Join Date
    02-20-2010
    Location
    Santiago, Chile
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Executing queries in Access from Excel

    I have review this and it is checked.

    Any other idea?

    Thanks,

    Felipe99

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,017

    Re: Executing queries in Access from Excel

    The error would imply that the SQL itself is incorrect, rather than the code necessarily. What is the SQL in question?
    Everyone who confuses correlation and causation ends up dead.

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Executing queries in Access from Excel

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here

    Added this once only,future posts that break the Forum Rules may be closed,so READ THEM
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  6. #6
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264

    Re: Executing queries in Access from Excel

    In a similar vein to what the big R asked, is it always the same query that fails (and does this particular query ever work)? Also, what are you doing with the recordset produced? You don't seem to be assigning it anywhere.

    Richard
    Richard Schollar
    Microsoft MVP - Excel

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,017

    Re: Executing queries in Access from Excel

    Surely, of the two of us, you would be the big R?

  8. #8
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Executing queries in Access from Excel

    The mighty big R's ...

  9. #9
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264

    Re: Executing queries in Access from Excel

    Quote Originally Posted by romperstomper View Post
    Surely, of the two of us, you would be the big R?
    Certainly in terms of girth and height Probably not brain power, unfortunately

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1