ClimoC
Well-known Member
- Joined
- Aug 21, 2009
- Messages
- 584
Hi
I wrote this:
which works fine when it's an open workbook (or at least, it did). I then saved it as an add-in, XML'd in a Ribbon button, and whilst it all seems to execute fine, when the call procedure is finished, the RecordSet Object is Nothing.
Thoughts? Basic principle I'm missing? I've done similar with a workbook before, but the Recordset is created in the document (and maintains itself when you switch between workbooks/sheets, you come back and it's still there, in memory)
Help? :D
C
nb: When I step through it, it's still there right up to 'End Sub' on the Ribbon callback. Then, immediate-window '?CRS is nothing' becomes True. This didn't happen with my other Button-make-recordset - the only difference I can see is that this is for an Add-in, not a workbook.
I wrote this:
Code:
Global CRS As ADODB.Recordset
Public ColType As Integer, ColSize As Integer
Public Arr1() As Variant, Arr2() As Variant, RuleList() As Variant
Public frmEvents As Boolean
Public Function BuildRecordSet()
Dim i As Long, z As Long, XCol As Long, YRow As Long, FRow As Long, FUCV1 As Range, FUCV2 As Range, FUCV3 As Range, COlTypeCOll As New Scripting.Dictionary, StrRplcColHead As String, CollTypeSum As Double
Set CRS = New ADODB.Recordset
With Selection
XCol = .End(xlToRight).Column 'Width in Columns
YRow = .Rows.Count + .Rows(1).Row - 1
FRow = .Rows(1).Row
For i = .Columns(1).Column To XCol
'Test 3 values from the field, to determine Data type guestimate
Set FUCV1 = Cells(FRow, i).End(xlDown)
Set FUCV2 = Cells(FRow, i).Offset(1, 0)
Set FUCV3 = Cells(WorksheetFunction.RandBetween(FUCV2.Row, FUCV1.Row), i)
Select Case TypeName(FUCV1.Value)
Case "Double"
CollTypeSum = 1
Case "String"
CollTypeSum = 100
Case "Date"
CollTypeSum = 10
Case "Boolen"
CollTypeSum = 0.1
Case Else
CollTypeSum = 100
End Select
Select Case TypeName(FUCV2.Value)
Case "Double"
CollTypeSum = CollTypeSum + 1
Case "String"
CollTypeSum = CollTypeSum + 100
Case "Date"
CollTypeSum = CollTypeSum + 10
Case "Boolen"
CollTypeSum = CollTypeSum + 0.1
Case Else
CollTypeSum = CollTypeSum + 100
End Select
Select Case TypeName(FUCV3.Value)
Case "Double"
CollTypeSum = CollTypeSum + 1
Case "String"
CollTypeSum = CollTypeSum + 100
Case "Date"
CollTypeSum = CollTypeSum + 10
Case "Boolen"
CollTypeSum = CollTypeSum + 0.1
Case Else
CollTypeSum = CollTypeSum + 100
End Select
CRSFirstForm.ComboBox1.AddItem Cells(FRow, i).Value
CRSFirstForm.ListBox1.AddItem Cells(FRow, i).Value
Select Case CollTypeSum
Case Is < 1
CRSFirstForm.ListBox1.List(CRSFirstForm.ListBox1.ListCount - 1, 1) = "Boolean"
Case 1.3 To 3
CRSFirstForm.ListBox1.List(CRSFirstForm.ListBox1.ListCount - 1, 1) = "Double"
Case 10.3 To 21
CRSFirstForm.ListBox1.List(CRSFirstForm.ListBox1.ListCount - 1, 1) = "~String"
Case 30
CRSFirstForm.ListBox1.List(CRSFirstForm.ListBox1.ListCount - 1, 1) = "Date"
Case Is > 100
CRSFirstForm.ListBox1.List(CRSFirstForm.ListBox1.ListCount - 1, 1) = "~String"
Case Else
CRSFirstForm.ListBox1.List(CRSFirstForm.ListBox1.ListCount - 1, 1) = "~String"
End Select
Next i
CRSFirstForm.ComboBox2.AddItem "Double"
CRSFirstForm.ComboBox2.AddItem "String"
CRSFirstForm.ComboBox2.AddItem "Date"
CRSFirstForm.ComboBox2.AddItem "Boolean"
CRSFirstForm.Show
End With
'Build
With CRS
For z = 1 To UBound(Arr1())
CRS.Fields.Append CStr(Arr1(z, 1)), CInt(Arr1(z, 2)), CInt(Arr1(z, 3))
Next
.LockType = adLockOptimistic
.CursorType = 3
.CursorLocation = 3
.CacheSize = 1000
.Open
On Error Resume Next
For z = (FRow + 1) To YRow
CRS.AddNew
For i = Selection.Columns(1).Column To XCol
Err.Clear
CRS.Fields(i - Selection.Columns(1).Column).Value = WB.Sheets(1).Cells(z, i).Value
If Err.Number <> 0 Then
Select Case CRS.Fields(i - 1).Type
Case Is < 1
CRS.Fields(i - 1).Value = 0
Case 1.3 To 3
CRS.Fields(i - 1).Value = 0#
Case 10.3 To 21
CRS.Fields(i - 1).Value = ""
Case 30
CRS.Fields(i - 1).Value = CDate("01/01/1901")
Case Is > 100
CRS.Fields(i - 1).Value = ""
Case Else
CRS.Fields(i - 1).Value = ""
End Select
End If
Next
Next
Err.Clear
On Error GoTo 0
.Update
End With
CRS.MoveFirst
End Function
Public Sub RibbonCall(control As IRibbonControl)
Select Case control.ID
Case "addCRS"
BuildRecordSet
End Select
End Sub
which works fine when it's an open workbook (or at least, it did). I then saved it as an add-in, XML'd in a Ribbon button, and whilst it all seems to execute fine, when the call procedure is finished, the RecordSet Object is Nothing.
Thoughts? Basic principle I'm missing? I've done similar with a workbook before, but the Recordset is created in the document (and maintains itself when you switch between workbooks/sheets, you come back and it's still there, in memory)
Help? :D
C
nb: When I step through it, it's still there right up to 'End Sub' on the Ribbon callback. Then, immediate-window '?CRS is nothing' becomes True. This didn't happen with my other Button-make-recordset - the only difference I can see is that this is for an Add-in, not a workbook.
Last edited: