azizrasul

Well-known Member
Joined
Jul 7, 2003
Messages
1,304
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
How do I change the following code, which works, so that it is early binding?

Code:
    Dim strConnect As String
    Dim strAccessFile As String
    Dim strSQL As String
    Dim rst As Object
    Dim strLandParcelID As String
    
    On Error GoTo ErrorHandler
    
    strAccessFile = "C:\Users\Aziz.Rasul\Scheduling Database - Back End.accdb"
    
    If Application.Version < 12 Then
        strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strAccessFile
    Else
        strConnect = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strAccessFile
    End If

    'Late binding, so no reference is needed.
    Set cnn = CreateObject("ADODB.Connection")

    cnn.Open strConnect
    
    strSQL = "SELECT tblTHAs.customer, tblTHAs.landparcelID, tblTHAs.surveyID, tblTHAs.surveydate, tblTHAs.surveyorID, tblTHAs.secondID, tblTHAsDetailed.hazards FROM tblTHAs INNER JOIN tblTHAsDetailed ON tblTHAs.surveyID = tblTHAsDetailed.surveyID ORDER BY tblTHAs.landparcelID, tblTHAs.surveydate;"

'    cnn.Execute strSQL 'Use this to run an action query.
    
    Set rst = cnn.Execute(strSQL)
    
    Do While Not rst.EOF
        strLandParcelID = rst!landparcelID
        rst.MoveNext
    Loop
    
    rst.Close
    Set rst = Nothing

ErrorHandler:
    If Err.Number <> 0 Then
        MsgBox Err.Number & " - " & Err.Description
    End If
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Firstly, in the VBA Editor, you'll need to go to Tool>References and add a reference to Microsoft ActiveX Data Objects 6.1 Library

Once you've done that, change the code to this:

Code:
    Dim strConnect As String
    Dim strAccessFile As String
    Dim strSQL As String
    Dim rst As ADODB.Recordset
    Dim strLandParcelID As String
    Dim cnn As ADODB.Connection
    
    On Error GoTo ErrorHandler
    
    strAccessFile = "C:\Users\Aziz.Rasul\Scheduling Database - Back End.accdb"
    
    If Application.Version < 12 Then
        strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strAccessFile
    Else
        strConnect = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strAccessFile
    End If

    'Late binding, so no reference is needed.
    Set cnn = New ADODB.Connection

    cnn.Open strConnect
    
    strSQL = "SELECT tblTHAs.customer, tblTHAs.landparcelID, tblTHAs.surveyID, tblTHAs.surveydate, tblTHAs.surveyorID, tblTHAs.secondID, tblTHAsDetailed.hazards FROM tblTHAs INNER JOIN tblTHAsDetailed ON tblTHAs.surveyID = tblTHAsDetailed.surveyID ORDER BY tblTHAs.landparcelID, tblTHAs.surveydate;"

'    cnn.Execute strSQL 'Use this to run an action query.
    
    Set rst = cnn.Execute(strSQL)
    
    Do While Not rst.EOF
        strLandParcelID = rst!landparcelID
        rst.MoveNext
    Loop
    
    rst.Close
    Set rst = Nothing

ErrorHandler:
    If Err.Number <> 0 Then
        MsgBox Err.Number & " - " & Err.Description
    End If

WBD
 
Upvote 0
Great, many thanks. Oh and good luck with the England match.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,720
Members
448,986
Latest member
andreguerra

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