UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> populate excel combo box w/Access data    
 
   
vandensype
post Oct 14 2006, 04:17 PM
Post #1

UtterAccess Addict
Posts: 201



Hello all,

I am having a problem with the code below. Basically, I want to populate a combo box with Access data by sending the combobox object and a SQL string to a subroutine. My problem is that it works when I use a simple query like: "SELECT * FROM tblname;", but anything more complicated than that fails with the error "Method 'Execute' of object '_Connection' Failed"

Any ideas why this is happening?


CODE


Sub Populate_Combobox_Recordset(ByRef objCBO As ComboBox, sSQL As String)
    Dim cnt As ADODB.Connection
    Dim rst As ADODB.Recordset
    Dim stDB As String, stConn As String, stSQL As String
    Dim xlCalc As XlCalculation
    Dim vaData As Variant
    Dim k As Long
    
     'In order to increase the performance.

    With Application
        xlCalc = .Calculation
        .Calculation = xlCalculationManual
        .EnableEvents = False
        .ScreenUpdating = False
    End With
    
     'Instantiate the Connectionobject.
    Set cnt = New ADODB.Connection
    
     'Path to and the name of the database.
    stDB = GBL_DatabasePath
    
     'Create the connectionstring.
    stConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & stDB & ";"
    

'==================================
sSQL = "SELECT * FROM sp83_zones;"   'This works

sSQL = "SELECT [Zone] & " - " & [Description] AS [Text], utm_zones.Zone FROM utm_zones;"  'This doesn't work
'==============================

    With cnt
        .CursorLocation = adUseClient 'Necesary for creating disconnected recordset.
        .Open stConn 'Open connection.
         'Instantiate the Recordsetobject and execute the SQL-state.
'This is where it fails: =================================
        Set rst = .Execute(sSQL)
'==============================================
    End With
    
    With rst
        Set .ActiveConnection = Nothing 'Disconnect the recordset.
        k = .Fields.Count
         'Populate the array with the whole recordset.
        vaData = .GetRows
    End With
    
     'Close the connection.
    cnt.Close
    
    With objCBO
        .Clear
        .BoundColumn = k
        .List = Application.Transpose(vaData)
        .ListIndex = -1
        .ColumnCount = 1
    End With
        
     'Restore the settings.
    With Application
        .Calculation = xlCalc
        .EnableEvents = True
        .ScreenUpdating = True
    End With
    
     'Release objects from memory.
    Set rst = Nothing
    Set cnt = Nothing

End Sub
Go to the top of the page
 
+
ace
post Oct 14 2006, 06:39 PM
Post #2

UtterAccess VIP
Posts: 5,279
From: Upstate NY, USA



What else have you tried?
Have you tried selecting fields by name without the concantenation?
Have you tried removing the brackets around the alias [text]?
Did you run the same query in the Access QBE?
Go to the top of the page
 
+
vandensype
post Oct 15 2006, 08:02 AM
Post #3

UtterAccess Addict
Posts: 201



Ace, I indeed have tried all those things (the wuery works in the QBE, in foact that is where I design it and then I just copy and paste to Excel).

I did some research, and it seems that some people fixed this by updating their MDAC, but I am on WinXP SP2 and apparantly you cannot download and install the MDAC (v 2.8 SP1) for this?!?

Anyone else know of this problem?

Thanks
Go to the top of the page
 
+
norie
post Oct 15 2006, 09:41 AM
Post #4

UtterAccess VIP
Posts: 4,297



Are you sure sSQL is valid?

It looks to me as though you are trying to do some sort of subtraction.(IMG:http://www.utteraccess.com/forum/style_emoticons/default/dazed.gif)

When I run just this line I get a type mismatch.
CODE
sSQL = "SELECT [Zone] & " - " & [Description] AS [Text], utm_zones.Zone FROM utm_zones;"
Go to the top of the page
 
+
vandensype
post Oct 15 2006, 10:23 AM
Post #5

UtterAccess Addict
Posts: 201



It works for me, I am just concatenating a hyphen (as a string). Nevertheless, even if I take out the hypen the ADO execute method still fails with just the two fields in the string.
Go to the top of the page
 
+
norie
post Oct 15 2006, 10:55 AM
Post #6

UtterAccess VIP
Posts: 4,297



It is the exact code isn't it?

Because I can't even get it to run.(IMG:http://www.utteraccess.com/forum/style_emoticons/default/dazed.gif)
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 24th May 2013 - 01:51 PM