Run-Time Error 3265 - Using ADO in Excel 2010 VBA in Windows 7 64 bit

djbalcom

New Member
Joined
Aug 26, 2014
Messages
2


I am not an advanced user, but manage to get around in VBA. A lot of thanks to the posts here. I am new to ADO and manage to be somewhat successful, until now. I have a workbook that I am using as a mini database as I prefer the functionality of Excel over Access for what I need to do. I posted code below, so let me explain what is going on. The 2 main routines are UpdateDept() and GetChartData(). Both are local to a specific worksheet - "Charts" - as if that matters. Anyhow, I have these routines called when there is a Worksheet_Change for specific cells. The UpdateDept() routine works without issues by itself, repeatedly. However, once I couple that with the GetChartData() routine, I run into issues. The UpdateDept() routine just updates ActiveX combo box lists based on certain criteria the user defines, like date ranges. Once they are updated, the user can choose from these drop down lists. I use ADO to grab a recordset from my stored data so I can “dynamically” create some charts based on the user selected criteria. Once I run the GetChartData() routine, the next Worksheet_Change event for specified target cells will call UpdateDept(). When trying to open the recordset, I get that Run-Time Error 3265. The weird thing is, that once I End, or Debug, the error, the next time a Worksheet_Change event occurs and calls UpdateDept() and GetChartData() routines, they work fine. But hen issue comes back repeatedly. I thought perhaps I was not closing the recordset, connection, or something. But that doesn’t appear to be it, at least from my limited ADO VBA coding experience. Sorry code is rudimentary.







Perhaps I have explained too much, or too little, but any help you can provide is most appreciated.






Code below.





‘////





Private Sub UpdateDept()


Dim StartDate, EndDate As Date


Dim a As Long





a = 0





StartDate = Range("E1")


EndDate = Range("E2")





'Updating Department Drop Down List based on selected criteria


strSQL = "Select Distinct [DEPARTMENT] From [Data$] WHERE [DATE ENTERED] >=#" & StartDate & "# AND [DATE ENTERED] <=#" & EndDate & "#"





If Range("E7") <> "ALL" Then


strSQL = strSQL & " AND [STATUS] = '" & Range("E7") & "'"


End If





If Range("E4") <> "All QC Errors" Then


strSQL = strSQL & " AND [INTERNAL or External] = '" & Range("E4") & "'"


End If





closeRS


OpenDB


cmbDepartment.Clear





rs.Open strSQL, cnn, adOpenKeyset, adLockPessimistic '<---- Error here


If rs.RecordCount > 0 Then


Do While Not rs.EOF


If a = 0 Then


cmbDepartment.AddItem "All Departments"


a = a + 1


rs.MoveNext


Else


cmbDepartment.AddItem rs.Fields(0)


rs.MoveNext


End If


Loop


Else


MsgBox "I was not able to find any unique Categories(s).", vbCritical + vbOKOnly


Exit Sub


End If





'Updating Customer Drop Down List based on selected criteria


a = 0


strSQL = "Select Distinct [CUSTOMER] From [Data$] WHERE [DATE ENTERED] >=#" & StartDate & "# AND [DATE ENTERED] <=#" & EndDate & "#"





If Range("E7") <> "ALL" Then


strSQL = strSQL & " AND [STATUS] = '" & Range("E7") & "'"


End If





If Range("E4") <> "All QC Errors" Then


strSQL = strSQL & " AND [INTERNAL or External] = '" & Range("E4") & "'"


End If





closeRS


OpenDB


cmbCustomer.Clear





rs.Open strSQL, cnn, adOpenKeyset, adLockPessimistic


If rs.RecordCount > 0 Then


Do While Not rs.EOF


If a = 0 Then


cmbCustomer.AddItem "All Customers"


a = a + 1


rs.MoveNext


Else


cmbCustomer.AddItem rs.Fields(0)


rs.MoveNext


End If


Loop


Else


MsgBox "I was not able to find any unique Categories(s).", vbCritical + vbOKOnly


Exit Sub


End If





'Updating Category Drop Down List based on selected criteria


a = 0


strSQL = "Select Distinct [ERROR CATEGORY] From [Data$] WHERE [DATE ENTERED] >=#" & StartDate & "# AND [DATE ENTERED] <=#" & EndDate & "#"





If Range("E7") <> "ALL" Then


strSQL = strSQL & " AND [STATUS] = '" & Range("E7") & "'"


End If





If Range("E4") <> "All QC Errors" Then


strSQL = strSQL & " AND [INTERNAL or External] = '" & Range("E4") & "'"


End If





closeRS


OpenDB


cmbCategory.Clear





rs.Open strSQL, cnn, adOpenKeyset, adLockPessimistic


If rs.RecordCount > 0 Then


Do While Not rs.EOF


If a = 0 Then


