Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.vb.database.ado,microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default ADO Open command fails on multiple SQL functions

Hello All:

About a dozen users are independently updating separate Access tables
in the same db. The following Excel front end code is designed to
return the list of tables that are then passed to a SQL command that
runs three functions: Count, Sum and Max to determine the row counts,
when the table was updated, etc.

The code works fine with only one function, but it crashes at the
recordset open command when two or more functions are called resulting
in a "...Run-time error...(80040e14) is not a valid name. Make sure
that is does not include invalid characters or punctuation and that it
is not too long".

Thanks for any help.

'// ADO to retrieve table names
Sub ListTables()

'//Code to retrieve table names and post to a Excel worksheet...

Call seeupdate

End Sub

Sub seeupdate()
Dim dblocation As String
Dim queryname As String
Dim TableName As String
Dim i As Integer

Sheet2.Select
lastcell = Sheet2.Range("a65536").End(xlUp).Row

For i = 2 To lastcell
dblocation = "H:\Test\Stages.mdb"
TableName = Sheet2.Range("A" & i).Value

'//***** Code works fine when the query uses only one function
*****
'queryname = "SELECT MAX(A.TIME) AS SumOfVolume FROM " & TableName
& " A"
queryname = "SELECT Sum(A.Volume), Count(A.Volume), Max(A.Time)
FROM " & TableName & " A"

Call runaccessquery(dblocation, queryname, "Sheet2", i)
Next i
End Sub

Sub runaccessquery(dblocation As String, queryname As String,
outputsheet As String, rtndata As Variant)

Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim fld As ADODB.Field
Dim errorarray() As Variant

'// Open the connection
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" &
dblocation & ";"

'//***** Here's where the code breaks when the SQL contains more
than one function ******
'// Open the recordset
rst.Open "[" & queryname & "]", _
cnn, adOpenForwardOnly, adLockReadOnly, adCmdStoredProc

'// Display the records
x = rtndata
Do Until rst.EOF
y = 2
For Each fld In rst.Fields
Sheets(outputsheet).Cells(x, y).Value = fld.Value
y = y + 1
Next
Debug.Print
rst.MoveNext
x = x + 1
Loop

'// Close the recordset and connection
rst.Close
cnn.Close

End Sub
  #2   Report Post  
Posted to microsoft.public.vb.database.ado,microsoft.public.excel.programming
external usenet poster
 
Posts: 47
Default ADO Open command fails on multiple SQL functions

Hi

I've never har problems with that. And the only thing you seem to do different from what I usually do is
- square brackets around SQL statement,
- "adCmdStoredProc", and missing
Set rst = Nothing
Set cnn = nothing
at the very end.

Consider also a global connection that stays open during the repeating loop, instead of a new cnn each time. It's a little faster
and should also be more stable. It's impossible for me to test your code as is, so this is just guessing. And I may well be
overlooking something important.

Want a incredibly faster way to paste the results btw ?

Sheets(outputsheet).Cells(2, 1).CopyFromRecordset rst
rst.Close

No looping, simply that.

--
HTH. Best wishes Harald
Excel MVP

Followup to newsgroup only please.

"Zachary Bass" skrev i melding m...
Hello All:

About a dozen users are independently updating separate Access tables
in the same db. The following Excel front end code is designed to
return the list of tables that are then passed to a SQL command that
runs three functions: Count, Sum and Max to determine the row counts,
when the table was updated, etc.

The code works fine with only one function, but it crashes at the
recordset open command when two or more functions are called resulting
in a "...Run-time error...(80040e14) is not a valid name. Make sure
that is does not include invalid characters or punctuation and that it
is not too long".

Thanks for any help.

'// ADO to retrieve table names
Sub ListTables()

'//Code to retrieve table names and post to a Excel worksheet...

Call seeupdate

End Sub

Sub seeupdate()
Dim dblocation As String
Dim queryname As String
Dim TableName As String
Dim i As Integer

Sheet2.Select
lastcell = Sheet2.Range("a65536").End(xlUp).Row

For i = 2 To lastcell
dblocation = "H:\Test\Stages.mdb"
TableName = Sheet2.Range("A" & i).Value

'//***** Code works fine when the query uses only one function
*****
'queryname = "SELECT MAX(A.TIME) AS SumOfVolume FROM " & TableName
& " A"
queryname = "SELECT Sum(A.Volume), Count(A.Volume), Max(A.Time)
FROM " & TableName & " A"

Call runaccessquery(dblocation, queryname, "Sheet2", i)
Next i
End Sub

Sub runaccessquery(dblocation As String, queryname As String,
outputsheet As String, rtndata As Variant)

Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim fld As ADODB.Field
Dim errorarray() As Variant

'// Open the connection
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" &
dblocation & ";"

'//***** Here's where the code breaks when the SQL contains more
than one function ******
'// Open the recordset
rst.Open "[" & queryname & "]", _
cnn, adOpenForwardOnly, adLockReadOnly, adCmdStoredProc

'// Display the records
x = rtndata
Do Until rst.EOF
y = 2
For Each fld In rst.Fields
Sheets(outputsheet).Cells(x, y).Value = fld.Value
y = y + 1
Next
Debug.Print
rst.MoveNext
x = x + 1
Loop

'// Close the recordset and connection
rst.Close
cnn.Close

End Sub



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel 2007 fails to open documents VladimirF New Users to Excel 2 May 12th 08 05:33 PM
Workbook fails to open - Excel 2003 nc Excel Discussion (Misc queries) 1 February 2nd 06 06:33 PM
Excel Template fails to open??? Rob Smith Excel Discussion (Misc queries) 2 April 11th 05 06:42 PM
Shortcut file fails to open JimH Excel Discussion (Misc queries) 3 January 15th 05 10:13 PM
Autorefresh Fails on Open STEVE Excel Discussion (Misc queries) 0 December 20th 04 10:23 PM


All times are GMT +1. The time now is 10:22 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"