Hello,
My company recently migrated to Windows 7 and Excel 2010, the below macro used to work in Windows XP and Excel 07. The first time the macro is run I get a:
"Microsoft Visual Basic
run-time error '-2147467249 (80004005)':
Query timeout expired
Here is the code:
Sub ImportMacola()
Worksheets("BMB").Range("A2:I10000").ClearContents
Worksheets("RMN").Range("A2:I10000").ClearContents
Worksheets("FFD").Range("A2:I10000").ClearContents
Worksheets("TUM").Range("A2:I10000").ClearContents
Worksheets("MTV").Range("A2:I10000").ClearContents
Call ImportPaperVisionMacolaData("data_04", DateInMacolaFormat(Sheet1.Range("startdate").Value), DateInMacolaFormat(Sheet1.Range("enddate").Value))
Call ImportPaperVisionMacolaData("data_03", DateInMacolaFormat(Sheet1.Range("startdate").Value), DateInMacolaFormat(Sheet1.Range("enddate").Value))
Call ImportPaperVisionMacolaData("data_005", DateInMacolaFormat(Sheet1.Range("startdate").Value), DateInMacolaFormat(Sheet1.Range("enddate").Value))
Call ImportPaperVisionMacolaData("data_006", DateInMacolaFormat(Sheet1.Range("startdate").Value), DateInMacolaFormat(Sheet1.Range("enddate").Value))
Call ImportPaperVisionMacolaData("data_07", DateInMacolaFormat(Sheet1.Range("startdate").Value), DateInMacolaFormat(Sheet1.Range("enddate").Value))
End Sub
Private Sub ImportPaperVisionMacolaData(str_CATALOG As String, str_StartDate As String, str_EndDate As String)
' Create a connection object.
Dim cnPubs As ADODB.Connection
Set cnPubs = New ADODB.Connection
Dim intRecCount As Integer
' Provide the connection string.
Dim strConn As String
'Use the SQL Server OLE DB Provider.
strConn = "PROVIDER=SQLOLEDB;"
'Connect to the Pubs database on the local server.
strConn = strConn & "DATA SOURCE=NJEDIMAC02;INITIAL CATALOG=" & str_CATALOG & ";"
'Use an integrated login.
'strConn = strConn & " INTEGRATED SECURITY=sspi;"
strConn = strConn & " User ID=papervision;Password=XXXXXXXX;"
'Now open the connection.
cnPubs.Open strConn
Dim rsPubs As ADODB.Recordset
Set rsPubs = New ADODB.Recordset
With rsPubs
' Assign the Connection object.
.ActiveConnection = cnPubs
' Extract the required records.
.Open "SELECT cus_no AS 'CUSTOMER #'" & _
" ,loc_desc AS 'STOCKING LOC'" & _
" ,bill_to_name AS 'CUSTOMER NAME'" & _
" ,oe_po_no AS 'PO #'" & _
" ,cast(cast(ord_no as integer) as varchar(8)) AS 'ORDER #'" & _
" ,inv_no AS 'INVOICE #'" & _
" ,cast(substring(cast(inv_dt as varchar(8)),5,2) + '/' + substring(cast(inv_dt as varchar(8)),7,2) + '/' + substring(cast(inv_dt as varchar(8)),1,4)as varchar(10)) AS 'INVOICE DATE'" & _
" ,cast('$' + cast(tot_dollars as varchar(20)) as varchar(20)) AS 'INVOICE AMT'" & _
" FROM oehdrhst_sql AS OHH" & _
" LEFT OUTER JOIN IMLOCFIL_SQL ON mfg_loc = loc " & _
" WHERE inv_dt BETWEEN '" & str_StartDate & "' and '" & str_EndDate & "'" & _
" ORDER BY inv_no", , adOpenDynamic
Select Case str_CATALOG
Case "data_04"
Sheet2.Range("A2").CopyFromRecordset rsPubs
If Not rsPubs.BOF Then
rsPubs.MoveFirst
End If
If Not rsPubs.EOF Then
While Not rsPubs.EOF
intRecCount = intRecCount + 1
Sheet2.Cells(intRecCount + 1, 9).Value = intRecCount
rsPubs.MoveNext
Wend
rsPubs.MoveFirst
End If
Case "data_03"
Sheet3.Range("A2").CopyFromRecordset rsPubs
If Not rsPubs.BOF Then
rsPubs.MoveFirst
End If
If Not rsPubs.EOF Then
While Not rsPubs.EOF
intRecCount = intRecCount + 1
Sheet3.Cells(intRecCount + 1, 9).Value = intRecCount
rsPubs.MoveNext
Wend
rsPubs.MoveFirst
End If
Case "data_005"
Sheet4.Range("A2").CopyFromRecordset rsPubs
If Not rsPubs.BOF Then
rsPubs.MoveFirst
End If
If Not rsPubs.EOF Then
While Not rsPubs.EOF
intRecCount = intRecCount + 1
Sheet4.Cells(intRecCount + 1, 9).Value = intRecCount
rsPubs.MoveNext
Wend
rsPubs.MoveFirst
End If
Case "data_006"
Sheet5.Range("A2").CopyFromRecordset rsPubs
If Not rsPubs.BOF Then
rsPubs.MoveFirst
End If
If Not rsPubs.EOF Then
While Not rsPubs.EOF
intRecCount = intRecCount + 1
Sheet5.Cells(intRecCount + 1, 9).Value = intRecCount
rsPubs.MoveNext
Wend
rsPubs.MoveFirst
End If
Case "data_07"
Sheet6.Range("A2").CopyFromRecordset rsPubs
If Not rsPubs.BOF Then
rsPubs.MoveFirst
End If
If Not rsPubs.EOF Then
While Not rsPubs.EOF
intRecCount = intRecCount + 1
Sheet6.Cells(intRecCount + 1, 9).Value = intRecCount
rsPubs.MoveNext
Wend
rsPubs.MoveFirst
End If
End Select
' Tidy up
.Close
End With
cnPubs.Close
Set rsPubs = Nothing
Set cnPubs = Nothing
End Sub
Private Function DateInMacolaFormat(dat) As String
DateInMacolaFormat = Format(dat, "yyyy") & Format(dat, "mm") & Format(dat, "dd")
End Function
If I change the password and intentionally crash the macro, then fix the password and run it again, it works.
The dubugger is pointing to this line:
Sheet2.Range("A2").CopyFromRecordset rsPubs
I do understand the timeoput message, but when the macro runs, it only tales 4 seconds of query time.
Any suggestions?
Thanks Rich
My company recently migrated to Windows 7 and Excel 2010, the below macro used to work in Windows XP and Excel 07. The first time the macro is run I get a:
"Microsoft Visual Basic
run-time error '-2147467249 (80004005)':
Query timeout expired
Here is the code:
Sub ImportMacola()
Worksheets("BMB").Range("A2:I10000").ClearContents
Worksheets("RMN").Range("A2:I10000").ClearContents
Worksheets("FFD").Range("A2:I10000").ClearContents
Worksheets("TUM").Range("A2:I10000").ClearContents
Worksheets("MTV").Range("A2:I10000").ClearContents
Call ImportPaperVisionMacolaData("data_04", DateInMacolaFormat(Sheet1.Range("startdate").Value), DateInMacolaFormat(Sheet1.Range("enddate").Value))
Call ImportPaperVisionMacolaData("data_03", DateInMacolaFormat(Sheet1.Range("startdate").Value), DateInMacolaFormat(Sheet1.Range("enddate").Value))
Call ImportPaperVisionMacolaData("data_005", DateInMacolaFormat(Sheet1.Range("startdate").Value), DateInMacolaFormat(Sheet1.Range("enddate").Value))
Call ImportPaperVisionMacolaData("data_006", DateInMacolaFormat(Sheet1.Range("startdate").Value), DateInMacolaFormat(Sheet1.Range("enddate").Value))
Call ImportPaperVisionMacolaData("data_07", DateInMacolaFormat(Sheet1.Range("startdate").Value), DateInMacolaFormat(Sheet1.Range("enddate").Value))
End Sub
Private Sub ImportPaperVisionMacolaData(str_CATALOG As String, str_StartDate As String, str_EndDate As String)
' Create a connection object.
Dim cnPubs As ADODB.Connection
Set cnPubs = New ADODB.Connection
Dim intRecCount As Integer
' Provide the connection string.
Dim strConn As String
'Use the SQL Server OLE DB Provider.
strConn = "PROVIDER=SQLOLEDB;"
'Connect to the Pubs database on the local server.
strConn = strConn & "DATA SOURCE=NJEDIMAC02;INITIAL CATALOG=" & str_CATALOG & ";"
'Use an integrated login.
'strConn = strConn & " INTEGRATED SECURITY=sspi;"
strConn = strConn & " User ID=papervision;Password=XXXXXXXX;"
'Now open the connection.
cnPubs.Open strConn
Dim rsPubs As ADODB.Recordset
Set rsPubs = New ADODB.Recordset
With rsPubs
' Assign the Connection object.
.ActiveConnection = cnPubs
' Extract the required records.
.Open "SELECT cus_no AS 'CUSTOMER #'" & _
" ,loc_desc AS 'STOCKING LOC'" & _
" ,bill_to_name AS 'CUSTOMER NAME'" & _
" ,oe_po_no AS 'PO #'" & _
" ,cast(cast(ord_no as integer) as varchar(8)) AS 'ORDER #'" & _
" ,inv_no AS 'INVOICE #'" & _
" ,cast(substring(cast(inv_dt as varchar(8)),5,2) + '/' + substring(cast(inv_dt as varchar(8)),7,2) + '/' + substring(cast(inv_dt as varchar(8)),1,4)as varchar(10)) AS 'INVOICE DATE'" & _
" ,cast('$' + cast(tot_dollars as varchar(20)) as varchar(20)) AS 'INVOICE AMT'" & _
" FROM oehdrhst_sql AS OHH" & _
" LEFT OUTER JOIN IMLOCFIL_SQL ON mfg_loc = loc " & _
" WHERE inv_dt BETWEEN '" & str_StartDate & "' and '" & str_EndDate & "'" & _
" ORDER BY inv_no", , adOpenDynamic
Select Case str_CATALOG
Case "data_04"
Sheet2.Range("A2").CopyFromRecordset rsPubs
If Not rsPubs.BOF Then
rsPubs.MoveFirst
End If
If Not rsPubs.EOF Then
While Not rsPubs.EOF
intRecCount = intRecCount + 1
Sheet2.Cells(intRecCount + 1, 9).Value = intRecCount
rsPubs.MoveNext
Wend
rsPubs.MoveFirst
End If
Case "data_03"
Sheet3.Range("A2").CopyFromRecordset rsPubs
If Not rsPubs.BOF Then
rsPubs.MoveFirst
End If
If Not rsPubs.EOF Then
While Not rsPubs.EOF
intRecCount = intRecCount + 1
Sheet3.Cells(intRecCount + 1, 9).Value = intRecCount
rsPubs.MoveNext
Wend
rsPubs.MoveFirst
End If
Case "data_005"
Sheet4.Range("A2").CopyFromRecordset rsPubs
If Not rsPubs.BOF Then
rsPubs.MoveFirst
End If
If Not rsPubs.EOF Then
While Not rsPubs.EOF
intRecCount = intRecCount + 1
Sheet4.Cells(intRecCount + 1, 9).Value = intRecCount
rsPubs.MoveNext
Wend
rsPubs.MoveFirst
End If
Case "data_006"
Sheet5.Range("A2").CopyFromRecordset rsPubs
If Not rsPubs.BOF Then
rsPubs.MoveFirst
End If
If Not rsPubs.EOF Then
While Not rsPubs.EOF
intRecCount = intRecCount + 1
Sheet5.Cells(intRecCount + 1, 9).Value = intRecCount
rsPubs.MoveNext
Wend
rsPubs.MoveFirst
End If
Case "data_07"
Sheet6.Range("A2").CopyFromRecordset rsPubs
If Not rsPubs.BOF Then
rsPubs.MoveFirst
End If
If Not rsPubs.EOF Then
While Not rsPubs.EOF
intRecCount = intRecCount + 1
Sheet6.Cells(intRecCount + 1, 9).Value = intRecCount
rsPubs.MoveNext
Wend
rsPubs.MoveFirst
End If
End Select
' Tidy up
.Close
End With
cnPubs.Close
Set rsPubs = Nothing
Set cnPubs = Nothing
End Sub
Private Function DateInMacolaFormat(dat) As String
DateInMacolaFormat = Format(dat, "yyyy") & Format(dat, "mm") & Format(dat, "dd")
End Function
If I change the password and intentionally crash the macro, then fix the password and run it again, it works.
The dubugger is pointing to this line:
Sheet2.Range("A2").CopyFromRecordset rsPubs
I do understand the timeoput message, but when the macro runs, it only tales 4 seconds of query time.
Any suggestions?
Thanks Rich