jeudi 5 mars 2015

Using VBA, How to Create CheckBox in a UserForm passing the RecordSet Fields

I have Command Button which is required to show a User Form Dynamically as Below, with the Column Headers fetched from a Record Set as a Check Box Option.


Table Range


User Form


The Code that I Have for Initialize is as below



Private Sub UserForm_Initialize()
Set xl1 = Excel.Application
Dim xlXML As Object
Dim adoRecordset As Object
Dim rng As Range
Dim recordCount1 As Long
Dim Fieldcount1 As Long
xl1.ThisWorkbook.Sheets(2).Activate
Set rng = xl1.ThisWorkbook.Sheets(2).Range("A1:I15")
Set adoRecordset = CreateObject("ADODB.Recordset")
Set xlXML = CreateObject("MSXML2.DOMDocument")
xlXML.LoadXML rng.Value(xlRangeValueMSPersistXML)
adoRecordset.Open xlXML
adoRecordset.MoveFirst
Fieldcount1 = adoRecordset.Fields.Count
Dim i As Long
Dim chkBox As CheckBox
For i = 1 To Fieldcount1
Set chkBox = UserForm1.Controls.Add("Forms.Checkbox.1", "Checkbox" & i)
chkBox.Caption = adoRecordset.Fields(i - 1)
chkBox.Value = False
chkBox.Top = (chkBox.Height + Spacing) * (i - 1)
Next i

End Sub


I am getting "Run-time error '13': Type mismatch" error


Could you please help me on this.


I am relatively a beginner in Scripting


Aucun commentaire:

Enregistrer un commentaire