cmbCategory.AddItem "All Categories"


a = a + 1


rs.MoveNext


Else


cmbCategory.AddItem rs.Fields(0)


rs.MoveNext


End If


Loop


Else


MsgBox "I was not able to find any unique Categories(s).", vbCritical + vbOKOnly


Exit Sub


End If





'Updating Press Drop Down List based on selected criteria


a = 0


strSQL = "Select Distinct [PRESS] From [Data$] WHERE [DATE ENTERED] >=#" & StartDate & "# AND [DATE ENTERED] <=#" & EndDate & "#"





If Range("E7") <> "ALL" Then


strSQL = strSQL & " AND [STATUS] = '" & Range("E7") & "'"


End If





If Range("E4") <> "All QC Errors" Then


strSQL = strSQL & " AND [INTERNAL or External] = '" & Range("E4") & "'"


End If





closeRS


OpenDB


cmbPress.Clear





rs.Open strSQL, cnn, adOpenKeyset, adLockPessimistic


If rs.RecordCount > 0 Then


Do While Not rs.EOF


If a = 0 Then


cmbPress.AddItem "All Presses"


a = a + 1


rs.MoveNext


Else


cmbPress.AddItem rs.Fields(0)


rs.MoveNext


End If


Loop


Else


MsgBox "I was not able to find any unique Categories(s).", vbCritical + vbOKOnly


Exit Sub


End If





End Sub





Private Sub Worksheet_Change(ByVal Target As Excel.Range)





If Intersect(Target, [E1,E2,E4,E7]) Is Nothing Then Exit Sub





UpdateDept


GetChartData





End Sub





Private Sub GetChartData()





Dim a, b As Long


Dim ChartCat As String


Dim CountCost As String


Dim DepPressCat As String


Dim StartDate, EndDate As Date





StartDate = Range("E1")


EndDate = Range("E2")





If Range("E5") <> "NonConformance" Then


ChartCat = Range("E5")


If ChartCat = "Department" Then


cmbDepartment.Clear


Else


cmbPress.Clear


End If


Else


ChartCat = "Error Category"


cmbCategory.Clear


End If





If Range("E6") = "Count" Then


CountCost = "Count([$ AMOUNT]) "


Else


CountCost = "Sum([$ AMOUNT])"


End If








strSQL = "SELECT distinct [" & ChartCat & "], " & CountCost & " as total " & _


"FROM [Data$] WHERE [DATE ENTERED] >=#" & StartDate & "# AND [DATE ENTERED] <=#" & EndDate & "#"





If Range("E4") <> "All QC Errors" Then


strSQL = strSQL & " AND [INTERNAL or External] = '" & Range("E4") & "'"


End If





If Range("E7") <> "ALL" Then


strSQL = strSQL & " AND [STATUS] = '" & Range("E7") & "'"


End If





If ChartCat = "Department" Then


If Range("H5") <> "" And Range("H5") <> "All Presses" Then DepPressCat = " AND [PRESS] = '" & Range("H5") & "'"


If Range("H7") <> "" And Range("H7") <> "All Categories" Then DepPressCat = DepPressCat & " AND [ERROR CATEGORY] = '" & Range("H7") & "'"


ElseIf ChartCat = "Press" Then


If Range("H4") <> "" And Range("H4") <> "All Departments" Then DepPressCat = " AND [DEPARTMENT] = '" & Range("H4") & "'"


If Range("H7") <> "" And Range("H7") <> "All Categories" Then DepPressCat = DepPressCat & " AND [ERROR CATEGORY] = '" & Range("H7") & "'"


ElseIf ChartCat = "Error Category" Then


If Range("H4") <> "" And Range("H4") <> "All Departments" Then DepPressCat = " AND [DEPARTMENT] = '" & Range("H4") & "'"


If Range("H5") <> "" And Range("H5") <> "All Presses" Then DepPressCat = " AND [PRESS] = '" & Range("H5") & "'"


End If





strSQL = strSQL & DepPressCat & " GROUP BY [" & ChartCat & "]"





'now extract data


closeRS


OpenDB





rs.Open strSQL, cnn, adOpenKeyset, adLockPessimistic


If rs.RecordCount > 0 Then


Sheets("Charts").Visible = True


Sheets("Charts").Select


Range("chartdataSet").Select


Range(Selection, Selection.End(xlDown)).ClearContents





'Now putting the data on the sheet


rs.Sort = "total DESC"


ActiveCell.CopyFromRecordset rs


Else


MsgBox "I was not able to find any matching records.", vbExclamation + vbOKOnly


Exit Sub


End If





Range("E2").Select


closeRS


cnn.Close


End Sub








‘////




 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
OK - I figured it out, I think... at least it seems to be working. I had to set the record set to nothing when closing it. See below.

