Click here to Skip to main content
15,847,653 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Dear Experts,

Here I am doing small Sample project in VBA Named as sample.accdb.
In this Project we are not using any linked database
In the Main form i am using one subform.

The code as follows

Private Sub Form_Load()
Dim s As String
Dim k As Integer
s = "Provider=Microsoft.Access.OLEDB.10.0;Data Source=MyServerName;Initial Catalog=DataBaseName;Uid=sa;Pwd=Password;trusted connection = yes;Data Provider=SQLOLEDB;"
Dim aCon As ADODB.Connection
Dim aRs As ADODB.Recordset
Set aCon = New ADODB.Connection

With aCon
    .Open s
    Set aRs = .Execute("SELECT * from AirCL2Loss_GNP_HU_TY13_Use.dbo.Sample")
End With

k = aRs.Fields.Count
k = k - 1

Set Me.NewSubForm.Form.Recordset = aRs
For x = 0 To k
        Form_Form1("NewSubForm")("Id" & x + 1).ControlSource = aRs(x).Name
        Form_Form1("NewSubForm")("Id" & x + 1 & "_Label").Caption = aRs(x).Name
        Form_Form1("NewSubForm")("Id" & x + 1).Visible = True
        If x = k Then Exit For
End Sub

At this line Set Me.NewSubForm.Form.Recordset = aRs i am getting error like

Runtime error '-2147217843 (80040e4d)' Data Provider could not be intialized

Can you please tell why this error coming.
Updated 2-May-13 20:55pm

1 solution

Share this answer
dmunisubbu 15-May-13 2:46am    
Dear Maciej,

If connection string is wrong, data won't stored in record set.

using the above connection string i am getting the data and stored in record set.
Maciej Los 15-May-13 3:15am    
No, it isn't! Have a look at it: "Provider=Microsoft.Access.OLEDB.10.0;Data Source=MyServerName;Initial Catalog=DataBaseName;Uid=sa;Pwd=Password;trusted connection = yes;Data Provider=SQLOLEDB;". A word Provider is used twice. Follow this link:[^] to get correct connection string. In case of proper connection string, you'll NOT get this error: "Runtime error '-2147217843 (80040e4d)' Data Provider could not be intialized"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900