Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7915027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:19:15+00:00 2026-06-03T14:19:15+00:00

I am currently writing some code that can access a separate workbook through an

  • 0

I am currently writing some code that can access a separate workbook through an ADODB connection. I have chosen this instead of other methods because of speed. Here is my code below:

    Sub GetWorksheetData(strSourceFile As String, strSQL As String, TargetCell As      range)
    Dim cn As ADODB.Connection, rs As ADODB.Recordset, f As Integer, r As Long
    If TargetCell Is Nothing Then Exit Sub
    Set cn = New ADODB.Connection
    On Error Resume Next
    cn.Open "DRIVER={Microsoft Excel Driver (*.xls)};DriverId=790;ReadOnly=True;" & _
    "DBQ=" & strSourceFile & ";"
    ' DriverId=790: Excel 97/2000
    ' DriverId=22: Excel 5/95
    ' DriverId=278: Excel 4
    ' DriverId=534: Excel 3
    On Error GoTo 0
    If cn Is Nothing Then
    MsgBox "Can't find the file!", vbExclamation, ThisWorkbook.Name
    Exit Sub
    End If

    ' open a recordset
    Set rs = New ADODB.Recordset
    On Error Resume Next
    rs.Open strSQL, cn, adOpenForwardOnly, adLockReadOnly, adCmdText
    ' rs.Open "SELECT * FROM [SheetName$]", _
    cn, adOpenForwardOnly, adLockReadOnly, adCmdText
    ' rs.Open "SELECT * FROM [SheetName$]", _
    cn, adOpenStatic, adLockOptimistic, adCmdText
    ' rs.Open "SELECT * FROM [SheetName$] WHERE [Field Name] LIKE 'A%'", _
    cn, adOpenStatic, adLockOptimistic, adCmdText
    ' rs.Open "SELECT * FROM [SheetName$] WHERE [Field Name] LIKE 'A%' ORDER BY [Field                 Name]", _
    cn, adOpenStatic, adLockOptimistic, adCmdText

    ' optional ways of retrieving a recordset
    ' Set rs = cn.Execute("[A1:Z1000]") ' first worksheet
    ' Set rs = cn.Execute("[DefinedRangeName]") ' any worksheet

    On Error GoTo 0
    If rs Is Nothing Then
    MsgBox "Can't open the file!", vbExclamation, ThisWorkbook.Name
    cn.Close
    Set cn = Nothing
    Exit Sub
    End If

    'RS2WS rs, TargetCell
    TargetCell.CopyFromRecordset rs ' optional approach for Excel 2000 or later

    If rs.State = adStateOpen Then
    rs.Close
    End If
    Set rs = Nothing
    cn.Close
    Set cn = Nothing
    End Sub

Now, this code mostly works however when a row contains mixed data types, the query will skip some values. For example:

Original data:

    3844774 12505604
    3844794 12505604
    4266113 3281271
    4295817 1307HX

Returned data:

    3844774 12505604
    3844794 12505604
    4266113 3281271
    4295817 

Notice how the last bit of data is skipped. This is true for multiple entries, but only those that contain letters (making it text). The original table has everything set to text as well. Any suggestions so that it doesn’t skip these lines?

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T14:19:16+00:00Added an answer on June 3, 2026 at 2:19 pm

    That is because you are missing IMEX 🙂

    See this link (Section Treating data as text)

    http://connectionstrings.com/excel-2007

    Quote from that link.

    Treating data as text

    Use this one when you want to treat all data in the file as text, overriding Excels column type “General” to guess what type of data is in the column.

    Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcel2007file.xlsx;Extended Properties="Excel 12.0 Xml;HDR=YES;IMEX=1";
    

    If you want to read the column headers into the result set (using HDR=NO even though there is a header) and the column data is numeric, use IMEX=1 to avoid crash.

    To always use IMEX=1 is a safer way to retrieve data for mixed data columns. Consider the scenario that one Excel file might work fine cause that file’s data causes the driver to guess one data type while another file, containing other data, causes the driver to guess another data type. This can cause your app to crash.

    For XLS files see this link

    http://connectionstrings.com/excel

    HTH

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently writing a piece of code that does some searches which returns IDisposable
I'm currently writing some code where I have something along the lines of: double
I have been writing some code that detects add and removal of USB devices,
I am currently writing a little bootstrap code for a service that can be
I'm currently writing a java program that requires some Data to run. The data
We are currently going through the long process of writing some coding standards for
I'm writing some code that plays back WAV files at different speeds, so that
I'm currently writing some code for UnconstrainedMelody which has generic methods to do with
I'm writing some code to determine the name that an object is assigned to.
I currently have a macro that uses a form in order to perform some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.