Public Sub closeRS()
If rs.State = adStateOpen Then
rs.Close
Set rs = Nothing '<---- Had to add this
End If
rs.CursorLocation = adUseClient
End Sub


I am not an advanced user, but manage to get around in VBA. A lot of thanks to the posts here. I am new to ADO and manage to be somewhat successful, until now. I have a workbook that I am using as a mini database as I prefer the functionality of Excel over Access for what I need to do. I posted code below, so let me explain what is going on. The 2 main routines are UpdateDept() and GetChartData(). Both are local to a specific worksheet - "Charts" - as if that matters. Anyhow, I have these routines called when there is a Worksheet_Change for specific cells. The UpdateDept() routine works without issues by itself, repeatedly. However, once I couple that with the GetChartData() routine, I run into issues. The UpdateDept() routine just updates ActiveX combo box lists based on certain criteria the user defines, like date ranges. Once they are updated, the user can choose from these drop down lists. I use ADO to grab a recordset from my stored data so I can “dynamically” create some charts based on the user selected criteria. Once I run the GetChartData() routine, the next Worksheet_Change event for specified target cells will call UpdateDept(). When trying to open the recordset, I get that Run-Time Error 3265. The weird thing is, that once I End, or Debug, the error, the next time a Worksheet_Change event occurs and calls UpdateDept() and GetChartData() routines, they work fine. But hen issue comes back repeatedly. I thought perhaps I was not closing the recordset, connection, or something. But that doesn’t appear to be it, at least from my limited ADO VBA coding experience. Sorry code is rudimentary.







Perhaps I have explained too much, or too little, but any help you can provide is most appreciated.






Code below.





‘////





Private Sub UpdateDept()


Dim StartDate, EndDate As Date


Dim a As Long





a = 0





StartDate = Range("E1")


EndDate = Range("E2")





'Updating Department Drop Down List based on selected criteria


strSQL = "Select Distinct [DEPARTMENT] From [Data$] WHERE [DATE ENTERED] >=#" & StartDate & "# AND [DATE ENTERED] <=#" & EndDate & "#"





If Range("E7") <> "ALL" Then


strSQL = strSQL & " AND [STATUS] = '" & Range("E7") & "'"


End If





If Range("E4") <> "All QC Errors" Then


strSQL = strSQL & " AND [INTERNAL or External] = '" & Range("E4") & "'"


End If





closeRS


OpenDB


cmbDepartment.Clear





rs.Open strSQL, cnn, adOpenKeyset, adLockPessimistic '<---- Error here


If rs.RecordCount > 0 Then


Do While Not rs.EOF


If a = 0 Then


cmbDepartment.AddItem "All Departments"


a = a + 1


rs.MoveNext


Else


cmbDepartment.AddItem rs.Fields(0)


rs.MoveNext


End If


Loop


Else


MsgBox "I was not able to find any unique Categories(s).", vbCritical + vbOKOnly


Exit Sub


End If





'Updating Customer Drop Down List based on selected criteria


a = 0


strSQL = "Select Distinct [CUSTOMER] From [Data$] WHERE [DATE ENTERED] >=#" & StartDate & "# AND [DATE ENTERED] <=#" & EndDate & "#"





If Range("E7") <> "ALL" Then


strSQL = strSQL & " AND [STATUS] = '" & Range("E7") & "'"


End If





If Range("E4") <> "All QC Errors" Then


strSQL = strSQL & " AND [INTERNAL or External] = '" & Range("E4") & "'"


End If





closeRS


OpenDB


cmbCustomer.Clear





rs.Open strSQL, cnn, adOpenKeyset, adLockPessimistic


If rs.RecordCount > 0 Then


Do While Not rs.EOF


If a = 0 Then


cmbCustomer.AddItem "All Customers"


a = a + 1


rs.MoveNext


Else


cmbCustomer.AddItem rs.Fields(0)


rs.MoveNext


End If


Loop


Else


MsgBox "I was not able to find any unique Categories(s).", vbCritical + vbOKOnly


Exit Sub


End If





'Updating Category Drop Down List based on selected criteria


a = 0


strSQL = "Select Distinct [ERROR CATEGORY] From [Data$] WHERE [DATE ENTERED] >=#" & StartDate & "# AND [DATE ENTERED] <=#" & EndDate & "#"





If Range("E7") <> "ALL" Then


strSQL = strSQL & " AND [STATUS] = '" & Range("E7") & "'"


End If





If Range("E4") <> "All QC Errors" Then


strSQL = strSQL & " AND [INTERNAL or External] = '" & Range("E4") & "'"


End If





closeRS


OpenDB


cmbCategory.Clear





rs.Open strSQL, cnn, adOpenKeyset, adLockPessimistic


If rs.RecordCount > 0 Then


Do While Not rs.EOF


If a = 0 Then


cmbCategory.AddItem "All Categories"


a = a + 1


rs.MoveNext


Else


cmbCategory.AddItem rs.Fields(0)


rs.MoveNext


End If


Loop


Else


MsgBox "I was not able to find any unique Categories(s).", vbCritical + vbOKOnly


Exit Sub


End If





'Updating Press Drop Down List based on selected criteria


a = 0


strSQL = "Select Distinct [PRESS] From [Data$] WHERE [DATE ENTERED] >=#" & StartDate & "# AND [DATE ENTERED] <=#" & EndDate & "#"





If Range("E7") <> "ALL" Then


strSQL = strSQL & " AND [STATUS] = '" & Range("E7") & "'"


End If





If Range("E4") <> "All QC Errors" Then


strSQL = strSQL & " AND [INTERNAL or External] = '" & Range("E4") & "'"


End If





closeRS


OpenDB


cmbPress.Clear





rs.Open strSQL, cnn, adOpenKeyset, adLockPessimistic


If rs.RecordCount > 0 Then


Do While Not rs.EOF


If a = 0 Then


cmbPress.AddItem "All Presses"


a = a + 1


rs.MoveNext


Else


cmbPress.AddItem rs.Fields(0)


rs.MoveNext


End If


Loop


Else


MsgBox "I was not able to find any unique Categories(s).", vbCritical + vbOKOnly


Exit Sub


End If





End Sub





Private Sub Worksheet_Change(ByVal Target As Excel.Range)





If Intersect(Target, [E1,E2,E4,E7]) Is Nothing Then Exit Sub





UpdateDept


GetChartData





End Sub





Private Sub GetChartData()





Dim a, b As Long


Dim ChartCat As String


Dim CountCost As String


Dim DepPressCat As String


Dim StartDate, EndDate As Date





StartDate = Range("E1")


EndDate = Range("E2")





If Range("E5") <> "NonConformance" Then


ChartCat = Range("E5")


If ChartCat = "Department" Then


cmbDepartment.Clear


Else


cmbPress.Clear


End If


Else


ChartCat = "Error Category"


cmbCategory.Clear


End If





If Range("E6") = "Count" Then


CountCost = "Count([$ AMOUNT]) "


Else


CountCost = "Sum([$ AMOUNT])"


End If








strSQL = "SELECT distinct [" & ChartCat & "], " & CountCost & " as total " & _


"FROM [Data$] WHERE [DATE ENTERED] >=#" & StartDate & "# AND [DATE ENTERED] <=#" & EndDate & "#"





If Range("E4") <> "All QC Errors" Then


strSQL = strSQL & " AND [INTERNAL or External] = '" & Range("E4") & "'"


End If





If Range("E7") <> "ALL" Then


strSQL = strSQL & " AND [STATUS] = '" & Range("E7") & "'"


End If





If ChartCat = "Department" Then


If Range("H5") <> "" And Range("H5") <> "All Presses" Then DepPressCat = " AND [PRESS] = '" & Range("H5") & "'"


If Range("H7") <> "" And Range("H7") <> "All Categories" Then DepPressCat = DepPressCat & " AND [ERROR CATEGORY] = '" & Range("H7") & "'"


ElseIf ChartCat = "Press" Then


If Range("H4") <> "" And Range("H4") <> "All Departments" Then DepPressCat = " AND [DEPARTMENT] = '" & Range("H4") & "'"


If Range("H7") <> "" And Range("H7") <> "All Categories" Then DepPressCat = DepPressCat & " AND [ERROR CATEGORY] = '" & Range("H7") & "'"


ElseIf ChartCat = "Error Category" Then


If Range("H4") <> "" And Range("H4") <> "All Departments" Then DepPressCat = " AND [DEPARTMENT] = '" & Range("H4") & "'"


If Range("H5") <> "" And Range("H5") <> "All Presses" Then DepPressCat = " AND [PRESS] = '" & Range("H5") & "'"


End If





strSQL = strSQL & DepPressCat & " GROUP BY [" & ChartCat & "]"





'now extract data


closeRS


OpenDB





rs.Open strSQL, cnn, adOpenKeyset, adLockPessimistic


If rs.RecordCount > 0 Then


Sheets("Charts").Visible = True


Sheets("Charts").Select


Range("chartdataSet").Select


Range(Selection, Selection.End(xlDown)).ClearContents





'Now putting the data on the sheet


rs.Sort = "total DESC"


ActiveCell.CopyFromRecordset rs


Else


MsgBox "I was not able to find any matching records.", vbExclamation + vbOKOnly


Exit Sub


End If





Range("E2").Select


closeRS


cnn.Close


End Sub








‘////

 
Upvote 0

Forum statistics

Threads
1,224,598
Messages
6,179,823
Members
452,946
Latest member
JoseDavid

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top